|
| 1 | +#!/usr/bin/env bb |
| 2 | + |
| 3 | +(ns watch-ci |
| 4 | + (:require [babashka.curl :as curl] |
| 5 | + [babashka.tasks :refer [shell]] |
| 6 | + [bask.colors :as c] |
| 7 | + [bb.cli :as cli] |
| 8 | + [cheshire.core :as json] |
| 9 | + [clojure.edn :as edn] |
| 10 | + [clojure.pprint :as pp] |
| 11 | + [clojure.string :as str] |
| 12 | + [selmer.parser :refer [<<]])) |
| 13 | + |
| 14 | +(defn env |
| 15 | + ([] (into {} (System/getenv))) |
| 16 | + ([env-var] (env env-var (fn [] (println "Warning: cannot find " (c/red env-var) " in env.")))) |
| 17 | + ([env-var error-thunk] (or ((env) (name env-var)) (error-thunk)))) |
| 18 | + |
| 19 | +(defn- gh-GET [url] |
| 20 | + (try (-> url |
| 21 | + (curl/get {:headers {"Accept" "application/vnd.github+json" "Authorization" (str "Bearer " (env "GH_TOKEN"))}}) |
| 22 | + :body |
| 23 | + (json/decode true)) |
| 24 | + (catch Exception e |
| 25 | + (let [{:keys [status]} (ex-data e)] |
| 26 | + (when (= status 401) (println (c/red "Is your GH_TOKEN out of date?"))) |
| 27 | + (throw (ex-info (str "Error trying to get url " url " status: " status) |
| 28 | + {:status status :url url})))))) |
| 29 | + |
| 30 | +(def mb-dir (env "MB_DIR" (fn [] |
| 31 | + (println (c/red "Please set MB_DIR env variable to your metabase directory!")) |
| 32 | + (System/exit 1)))) |
| 33 | + |
| 34 | +(defn current-branch [] |
| 35 | + (->> "git rev-parse --abbrev-ref HEAD" |
| 36 | + (shell {:dir mb-dir :out :string}) |
| 37 | + :out |
| 38 | + str/trim)) |
| 39 | + |
| 40 | +(def branch (current-branch)) |
| 41 | + |
| 42 | +(defn checks-for-branch |
| 43 | + ;; note: this is a ref, so it can e.g. also be a sha. |
| 44 | + [] |
| 45 | + (->> (str "https://api.github.com/repos/metabase/metabase/commits/" branch "/check-runs") |
| 46 | + gh-GET |
| 47 | + :check_runs |
| 48 | + (mapv (comp (fn [x] (if (nil? x) :in-progress (keyword x))) :conclusion)) |
| 49 | + frequencies |
| 50 | + (sort-by first) |
| 51 | + reverse)) |
| 52 | + |
| 53 | +(def pretty {:success "✅" :skipped "⏭️ " :cancelled "⏹️" :in-progress "🔄" :failure "❌"}) |
| 54 | + |
| 55 | +(defn print-report-line [checks] |
| 56 | + (print (str "[" (.format (java.text.SimpleDateFormat. "hh:mm:ss a") (java.util.Date.)) "] ")) |
| 57 | + (doseq [[status count] checks] |
| 58 | + (print (str/join (repeat count (pretty status)))) (print " ")) |
| 59 | + (println) |
| 60 | + (flush)) |
| 61 | + |
| 62 | +(defn -main [] |
| 63 | + ;; (prn (env)) |
| 64 | + (println "Legend:" (pr-str pretty)) |
| 65 | + (loop [n 0] |
| 66 | + (let [checks (checks-for-branch)] |
| 67 | + (when (zero? (mod n 20)) |
| 68 | + (println (c/green "Checking CI for branch:") (c/white branch) (c/green "."))) |
| 69 | + (print-report-line checks) |
| 70 | + (when-not (= (keys checks) [:success]) |
| 71 | + (Thread/sleep 5000) |
| 72 | + (recur (inc n)))))) |
| 73 | + |
| 74 | +(when (= *file* (System/getProperty "babashka.file")) (-main)) |
0 commit comments