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
115 changes: 63 additions & 52 deletions bb.edn
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{:min-bb-version "0.9.162"
:paths ["bb"]
:paths ["."]
:deps {escherize/bask {:git/url "https://github.com/escherize/bask.git"
:git/sha "81cc9af3021d7689cfbddf0518a1e828f785f006"}
table/table {:mvn/version "0.5.0"}}
:tasks {:requires [[bask.colors :as c]
:tasks {:requires [[babashka.process :refer [shell]]
[bask.colors :as c]
[bb.cli :as cli]
[bb.tasks :as t]
[clojure.string :as str]]
Expand All @@ -28,64 +29,75 @@
{:id :port :msg "What port should we run it on?" :short "-p" :long "--port PORT" :prompt :number :default 3337}
{:id :socket-repl :msg "What port shall we open a socket repl on?" :short "-s" :long "--socket-repl SOCKETPORT"}))))}

check-branch
{:requires [[bb.dl-and-run :as dl]]
:doc "Check the status of a branch in CI."
:examples [["bb check-branch --branch master" "Check master branch github checks"]]
:task
(do (dl/check-gh-token!)
(let [branch-info (cli/menu! (current-task)
{:id :branch
:msg "What branch should we check?"
:short "-b"
:long "--branch BRANCH"
:choices (delay (t/list-branches (t/mb-env)))
:choices-doc "list of branches"
:prompt :select}
{:id :watch?
:msg "Watch until success?"
:short "-w"
:long "--watch"})]
(if (:watch? branch-info)
(loop []
(let [check (dl/checks-for-branch branch-info)]
(println (str "\n----------[ "
(.format (java.text.SimpleDateFormat. "hh:mm:ss a") (java.util.Date.))
" ]"))
(doseq [[s c] check]
(println (dl/pretty s) s (str "(" c ") ")))
(when-not (= (keys check) [:success])
(Thread/sleep 5000)
(recur))))
(doseq [[s c] (dl/checks-for-branch branch-info)]
(println (dl/pretty s) s (str "(" c ") "))))))}

metabuild
{:doc "Starts metabase locally in dev mode. Set FORCE_MB_DB_CONNECTION_URI to override connection string building"
:requires [[bb.meta :as meta]]
:examples [["FORCE_MB_DB_CONNECTION_URI=mysql://localhost:3308/metabase_test?user=root bb metabuild -d mysql"
"Connect to MYSQL, running against run-mariadb-latest.sh"]]
:task (let [{:keys [app-db user-name password extensions] :as p}
:task (let [_ (println (c/red "Welcome to " (c/on-white (c/blue " MetaBuilder "))))
{:keys [app-db user-name password extensions] :as p}
(cli/menu! (current-task)
{:id :app-db :short "-d" :long "--database-type DB" :title "Which DB should we use for metabase's data?" :choices ["postgres" "h2" "mysql"] :required? true :prompt :select}
{:id :user-name :short "-u" :long "--username USER" :default (t/whoami)}
{:id :password :short "-p" :long "--pw PW" :default "password"}
{:id :extensions :short "-e" :long "--exensions EXT" :default ["dev" "ee" "ee-dev" "drivers" "drivers-dev" "cider/nrepl"] :prompt :multi}
{:id :db-name :short "-n" :long "--name DB_NAME" :default "metabase" :required? true :title "Name of the database to connect to." :prompt :text})]
{:id :app-db
:short "-d"
:long "--database-type DB"
:title "Pick Metabase's app-db"
:choices ["postgres" "h2" "mysql"]
:prompt :select}
{:id :user-name :short "-u" :long "--username USER" :default (t/whoami)}
{:id :password :short "-p" :long "--pw PW" :default "password"}
{:id :extensions :short "-e" :long "--extensions EXT" :default ["dev" "ee" "ee-dev" "drivers" "drivers-dev" "cider/nrepl"] :prompt :multi}
{:id :db-name
:short "-n"
:long "--name DB_NAME"
:default "metabase"
:title "Name of the database to connect to."
:prompt :text})]
;; (prn p)
(meta/build app-db user-name password extensions))}

quick-test {:doc "Quickly run a test against a namespace."
:requires [[bb.quick-test :as qt]]
:task (qt/go!
(:test-namespaces (cli/menu! (current-task)
{:id :test-namespaces
:msg "What namespace(s) to test?"
:long "--ns NS"
:required? true
:prompt :multi
:choices (delay (qt/test-nss))
:choices-doc "a list of clojure test namespaces"})))}
quick-test
{:doc "Quickly run a test against a namespace."
:requires [[bb.quick-test :as qt]]
:task (qt/go!
(:test-namespaces (cli/menu! (current-task)
{:id :test-namespaces
:msg "What namespace(s) to test?"
:long "--ns NS"
:prompt :multi
:choices (delay (qt/test-nss))
:choices-doc "a list of clojure test namespaces"})))}

watch-ci
{:requires [[bb.dl-and-run :as dl]
[clojure.pprint :as pp]]
: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
(do (dl/check-gh-token!)
(let [{:keys [branch]} (cli/menu! (current-task)
{:id :branch
:msg "What branch should we check?"
:short "-b"
:long "--branch BRANCH"
:choices (delay (t/list-branches (t/mb-env)))
: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)))
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))))))}

install-autotab {:doc "Prints shell code to autocomplete tasks using bb.
Note: for fish shell please make sure ~/.config/fish/completions exists."
Expand All @@ -96,7 +108,6 @@
(cli/menu! (current-task)
{:id :shell-type
:long "--shell SHELL"
:required? true
:title "What kind of shell?"
:choices ["zsh" "bash" "fish"]
:prompt :select})]
Expand Down
1 change: 1 addition & 0 deletions bb/build_and_run_jar_locally.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(ns bb.build-and-run-jar-locally)
10 changes: 6 additions & 4 deletions bb/bb/cli.clj → bb/cli.clj
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
(println (c/cyan " accepts no command line arguments.")))
(System/exit 0)))

(defn ->ask [{:keys [id msg prompt choices] :as _option}]
(defn ->ask [{:keys [id title prompt choices] :as _option}]
{:id id
:msg msg
:msg title
:type prompt
:choices (if (delay? choices) @choices choices)})

Expand Down Expand Up @@ -73,8 +73,10 @@
(System/exit 1))
asked-opts (into {} (for [hybrid-option missing-and-askable]
(println "todo: ask (menu-ask hybrid-option)" (pr-str hybrid-option))))
cli (assoc (merge parsed-opts asked-opts) :args arguments)]
(ask-unknown! cli opts)))
cli (assoc (merge parsed-opts asked-opts) :args arguments)
out (ask-unknown! cli opts)]
(println out)
out))

(defn add-parsing-for-multi [option]
(if (= :multi (:prompt option))
Expand Down
41 changes: 25 additions & 16 deletions bb/bb/dl_and_run.clj → bb/dl_and_run.clj
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
(ns bb.dl-and-run
(:require [babashka.tasks :refer [shell]]
[babashka.curl :as curl]
[bask.colors :as c]
[bb.tasks :as t]
[selmer.parser :refer [<<]]
[cheshire.core :as json]
[clojure.edn :as edn]))
(:require
[babashka.curl :as curl]
[babashka.tasks :refer [shell]]
[bask.colors :as c]
[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))
(reduce (fn [_ element] (when-let [resp (f element)] (reduced resp))) nil coll))

(defn- gh-get [url]
(try (-> url
Expand Down Expand Up @@ -79,14 +78,15 @@
(println (c/bold "Be sure to tick the *repo* permission."))
(System/exit 1))))

(defn download-and-run-latest-jar! [{:keys [branch port socket-repl]}]
(defn download-latest-jar! [{:keys [branch]}]
(let [finished (t/wait (str "Finding uberjar for branch" (c/green branch)) "📞")
{artifact-id :id
created-at :created_at
dl-url :archive_download_url
sha :head_sha
:as info} (branch->latest-artifact branch)
branch-dir (str download-dir branch)]
:as info_} (branch->latest-artifact branch)
branch-dir (str download-dir branch)
info (into (sorted-map) (assoc info_ :branch branch :branch-dir branch-dir))]
(finished)
(println (c/cyan "Found latest artifact!"))
(println (c/magenta (str " git SHA: " (c/green sha))))
Expand All @@ -99,6 +99,7 @@
;; - _or_ use the older artifact?
(println (c/magenta (str " Download Url: " (c/green dl-url))))
(prn info)
(println "Download directory: " branch-dir)
(shell (str "mkdir -p " branch-dir))
(if (= (try (edn/read-string (slurp (str branch-dir "/info.edn")))
(catch Throwable _ ::nothing-there))
Expand All @@ -109,6 +110,10 @@
(download-mb-jar! branch-dir dl-url)))
(println "Artifact download complete.")
(spit (str branch-dir "/info.edn") info)
info))

(defn run-jar! [info port socket-repl]
(let [{:keys [branch branch-dir]} info]
(println "Unzipping artifact...")
(try
(shell {:dir branch-dir :out nil} "unzip -o metabase.zip")
Expand All @@ -133,13 +138,17 @@
(t/print-env "mb" env+)
(shell {:dir branch-dir :out :inherit :env env+} cmd))))

(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 "⏹️ "
:cancelled "⏹️"
:in-progress "🔄"
:failure "❌"})

(defn checks-for-branch [{branch :branch}]
(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
Expand Down
10 changes: 6 additions & 4 deletions bb/bb/meta.clj → bb/meta.clj
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@
(println (c/green "[bb metabuild] ✅ Done."))))

(defn build [app-db user-name password extensions]
(let [env+ (assoc (t/env) "MB_DB_CONNECTION_URI"
(let [env+ (assoc (t/env)
"MB_DB_CONNECTION_URI"
(or (t/env "FORCE_MB_DB_CONNECTION_URI" (constantly false))
(case app-db
"postgres" (str "postgres://" user-name ":" password "@localhost:5432/metabase")
"mysql" (str "mysql://" user-name ":" password "@localhost:3306/metabase_test")
"h2" "" )))
"postgres" (str "postgres://" user-name ":" password "@localhost:5432/metabase")
"h2" "" ))
"MB_DB_TYPE" app-db)
cmd (str "clj -M" (str/join (map (fn [s-or-kw] (keyword (name s-or-kw))) extensions)))]
(println (:out (shell {:out :string} "java -version")))
(t/print-env "mb" env+)
(println (c/green "\n ==== Starting Metabase with: ==== \n"))
(println (c/green cmd))
(println (c/green "\n ================================= \n"))
(println (c/magenta "In directory: " (t/env "MB_DIR")))
(println (c/magenta "In directory 📂:" (t/env "MB_DIR")))
(future (listen-for-nrepl-and-init!))
(shell {:extra-env env+ :dir (t/env "MB_DIR")} cmd)))
File renamed without changes.
2 changes: 1 addition & 1 deletion bb/bb/tasks.clj → bb/tasks.clj
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

(defn list-branches [mb-dir]
(git-fetch mb-dir)
(letfn [(remove-origin [b] (str/replace (str/trim b) (re-pattern "^origin/") ""))]
(let [remove-origin (fn remove-origin [b] (str/replace (str/trim b) (re-pattern "^origin/") ""))]
(mapv remove-origin
(str/split-lines
(->> "git branch -r" (shell {:dir mb-dir :out :string}) :out)))))
Expand Down
74 changes: 0 additions & 74 deletions watch_ci.clj

This file was deleted.