executable_clone-gitlab.intr.scm 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env -S guile --no-auto-compile -e main -s
  2. !#
  3. (use-modules (ice-9 format)
  4. (ice-9 match)
  5. (ice-9 popen)
  6. (ice-9 rdelim)
  7. (json)
  8. (srfi srfi-1))
  9. (define gitlab-url
  10. (and=> (getenv "GITLAB_URL")
  11. (lambda (gitlab-url)
  12. gitlab-url)))
  13. (define gitlab-token
  14. (and=> (getenv "GITLAB_TOKEN")
  15. (lambda (gitlab-token)
  16. gitlab-token)))
  17. (define (main . args)
  18. (let* ((port (apply open-pipe* OPEN_READ
  19. (list "curl"
  20. "--header" (format #f "PRIVATE-TOKEN: ~a" gitlab-token)
  21. "--header" "Content-Type: application/json"
  22. "--silent"
  23. "--insecure"
  24. "--request" "GET"
  25. (string-append gitlab-url "/api/v4/groups"))))
  26. (output (read-string port)))
  27. (close-port port)
  28. (map (lambda (group)
  29. (let* ((port (apply open-pipe* OPEN_READ
  30. (list "curl"
  31. "--header" (format #f "PRIVATE-TOKEN: ~a" gitlab-token)
  32. "--header" "Content-Type: application/json"
  33. "--silent"
  34. "--insecure"
  35. "--request" "GET"
  36. (string-append gitlab-url "/api/v4/groups/"
  37. (number->string (assoc-ref group "id"))))))
  38. (output (read-string port)))
  39. (close-port port)
  40. (unless (file-exists? (assoc-ref group "name"))
  41. (mkdir (assoc-ref group "full_path")))
  42. (for-each (lambda (project)
  43. (system* "git" "clone"
  44. (assoc-ref project "ssh_url_to_repo")
  45. (string-append (assoc-ref group "full_path")
  46. "/"
  47. (assoc-ref project "path"))))
  48. (array->list (assoc-ref (json-string->scm output) "projects"))))
  49. (assoc-ref group "full_path"))
  50. (array->list (json-string->scm output)))))