Skip to content

Commit 32a97e6

Browse files
authored
Merge pull request #23 from metabase/better_watch_ci
Better watch-ci task
2 parents 9519cc0 + d953507 commit 32a97e6

File tree

5 files changed

+60
-37
lines changed

5 files changed

+60
-37
lines changed

bb.edn

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969

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

10292
install-autotab {:doc "Prints shell code to autocomplete tasks using bb.
10393
Note: for fish shell please make sure ~/.config/fish/completions exists."

bb/dl_and_run.clj

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
[bb.tasks :as t]
77
[cheshire.core :as json]
88
[clojure.edn :as edn]
9-
[clojure.string :as str]
109
[selmer.parser :refer [<<]]))
1110

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

17-
(defn- gh-get [url]
16+
(defn gh-get [url]
1817
(try (-> url
1918
(curl/get {:headers {"Accept" "application/vnd.github+json"
2019
"Authorization" (str "Bearer " (t/env "GH_TOKEN"))}})
@@ -141,20 +140,3 @@
141140
(defn download-and-run-latest-jar! [{:keys [branch port socket-repl] :as args}]
142141
(let [info (download-latest-jar! args)]
143142
(run-jar! info port socket-repl)))
144-
145-
(def pretty {:success ""
146-
:skipped "⏭️ "
147-
:cancelled "⏹️"
148-
:in-progress "🔄"
149-
:failure ""})
150-
151-
(defn checks-for-branch [branch]
152-
;; note: this is a ref, so it can e.g. also be a sha.
153-
(->> (str "https://api.github.com/repos/metabase/metabase/commits/" branch "/check-runs")
154-
gh-get
155-
:check_runs
156-
(mapv :conclusion)
157-
(mapv (fn [x] (if (nil? x) :in-progress (keyword x))))
158-
frequencies
159-
(sort-by first)
160-
reverse))

bb/quick_test.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
[bb.tasks :as t]
77
[clojure.string :as str]))
88

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

1111
(defn- file->ns [path]
1212
(-> path
1313
str
14-
(str/replace (str (t/mb-env) "/test/") "")
14+
(str/replace (str (t/mb-dir) "/test/") "")
1515
(str/replace #"\.clj$" "")
1616
(str/replace "_" "-")
1717
(str/replace "/" ".")))

bb/tasks.clj

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
(print (c/white "="))
6262
(println (c/cyan value))))))
6363

64-
(defn mb-env []
64+
(defn mb-dir []
6565
(env "MB_DIR" (fn []
6666
(println (c/red "Please put the path of your metabase repository into the MB_DIR env variable like so:"))
6767
(println (c/white "export MB_DIR=path/to/metabase"))
@@ -99,3 +99,10 @@
9999
_ (bencode/write-bencode out {"op" "eval" "code" expr})
100100
bytes (get (bencode/read-bencode in) "value")]
101101
(String. bytes)))
102+
103+
(defn current-branch
104+
"Returns current metabase repo branch, as given by MB_DIR"
105+
[]
106+
(str/trim
107+
(:out (shell {:dir (mb-dir) :out :string}
108+
"git rev-parse --abbrev-ref HEAD"))))

bb/watch_ci.clj

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
(ns bb.watch-ci
2+
(:require [clojure.string :as str]
3+
[bb.dl-and-run :as dl]
4+
[table.core :as table]))
5+
6+
(defn checks-for-branch [branch]
7+
(->> (str "https://api.github.com/repos/metabase/metabase/commits/" branch "/check-runs")
8+
dl/gh-get
9+
:check_runs
10+
(mapv #(select-keys % [:conclusion :name :html_url]))
11+
(mapv #(update % :conclusion (fnil keyword "in-progress")))
12+
(sort-by :conclusion)))
13+
14+
(defn tc [color & s]
15+
(let [cm {:bold 1
16+
:gray 30 :grey 30 :red 31 :green 32 :yellow 33
17+
:blue 34 :magenta 35 :cyan 36 :white 37
18+
:on-gray 40 :on-grey 40 :on-red 41 :on-green 42 :on-yellow 43
19+
:on-blue 44 :on-magenta 45 :on-cyan 46 :on-white 47}]
20+
(if-let [c (get cm color)]
21+
(str "[" c "m" (str/join s) "\033[0m")
22+
(str/join s))))
23+
24+
(defn colorize-line [line]
25+
(or (first (for [[re color] [[#"success" :green] [#"in-progress" :cyan] [#"failure" :red] [#"skipped" :yellow]]
26+
:when (re-find re line)]
27+
(tc color line)))
28+
line))
29+
30+
(defn branch [branch]
31+
(loop []
32+
(let [checks (checks-for-branch branch)]
33+
(println (tc :red (tc :bold branch)) (str "[ " (.format (java.text.SimpleDateFormat. "hh:mm:ss a") (java.util.Date.)) " ]"))
34+
(->> (table/table-str checks)
35+
str/split-lines
36+
(map colorize-line)
37+
(str/join "\n")
38+
println)
39+
(if (= #{:success} (set (map :conclusion checks)))
40+
(do
41+
(println (tc :green branch "passed."))
42+
(System/exit 0))
43+
(do (Thread/sleep 10000)
44+
(recur))))))

0 commit comments

Comments
 (0)