From 21f1f4885fb4058c000361f70aa7e05518be3719 Mon Sep 17 00:00:00 2001 From: Bryan Maass Date: Tue, 12 Mar 2024 16:02:14 -0600 Subject: [PATCH] remove some scripts that no longer work --- README.md | 9 +-- bb.edn | 55 +++++------------- bb/dl_and_run.clj | 142 ---------------------------------------------- bb/watch_ci.clj | 44 -------------- 4 files changed, 16 insertions(+), 234 deletions(-) delete mode 100644 bb/dl_and_run.clj delete mode 100644 bb/watch_ci.clj diff --git a/README.md b/README.md index a15d0de..2b0e38c 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,6 @@ Scripts that make local Metabase development handier. Currently these only consi databases we support like MySQL or Spark SQL locally, but we can add scripts for other stuff in the future if we think of anything good. -Please feel free to collaborate and improve these scripts or add new ones! - ### bb tasks To get setup you'll need: @@ -15,14 +13,9 @@ To see a list of avaliable tasks, run: bb tasks -#### setup for `run-branch` - -You'll need two environment variables set to use `bb run-branch`. +#### Setup MB_DIR MB_DIR=/path/to/metabase - GH_TOKEN=ghp_asdasdasdasdasdasdasdasdasd - -`GH_TOKEN` needs to be a classic can be obtained from: [https://github.com/settings/tokens](https://github.com/settings/tokens). Be sure to tick the *repo* permission. #### How to get help diff --git a/bb.edn b/bb.edn index 2703bdd..dfa6b4d 100644 --- a/bb.edn +++ b/bb.edn @@ -9,26 +9,6 @@ [bb.tasks :as t] [clojure.string :as str]] - run-branch - {:requires [[bb.dl-and-run :as dl]] - :doc "Download and run a jar for a branch, on a port. Respects MB_DB_CONNECTION_URI." - :examples [["bb run-branch --branch master --port 4445" "Run master branch on port 4445"] - ["bb run-branch" "Pick branch and port, and run branch on port"] - ["bb run-branch -p 9939 -s 9938" "Pick a branch, and run it on port 4445 with a socket repl on 9938"]] - :task - (do (dl/check-gh-token!) - (dl/download-and-run-latest-jar! - (let [list-branches (delay ;; choices can be a delay -- we dont want to grab theese unless we need them. - (t/list-branches - (t/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")) - (System/exit 1)))))] - (cli/menu! (current-task) - {:id :branch :msg "What branch should we run?" :short "-b" :long "--branch BRANCH" :choices list-branches :choices-doc "list of branches" :prompt :select} - {: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"}))))} - metabuild {:doc "Starts metabase locally in dev mode. Set FORCE_MB_DB_CONNECTION_URI to override connection string building" :requires [[bb.meta :as meta]] @@ -68,26 +48,21 @@ :choices-doc "a list of clojure test namespaces"})))} watch-ci - {:requires [[bb.dl-and-run :as dl] - [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 - (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-dir))) - :choices-doc "list of branches" - :prompt :select}) - branch (if (= "current" branch) - (let [current (t/current-branch)] - (do (println (c/green (c/bold "Using current branch: " (t/current-branch)))) - current)) - branch)] - (watch-ci/branch branch)))} + {:doc "Prints the command to watch your CI build for a specific branch." + :task (println + (str/join "\n" ["To watch your branch, cd to metabase, switch to your branch, and run:" + "" + "gh pr checks --watch --required && say done" + ""]))} + + github-bookmarklet + {:doc "Print info about Tim's Amazing Github Bookmarklet." + :task (println + (str/join "\n" + ["Save this as a bookmarklet, and click it on a github page to remove passing tests:" + "" + "javascript:%20document.querySelectorAll(%22.merge-status-item%20.octicon-check,.octicon-skip%22).forEach((el)%20=%3E%20el.parentElement.parentElement.remove())" + ""]))} install-autotab {:doc "Prints shell code to autocomplete tasks using bb. Note: for fish shell please make sure ~/.config/fish/completions exists." diff --git a/bb/dl_and_run.clj b/bb/dl_and_run.clj deleted file mode 100644 index fce5e92..0000000 --- a/bb/dl_and_run.clj +++ /dev/null @@ -1,142 +0,0 @@ -(ns bb.dl-and-run - (: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] - [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] - (try (-> url - (curl/get {:headers {"Accept" "application/vnd.github+json" - "Authorization" (str "Bearer " (t/env "GH_TOKEN"))}}) - :body - (json/decode true)) - (catch Exception e - (let [{:keys [status]} (ex-data e)] - (when (= status 401) (println (c/red "Is your GH_TOKEN out of date?"))) - (throw (ex-info (str "Error trying to get url " url " status: " status) - {:status status :url url})))))) - -(defn is-artifact-url-uberjar? [ee-or-oss] - {:pre [#{"ee" "oss"} ee-or-oss]} - (fn [url] - (let [artifact (gh-get url) - name->dl-url (->> artifact :artifacts (group-by :name) (into {}))] - ;; "metabase-oss-uberjar" - (first (get name->dl-url (str "metabase-" ee-or-oss "-uberjar")))))) - -(defn- no-artifact-found-error! [branch] - (println "\nCould not find an uberjar for branch" (c/red branch)) - (println "Our Github Actions retention period is currently 3 months.") - (println "If you are looking to run an older branch, that can be why it is not found.") - (println "Pushing an empty commit to the branch will rebuild it on Github Actions, which should take a few minutes.") - (println "More info: https://docs.github.com/en/actions/managing-workflow-runs/removing-workflow-artifacts#setting-the-retention-period-for-an-artifact") - (println "Could also happen if there are a lot of commits in quick succession") - (System/exit 1)) - -(defn branch->latest-artifact [branch] - (let [artifact-urls (->> branch - ;; TODO is 100 this enough? - (str "https://api.github.com/repos/metabase/metabase/actions/runs?per_page=100&branch=") - gh-get - :workflow_runs - (mapv :artifacts_url))] - - (or (keep-first (is-artifact-url-uberjar? "ee") artifact-urls) - (no-artifact-found-error! branch)))) - -(defn download-mb-jar! - [dl-path dl-url] - (println (c/green (str "downloading into " dl-path "/metabase.zip from: " dl-url))) - (shell {:dir dl-path} (str "curl" - " -H \"Accept:application/vnd.github+json\"" - " -H \"Authorization:Bearer " (t/env "GH_TOKEN") "\"" - " -Lo metabase.zip" - " " dl-url))) - -(def download-dir - ;; artifact zips will be downloaded into download-dir// - (or (t/env "LOCAL_MB_DL" (fn [])) "../")) - -(defn check-gh-token! [] - (t/env "GH_TOKEN" - (fn [] - (println "Please set GH_TOKEN.") - (println (c/white "This API is available for authenticated users, OAuth Apps, and GitHub Apps.")) - (println (c/white "Access tokens require") (c/cyan "repo scope") (c/white "for private repositories and") (c/cyan "public_repo scope") (c/white "for public repositories.")) - (println "More info at: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token") - (println "You can make one (classic) here: https://github.com/settings/tokens") - (println (c/bold "Be sure to tick the *repo* permission.")) - (System/exit 1)))) - -(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) - 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)))) - (println (c/magenta (str " Artifact Id: " (c/green artifact-id)))) - (println (c/magenta (str " Created At: " (c/green created-at)))) - ;; TODO - ;; We can check that the sha matches - ;; I couldn't find the latest. - ;; - you need to manually build it, or try again later. sorry. - ;; - _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)) - info) - (println (c/yellow "Already downloaded artifact created at " created-at)) - (do - (println (c/cyan "New version found, downloading...")) - (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") - (catch Exception _ (throw (ex-info - "Problem unzipping... has the artifact expired?" - (merge {:zip-location (str branch-dir "/metabase.zip") - :zip-length (count (slurp (str branch-dir "/metabase.zip"))) - :branch branch} - (when (< 10000 (count (slurp (str branch-dir "/metabase.zip")))) - {:zip-contents (slurp (str branch-dir "/metabase.zip"))})))))) - (println "Artifact unzipped!") - (shell {:dir branch-dir :out nil} (str "mv target/uberjar/metabase.jar ./metabase_" branch ".jar")) - (println (<< "starting branch {{branch}} of metabase on port:{{port}}...")) - (future (do (while (not= 200 (:status (curl/get (str "localhost:" port) {:throw false}))) - (Thread/sleep 1000)) - (t/open-url (str "http://localhost:" port)))) - (let [cmd (str "java " - (when socket-repl (str "-Dclojure.server.repl=\"{:port " socket-repl " :accept clojure.core.server/repl}\" ")) - "-jar " "metabase_" branch ".jar") - env+ (assoc (t/env) "MB_JETTY_PORT" port)] - (println (c/white "Running: ") (c/green cmd)) - (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))) diff --git a/bb/watch_ci.clj b/bb/watch_ci.clj deleted file mode 100644 index 5d9175f..0000000 --- a/bb/watch_ci.clj +++ /dev/null @@ -1,44 +0,0 @@ -(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))))))