Skip to content

Instantly share code, notes, and snippets.

@MrGung
Created November 10, 2020 15:31
Show Gist options
  • Save MrGung/81bee21eb52cb9307f336705d5ab08ad to your computer and use it in GitHub Desktop.
Save MrGung/81bee21eb52cb9307f336705d5ab08ad to your computer and use it in GitHub Desktop.
Gitlab: Clone all repositories in group
(ns gitlab.clone-group
(:require
[cheshire.core :as json]
[gitlab.api :refer [get-config]]
[babashka.curl :as curl]
[clojure.java.io :as io]
[clojure.tools.cli :refer [parse-opts]]
[babashka.process :refer [$]]))
(defn clone-all-repos-from-group [{:keys [gitlab-token gitlab-root group-id local-root]}]
(when-not (.exists (io/file local-root))
(.mkdir (io/file local-root))
(println "created directory" local-root))
(let [url (format "%s/groups/%s/projects?per_page=100" gitlab-root group-id) ;; pagination! max: 100
projects (-> (curl/get url {:headers {"PRIVATE-TOKEN" gitlab-token}
:raw-args ["--insecure"]})
:body
(json/parse-string true))
clone-urls (map :http_url_to_repo projects)]
(doseq [clone-url clone-urls]
(println "cloning" clone-url)
@^{:dir local-root :out :inherit :err :inherit} ($ git clone ~clone-url)
(println " ...done\n"))))
;; replace (get-config) with your values for gitlab-token and gitlab-root
(let [{:keys [gitlab-token gitlab-root]} (get-config)]
(def cli-options
;; An option with a required argument
[["-t" "--gitlab-token GITLAB-TOKEN" :required "Gitlab-token"
:default gitlab-token]
["-g" "--group-id GROUP-ID" :required "ID of Gitlab-group to clone"
:parse-fn #(Integer/parseInt %)]
["-r" "--gitlab-root GITLAB-ROOT" :required "Root of the Gitlab-API - https://gitlab.host/api/v4"
:default gitlab-root]
["-l" "--local-root LOCAL-ROOT" :required "Directory to which to clone"]
["-h" "--help"]]))
(if (= *file* (System/getProperty "babashka.file"))
(let [parsed-arguments (parse-opts *command-line-args* cli-options)]
(clone-all-repos-from-group (:options parsed-arguments))))
;; bb -f clone_group.clj --local-root d:\Users\Steffen\git\t -g 2391
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment