Skip to content

Instantly share code, notes, and snippets.

@kornysietsma
Created November 6, 2014 16:28
Show Gist options
  • Save kornysietsma/bb1f2548d6893175a5a5 to your computer and use it in GitHub Desktop.
Save kornysietsma/bb1f2548d6893175a5a5 to your computer and use it in GitHub Desktop.
gitlab stuff
(ns gitlab-utils.config)
(def api-base "http://localhost/api/v3/")
(ns gitlab-utils.fetch-all
(:require [cheshire.core :as c]
[clj-http.client :as client]
[clojure.java.io :as io]
[gitlab-utils.config :as config]
[gitlab-utils.gitlab :as gitlab]
)
(:gen-class))
(defn all-projects [token]
(gitlab/get-paginated (str config/api-base "projects") token))
(defn named-projects [token]
(into {}
(for [p (all-projects token)]
[(:path p )
(:ssh_url_to_repo p)])))
(defn dir-names-in [root-dir]
(into #{} (->> (.listFiles (io/file root-dir))
(filter #(.isDirectory %))
(map #(.getName %)))))
(defn get-or-pull-all [root-dir token]
(let [existing-dirs (dir-names-in root-dir)
projects (named-projects token)]
(clojure.string/join
"\n"
(concat
["#!/bin/bash -e"]
(flatten
(for [[name url] projects]
(if (existing-dirs name)
[ (str "echo " name)
(str "cd " name)
"git pull --rebase"
"cd .."]
[ (str "echo " name)
(str "git clone " url)]))
)))))
(defn -main
[& args]
(if (not (= 2 (count args)))
(println "please specify two parameters - the path to your base project dir, and your gitlab security token (from your gitlab profile page)")
(println (get-or-pull-all (first args) (second args)))))
(ns gitlab-utils.gitlab
(:require [gitlab-utils.config :as config]
[clj-http.client :as client]
[cheshire.core :as c]))
(defn get-token [login password]
(let [url (str config/api-base "session")
resp (client/post url {:form-params {:login login :password password}})
data (c/parse-string (:body resp) true)]
(:private_token data)))
(defn private-headers [token]
{:headers {"PRIVATE-TOKEN" token}})
(defn get-paginated "get a url returning a sequence of JSON data, following next: headers to traverse all pages"
([url token] (get-paginated [] url token))
([data-so-far url token]
(let [resp (client/get url (private-headers token))
data (concat data-so-far (c/parse-string (:body resp) true))]
(if-let [next-url (get-in resp [:links :next :href])]
(recur data next-url token)
data))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment