 (require '[clj-fileutils.fileutils :as file-utils])
 (require '[clj-jenkins-api.jenkins-api :as jenkins-api])
 
-(def verbose false)
-(def port    3000)
-(def workdir "/tmp/bare-repositories")
-(def job-list-command "/api/json?tree=jobs[name]")
-(def branch-job-prefix "branch-")
-
-
-(def jenkins-url (System/getenv "JENKINS_URL"))
+(def verbose
+  "Flag to enable or disable verbosity level."
+  false)
+
+(def port
+  "Default port for the service."
+  3000)
+
+(def workdir
+  "Working directory."
+  "/tmp/bare-repositories")
+
+(def job-list-command
+  "Command used to retrieve job list from Jenkins."
+  "/api/json?tree=jobs[name]")
+
+(def branch-job-prefix
+  "Prefix used for all 'branch' build jobs."
+  "branch-")
+
+(def jenkins-url
+  "URL to Jenkins server."
+  (System/getenv "JENKINS_URL"))
 
 (defn read-request-body
   "Read all informations from the request body."

            (repo-workdir workdir group reponame)))

(defn read-branch-list-from-repo
  [workdir group reponame]
  (let [repodir (repo-workdir workdir group reponame)]
    (log/info "Reading branches from repository" repodir)

  (file-utils/slurp- (:body request)))

(defn log-request
  "Record an event about request into log."
  [request]
  (log/info "Handling request: " (:uri request) (:remote-addr request))
  (if verbose
    (clojure.pprint/pprint request)))

(defn send-response
  "Send response in HTML format to client."
  [response-text]
  (-> (http-response/response response-text)
      (http-response/content-type "text/html")))

(defn parse-gitlab-info
  "Parse payload sent from GitLab in response body."
  [request]
  (let [body (read-request-body request)]
    (if body
      (json/read-str body :key-fn clojure.core/keyword))))

(defn repo-workdir
  "Construct name of work directory for given group and repository name."
  [workdir group reponame]
  (string/join "/" [workdir group reponame]))

(defn repodir-exists?
  "Check if directory exist for given group and repository name."
  [workdir group reponame]
  (let [f (new java.io.File (repo-workdir workdir group reponame))]
    (.isDirectory f)))

(defn fetch-mirror-repo
  "Fetch the content of selected repository."
  [workdir group reponame repository-url]
  (log/info "Fetching mirror repository" repository-url)
  ; TODO test --prune
  (shell/sh "git" "--git-dir" (repo-workdir workdir group reponame) "fetch"))

(defn clone-mirror-repo
  "Clone content of selected repository."
  [workdir group reponame repository-url]
  (log/info "Cloning mirror repository" repository-url)
  (shell/sh "git"
