Created
February 27, 2023 13:50
-
-
Save jrasanen/9bcaef0f1bc0f1e759237b6d7e416b73 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bb | |
(require '[babashka.process :refer [process shell sh pipeline pb]]) | |
(def +api-url+ "https://gitlab.com/api/v4") | |
(def +gitlab-personal-token+ (System/getenv "GITLAB_ACCESS_TOKEN")) | |
(def +gitlab-user+ (System/getenv "GITLAB_USERNAME")) | |
;; | |
;; Utils | |
;; | |
(defn not-empty [x] (seq x)) | |
;; | |
;; Gitlab API | |
;; | |
(defn gitlab-rest-action | |
[action] | |
(str +api-url+ action)) | |
(defn gitlab-headers | |
[] | |
{"Accept" "application/json" | |
"Authorization" (str "Bearer " +gitlab-personal-token+)}) | |
(defn make-gitlab-api-query | |
[action headers] | |
(-> (curl/get action {:headers headers}) | |
:body | |
(json/parse-string true))) | |
(defn get-mege-requests | |
[state] | |
(str (gitlab-rest-action "/merge_requests?state=") state)) | |
(defn get-open-merge-requests-for-user | |
[username] | |
(let [action (str (get-mege-requests "opened") "&author_username=" username) | |
headers (gitlab-headers)] | |
(make-gitlab-api-query action headers))) | |
(defn find-url-by-branch | |
[merge-requests predicate-branch] | |
(->> merge-requests | |
(filter (fn [{:keys [source_branch]}] (= source_branch predicate-branch))) | |
(map :web_url) | |
last)) | |
;; | |
;; Git | |
;; | |
(def get-source-branches (partial map :source_branch)) | |
(defn local-branch-exists? | |
[branch-name] | |
(let [branch-checksum (-> (pipeline (pb "git" "rev-parse" "--verify" branch-name) | |
(pb "cat")) | |
last | |
:out | |
slurp)] | |
(if (not-empty branch-checksum) | |
[branch-checksum branch-name] | |
false))) | |
(defn get-current-branch-name | |
[] | |
(-> (pipeline (pb "git" "name-rev" "--name-only" "HEAD") | |
(pb "cat")) | |
last | |
:out | |
slurp | |
clojure.string/trim-newline)) | |
;; | |
;; CLI | |
;; | |
(defn open-url | |
([] (print "No URL found for branch\n")) | |
([url] (if url | |
(shell (str "open " url)) | |
(open-url)))) | |
(defn print-usage | |
[command] | |
(print (str "Unknown command '" command "'\nCommmands known: open, ls\n"))) | |
; Commands | |
(defn command-open | |
"Open the current branch merge request in browser" | |
[] | |
(let [user-merge-requests (get-open-merge-requests-for-user +gitlab-user+) | |
current-branch (get-current-branch-name) | |
web-url (find-url-by-branch user-merge-requests current-branch)] | |
(open-url web-url))) | |
(defn command-ls | |
"List all local git branches that have an open merge request" | |
[] | |
(->> (get-open-merge-requests-for-user +gitlab-user+) | |
(get-source-branches) | |
(map local-branch-exists?) | |
(map last) | |
(pprint))) | |
(defn main | |
[] | |
(let [command (first *command-line-args*)] | |
(case command | |
"open" (command-open) | |
"ls" (command-ls) | |
(print-usage command)))) | |
(main) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment