Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Make `ci-report` work without GNU `timeout` (#69453)

lbrdnk985082f0590e
diff --git a/bb.edn b/bb.edn
index 0b8b89f025a..5189eb2ce50 100644
--- a/bb.edn
+++ b/bb.edn
@@ -289,11 +289,15 @@
{:doc "Generate a CI failure report for a PR"
:examples [["./bin/mage ci-report" "Check current branch, find its PR, and generate report"]
["./bin/mage ci-report 68292" "Generate report for PR #68292"]
- ["./bin/mage ci-report https://github.com/metabase/metabase/pull/68292" "Generate report from PR URL"]]
+ ["./bin/mage ci-report --detailed 68292" "Generate detailed report with full FAIL/ERROR blocks"]
+ ["./bin/mage ci-report --detailed > ci_report.md"
+ "Save detailed report to markdown file and print progress."]]
+ :options [["-d" "--detailed" "Include full FAIL/ERROR blocks with expected/actual diffs"]]
:usage-fn (fn [_]
(str "Fetches CI check status and failed job logs from GitHub Actions.\n"
"Parses Trunk test reports to extract test failure details.\n\n"
"With no arguments, looks up the PR for your current git branch.\n\n"
+ "Use --detailed to get full test failure output with expected/actual diffs.\n\n"
"Requires GitHub CLI (gh) to be installed and authenticated.\n"
"If you see 'gh: command not found' or auth errors, run: gh auth login"))
:requires [[mage.ci-report :as ci-report]]
diff --git a/mage/src/mage/ci_report.clj b/mage/src/mage/ci_report.clj
index 511ecd1b19a..9147c26675d 100644
--- a/mage/src/mage/ci_report.clj
+++ b/mage/src/mage/ci_report.clj
@@ -31,16 +31,37 @@
[s]
(-> s
(str/replace #"\x1b\[[0-9;]*m" "")
+ (str/replace #"\x1b" "") ;; Catch any stray ESC chars
(str/replace #"\[J" "")
(str/replace #"\[K" "")))
+(defn- resolve-carriage-returns
+ "Simulate carriage return behavior: for each line, keep only content after last \\r.
+ This handles terminal progress animations that use \\r to overwrite the line.
+ Also trims leading whitespace since terminal overwrites pad with spaces."
+ [s]
+ (->> (str/split-lines s)
+ (map (fn [line]
+ (if (str/includes? line "\r")
+ (str/triml (last (str/split line #"\r")))
+ line)))
+ (str/join "\n")))
+
+(def ^:private ^:dynamic *progress-log* nil)
+
(defn- log-progress [msg]
- (binding [*out* *err*]
- (println (str "\u001b[34m→\u001b[0m " msg))))
+ (let [formatted (str "→ " msg)]
+ (when *progress-log*
+ (swap! *progress-log* conj formatted))
+ (binding [*out* *err*]
+ (println (str "\u001b[34m" formatted "\u001b[0m")))))
(defn- log-success [msg]
- (binding [*out* *err*]
- (println (str "\u001b[32m✓\u001b[0m " msg))))
+ (let [formatted (str "✓ " msg)]
+ (when *progress-log*
+ (swap! *progress-log* conj formatted))
+ (binding [*out* *err*]
+ (println (str "\u001b[32m" formatted "\u001b[0m")))))
;;; GitHub API
@@ -100,16 +121,34 @@
(take-last 2000)
(str/join "\n")))))
+(defn- job-logs-raw
+ "Fetch raw logs for a specific job ID with retries"
+ [job-id]
+ (loop [attempt 1
+ delay-ms 1000]
+ (let [result (try
+ (let [proc (p/process {:out :string :err :string}
+ "gh" "api"
+ (format "repos/%s/actions/jobs/%s/logs" repo job-id))
+ result (deref proc 60000 ::timeout)]
+ (when (= ::timeout result)
+ (p/destroy-tree proc))
+ (when (and (not= ::timeout result)
+ (zero? (:exit result)))
+ (:out result)))
+ (catch Exception _ nil))]
+ (if result
+ result
+ ;; Retry up to 3 times with exponential backoff
+ (when (< attempt 3)
+ (Thread/sleep delay-ms)
+ (recur (inc attempt) (* delay-ms 2)))))))
+
(defn- job-logs
- "Fetch logs for a specific job ID"
+ "Fetch logs for a specific job ID, extracting test report section"
[job-id]
- (try
- (let [result (p/shell {:out :string :err :string :continue true}
- "timeout" "60" "gh" "api"
- (format "repos/%s/actions/jobs/%s/logs" repo job-id))]
- (when (zero? (:exit result))
- (extract-test-report-section (:out result))))
- (catch Exception _ nil)))
+ (when-let [raw (job-logs-raw job-id)]
+ (extract-test-report-section raw)))
(defn- extract-job-id
"Extract job ID from a check link URL"
@@ -119,10 +158,24 @@
;;; Log Parsing
+(defn- strip-timestamp
+ "Remove GitHub Actions timestamp prefix from log line.
+ Format: 2026-02-06T02:38:25.1443042Z <content>
+ Strips the timestamp plus one trailing space, preserves remaining indentation."
+ [line]
+ (str/replace line #"^\d{4}-\d{2}-\d{2}T[\d:.]+Z " ""))
+
(defn- parse-trunk-report
"Extract test failure info from Trunk test report section in logs"
[logs]
- (let [clean-logs (strip-ansi logs)
+ (let [clean-logs (-> logs
+ strip-ansi
+ ;; Trunk CLI uses \r for terminal overwrite - resolve to final content
+ resolve-carriage-returns
+ ;; Also strip GitHub Actions timestamps
+ (->> str/split-lines
+ (map strip-timestamp)
+ (str/join "\n")))
lines (str/split-lines clean-logs)
;; Find the test report section - look for 📚 emoji specifically
in-report? (atom false)
@@ -182,6 +235,79 @@
text)))
(str/join "\n"))))
+(defn- extract-failure-blocks
+ "Extract full FAIL/ERROR blocks from logs.
+ Splits on FAIL in|ERROR in|LONG TEST and keeps FAIL/ERROR blocks."
+ [logs]
+ (let [clean-logs (strip-ansi logs)
+ lines (->> (str/split-lines clean-logs)
+ (map strip-timestamp))
+ ;; State: collecting lines for current block
+ current-block (atom [])
+ blocks (atom [])
+ in-block? (atom false)
+ block-type (atom nil)]
+ (doseq [line lines]
+ (cond
+ ;; Start of a FAIL block
+ (re-find #"^FAIL in " line)
+ (do
+ ;; Save previous block if it was FAIL/ERROR
+ (when (and @in-block? (#{:fail :error} @block-type))
+ (swap! blocks conj {:type @block-type :lines @current-block}))
+ (reset! current-block [line])
+ (reset! in-block? true)
+ (reset! block-type :fail))
+
+ ;; Start of an ERROR block
+ (re-find #"^ERROR in " line)
+ (do
+ (when (and @in-block? (#{:fail :error} @block-type))
+ (swap! blocks conj {:type @block-type :lines @current-block}))
+ (reset! current-block [line])
+ (reset! in-block? true)
+ (reset! block-type :error))
+
+ ;; LONG TEST - ends previous block, but we don't collect this one
+ (re-find #"LONG TEST" line)
+ (do
+ (when (and @in-block? (#{:fail :error} @block-type))
+ (swap! blocks conj {:type @block-type :lines @current-block}))
+ (reset! current-block [])
+ (reset! in-block? false)
+ (reset! block-type nil))
+
+ ;; Test summary - ends collection, but include the line (has duration)
+ (re-find #"^Ran \d+ tests in" line)
+ (do
+ (when @in-block?
+ (swap! current-block conj line)
+ (swap! current-block conj "") ;; blank line after
+ (when (#{:fail :error} @block-type)
+ (swap! blocks conj {:type @block-type :lines @current-block})))
+ (reset! current-block [])
+ (reset! in-block? false)
+ (reset! block-type nil))
+
+ ;; Continue collecting if in a block
+ @in-block?
+ (swap! current-block conj line)))
+
+ ;; Don't forget the last block
+ (when (and @in-block? (#{:fail :error} @block-type) (seq @current-block))
+ (swap! blocks conj {:type @block-type :lines @current-block}))
+
+ @blocks))
+
+(defn- format-failure-blocks
+ "Format failure blocks as markdown"
+ [blocks]
+ (when (seq blocks)
+ (->> blocks
+ (map (fn [{:keys [lines]}]
+ (str/join "\n" lines)))
+ (str/join "\n\n---\n\n"))))
+
;;; Report Generation
(defn- categorize-checks
@@ -192,41 +318,62 @@
:passed (filter #(= "SUCCESS" (:state %)) checks)})
(defn- fetch-failed-logs-parallel
- "Fetch logs for failed checks in parallel"
- [failed-checks]
+ "Fetch logs for failed checks in parallel using futures.
+ When detailed? is true, fetches full logs; otherwise just the test report section."
+ [failed-checks {:keys [detailed?]}]
(let [job-ids (->> failed-checks
(map :link)
(map extract-job-id)
(filter some?)
distinct
- (take 15))] ; Limit to 15 jobs
+ vec)
+ fetch-fn (if detailed? job-logs-raw job-logs)]
(when (seq job-ids)
- (log-progress (format " Fetching logs for %d failed job(s) in parallel..." (count job-ids)))
- (->> job-ids
- (pmap (fn [job-id]
- (when-let [logs (job-logs job-id)]
- [job-id logs])))
- (filter some?)
- (into {})))))
+ (log-progress (format " Fetching logs for %d failed job(s) in parallel%s..."
+ (count job-ids)
+ (if detailed? " (with expected/actual)" "")))
+ ;; Launch all fetches as futures
+ (let [futures (mapv (fn [job-id]
+ [job-id (future (fetch-fn job-id))])
+ job-ids)]
+ ;; Collect results, with 60s timeout per job
+ (->> futures
+ (keep (fn [[job-id fut]]
+ (try
+ (when-let [logs (deref fut 60000 nil)]
+ [job-id logs])
+ (catch Exception _ nil))))
+ (into {}))))))
(defn- generate-report
"Generate markdown report"
- [pr-number pr-info checks logs-by-job-id]
+ [pr-number pr-info checks logs-by-job-id {:keys [detailed? progress-log]}]
(let [{:keys [failed pending passed]} (categorize-checks checks)
branch (:headRefName pr-info)
- sha (subs (:headRefOid pr-info) 0 7)
+ sha (:headRefOid pr-info)
title (:title pr-info)
pr-url (:url pr-info)]
- (println (format "# CI Status Report for PR #%s" pr-number))
+ (println (format "# CI Report for PR #%s: %s" pr-number title))
(println)
- (println (format "**Title:** %s" title))
+ (println "## Metadata")
(println)
- (println (format "**Branch:** `%s` | **Commit:** `%s`" branch sha))
- (println)
- (println (format "**PR:** %s" pr-url))
+ (println (format "- **PR:** [#%s](%s)" pr-number pr-url))
+ (println (format "- **Branch:** `%s`" branch))
+ (println (format "- **SHA:** `%s`" sha))
+ (println (format "- **Mode:** %s" (if detailed? "Detailed (with expected/actual)" "Summary")))
(println)
+ ;; Progress log section
+ (when (and progress-log (seq @progress-log))
+ (println "## Loading Data")
+ (println)
+ (println "```")
+ (doseq [msg @progress-log]
+ (println msg))
+ (println "```")
+ (println))
+
;; Summary table
(println "## Summary")
(println)
@@ -271,15 +418,27 @@
(let [job-id (extract-job-id (:link check))
job-logs (get logs-by-job-id job-id)]
(if job-logs
- (let [report (parse-trunk-report job-logs)
- formatted (format-trunk-report report)]
- (if (seq formatted)
- (do (println "**Test Failures:**")
- (println)
- (println "````")
- (println formatted)
- (println "````"))
- (println "_No test failures found in logs._")))
+ (if detailed?
+ ;; Detailed mode: show full FAIL/ERROR blocks
+ (let [blocks (extract-failure-blocks job-logs)
+ formatted (format-failure-blocks blocks)]
+ (if (seq formatted)
+ (do (println "**Test Failures:**")
+ (println)
+ (println "```")
+ (println formatted)
+ (println "```"))
+ (println "_No test failures found in logs._")))
+ ;; Summary mode: show trunk report summary
+ (let [report (parse-trunk-report job-logs)
+ formatted (format-trunk-report report)]
+ (if (seq formatted)
+ (do (println "**Test Failures:**")
+ (println)
+ (println "````")
+ (println formatted)
+ (println "````"))
+ (println "_No test failures found in logs._"))))
(println "_Logs not yet available or run still in progress._")))
(println)))
@@ -318,47 +477,51 @@
;; Otherwise return as-is and let gh handle it
:else arg))
-(defn generate-report! [{:keys [arguments]}]
- (let [pr-number (if (empty? arguments)
- ;; No args - try to get PR for current branch
- (do
- (log-progress "No PR specified, checking current branch...")
- (if-let [pr (get-current-branch-pr)]
- (do (log-success (format "Found PR #%s for current branch" pr))
- pr)
- (do
- (binding [*out* *err*]
- (println "Error: No PR found for current branch")
- (println "Usage: mage ci-report <pr-number or url>"))
- (System/exit 1))))
- ;; Parse the provided argument
- (parse-pr-arg (first arguments)))]
- (log-progress (format "Fetching PR #%s info..." pr-number))
-
- (let [pr-info (pr-info pr-number)]
- (when-not pr-info
- (binding [*out* *err*]
- (println (format "Error: Could not fetch PR #%s" pr-number)))
- (System/exit 1))
-
- (log-success (format "Found PR on branch: %s" (:headRefName pr-info)))
- (log-progress "Checking PR status...")
-
- (let [checks (pr-checks pr-number)
- {:keys [failed pending]} (categorize-checks checks)]
-
- (log-progress (format "Found %d failed, %d pending check(s)"
- (count failed) (count pending)))
-
- ;; Fetch logs for failed checks
- (log-progress "Fetching failed job logs...")
- (let [logs-by-job-id (if (pos? (count failed))
- (fetch-failed-logs-parallel failed)
- {})]
- (when (seq logs-by-job-id)
- (log-success "Retrieved logs"))
-
- (log-progress "Generating report...")
- (println)
- (generate-report pr-number pr-info checks logs-by-job-id)
- (log-success "Done!"))))))
+(defn generate-report! [{:keys [arguments options]}]
+ (binding [*progress-log* (atom [])]
+ (let [detailed? (:detailed options)
+ pr-number (if (empty? arguments)
+ ;; No args - try to get PR for current branch
+ (do
+ (log-progress "No PR specified, checking current branch...")
+ (if-let [pr (get-current-branch-pr)]
+ (do (log-success (format "Found PR #%s for current branch" pr))
+ pr)
+ (do
+ (binding [*out* *err*]
+ (println "Error: No PR found for current branch")
+ (println "Usage: mage ci-report [--detailed] <pr-number or url>"))
+ (System/exit 1))))
+ ;; Parse the provided argument
+ (parse-pr-arg (first arguments)))]
+ (log-progress (format "Fetching PR #%s info..." pr-number))
+
+ (let [pr-info (pr-info pr-number)]
+ (when-not pr-info
+ (binding [*out* *err*]
+ (println (format "Error: Could not fetch PR #%s" pr-number)))
+ (System/exit 1))
+
+ (log-success (format "Found PR on branch: %s" (:headRefName pr-info)))
+ (log-progress "Checking PR status...")
+
+ (let [checks (pr-checks pr-number)
+ {:keys [failed pending]} (categorize-checks checks)]
+
+ (log-progress (format "Found %d failed, %d pending check(s)"
+ (count failed) (count pending)))
+
+ ;; Fetch logs for failed checks
+ (log-progress "Fetching failed job logs...")
+ (let [logs-by-job-id (if (pos? (count failed))
+ (fetch-failed-logs-parallel failed {:detailed? detailed?})
+ {})]
+ (when (seq logs-by-job-id)
+ (log-success "Retrieved logs"))
+
+ (log-progress (str "Generating report" (when detailed? " (detailed mode)") "..."))
+ (println)
+ (generate-report pr-number pr-info checks logs-by-job-id
+ {:detailed? detailed?
+ :progress-log *progress-log*})
+ (log-success "Done!")))))))