Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions bb.edn
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@

watch-ci
{:requires [[bb.dl-and-run :as dl]
[clojure.pprint :as pp]]
[bb.watch-ci :as watch-ci]]
:doc "Watch the CI status for a branch until it passes."
:examples [["bb watch-ci --branch current" "Check currently checked out branch in MB_DIR repo"]]
:task
Expand All @@ -79,25 +79,15 @@
:msg "What branch should we check?"
:short "-b"
:long "--branch BRANCH"
:choices (delay (t/list-branches (t/mb-env)))
:choices (delay (t/list-branches (t/mb-dir)))
:choices-doc "list of branches"
:prompt :select})
branch (if (= "current" branch)
(let [current (str/trim
(:out (shell {:dir (t/env "MB_DIR" (fn []
(println "Please set MB_DIR to your metabase repo!")
(System/exit 1))) :out :string}
"git rev-parse --abbrev-ref HEAD")))]
(do (println (c/green (c/bold "Checking current branch: " current)))
(let [current (t/current-branch)]
(do (println (c/green (c/bold "Using current branch: " (t/current-branch))))
current))
branch)]
(pp/pprint dl/pretty)
(loop []
(let [check (dl/checks-for-branch branch)]
(print (str "\n[ " (.format (java.text.SimpleDateFormat. "hh:mm:ss a") (java.util.Date.)) " ]"))
(doseq [[status count] check] (print (str/join (repeat count (dl/pretty status)))) (print "|")) (flush)
(when-not (= (keys check) [:success])
(Thread/sleep 10000) (recur))))))}
(watch-ci/branch branch)))}

install-autotab {:doc "Prints shell code to autocomplete tasks using bb.
Note: for fish shell please make sure ~/.config/fish/completions exists."
Expand Down
20 changes: 1 addition & 19 deletions bb/dl_and_run.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
[bb.tasks :as t]
[cheshire.core :as json]
[clojure.edn :as edn]
[clojure.string :as str]
[selmer.parser :refer [<<]]))

(defn- keep-first
"like (fn [f coll] (first (keep f coll))) but does not do chunking."
[f coll]
(reduce (fn [_ element] (when-let [resp (f element)] (reduced resp))) nil coll))

(defn- gh-get [url]
(defn gh-get [url]
(try (-> url
(curl/get {:headers {"Accept" "application/vnd.github+json"
"Authorization" (str "Bearer " (t/env "GH_TOKEN"))}})
Expand Down Expand Up @@ -141,20 +140,3 @@
(defn download-and-run-latest-jar! [{:keys [branch port socket-repl] :as args}]
(let [info (download-latest-jar! args)]
(run-jar! info port socket-repl)))

(def pretty {:success "✅"
:skipped "⏭️ "
:cancelled "⏹️"
:in-progress "🔄"
:failure "❌"})

(defn checks-for-branch [branch]
;; note: this is a ref, so it can e.g. also be a sha.
(->> (str "https://api.github.com/repos/metabase/metabase/commits/" branch "/check-runs")
gh-get
:check_runs
(mapv :conclusion)
(mapv (fn [x] (if (nil? x) :in-progress (keyword x))))
frequencies
(sort-by first)
reverse))
4 changes: 2 additions & 2 deletions bb/quick_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
[bb.tasks :as t]
[clojure.string :as str]))

(defn- test-path [] (str (t/mb-env) "/test"))
(defn- test-path [] (str (t/mb-dir) "/test"))

(defn- file->ns [path]
(-> path
str
(str/replace (str (t/mb-env) "/test/") "")
(str/replace (str (t/mb-dir) "/test/") "")
(str/replace #"\.clj$" "")
(str/replace "_" "-")
(str/replace "/" ".")))
Expand Down
9 changes: 8 additions & 1 deletion bb/tasks.clj
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
(print (c/white "="))
(println (c/cyan value))))))

(defn mb-env []
(defn mb-dir []
(env "MB_DIR" (fn []
(println (c/red "Please put the path of your metabase repository into the MB_DIR env variable like so:"))
(println (c/white "export MB_DIR=path/to/metabase"))
Expand Down Expand Up @@ -99,3 +99,10 @@
_ (bencode/write-bencode out {"op" "eval" "code" expr})
bytes (get (bencode/read-bencode in) "value")]
(String. bytes)))

(defn current-branch
"Returns current metabase repo branch, as given by MB_DIR"
[]
(str/trim
(:out (shell {:dir (mb-dir) :out :string}
"git rev-parse --abbrev-ref HEAD"))))
44 changes: 44 additions & 0 deletions bb/watch_ci.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
(ns bb.watch-ci
(:require [clojure.string :as str]
[bb.dl-and-run :as dl]
[table.core :as table]))

(defn checks-for-branch [branch]
(->> (str "https://api.github.com/repos/metabase/metabase/commits/" branch "/check-runs")
dl/gh-get
:check_runs
(mapv #(select-keys % [:conclusion :name :html_url]))
(mapv #(update % :conclusion (fnil keyword "in-progress")))
(sort-by :conclusion)))

(defn tc [color & s]
(let [cm {:bold 1
:gray 30 :grey 30 :red 31 :green 32 :yellow 33
:blue 34 :magenta 35 :cyan 36 :white 37
:on-gray 40 :on-grey 40 :on-red 41 :on-green 42 :on-yellow 43
:on-blue 44 :on-magenta 45 :on-cyan 46 :on-white 47}]
(if-let [c (get cm color)]
(str "[" c "m" (str/join s) "\033[0m")
(str/join s))))

(defn colorize-line [line]
(or (first (for [[re color] [[#"success" :green] [#"in-progress" :cyan] [#"failure" :red] [#"skipped" :yellow]]
:when (re-find re line)]
(tc color line)))
line))

(defn branch [branch]
(loop []
(let [checks (checks-for-branch branch)]
(println (tc :red (tc :bold branch)) (str "[ " (.format (java.text.SimpleDateFormat. "hh:mm:ss a") (java.util.Date.)) " ]"))
(->> (table/table-str checks)
str/split-lines
(map colorize-line)
(str/join "\n")
println)
(if (= #{:success} (set (map :conclusion checks)))
(do
(println (tc :green branch "passed."))
(System/exit 0))
(do (Thread/sleep 10000)
(recur))))))