Skip to content

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

Add --attempt option to mage ci-report (#74975)

Ben Grabow51df60fa5106
diff --git a/bb.edn b/bb.edn
index 79f4318708f..0648d6fbff9 100644
--- a/bb.edn
+++ b/bb.edn
@@ -325,16 +325,25 @@
["./bin/mage ci-report source-replacement" "Report for a branch by name"]
["./bin/mage ci-report abc123def" "Report for a specific commit SHA"]
["./bin/mage ci-report --detailed 68292" "Detailed report with full FAIL/ERROR blocks"]
+ ["./bin/mage ci-report --attempt 1 68292"
+ "Report for attempt 1 of PR #68292 (use while a rerun attempt is in flight)"]
+ ["./bin/mage ci-report --attempt=-1 68292"
+ "Report for one attempt prior to the latest (0 = latest, -1 = prior, etc.)"]
["./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"]]
+ :options [["-d" "--detailed" "Include full FAIL/ERROR blocks with expected/actual diffs"]
+ ["-a" "--attempt N" "Workflow run attempt number to fetch (default: latest). Negative N indexes back from latest (0 = latest, -1 = one prior). Useful when a rerun is in progress and you want results from an earlier attempt."
+ :parse-fn Integer/parseInt]]
: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"
"Accepts a PR number/URL, branch name, or commit SHA.\n"
"With no arguments, uses the current branch's PR if one exists,\n"
"otherwise falls back to the current branch's latest CI run.\n\n"
- "Use --detailed to get full test failure output with expected/actual diffs.\n\n"
+ "Use --detailed to get full test failure output with expected/actual diffs.\n"
+ "Use --attempt N to fetch a specific workflow run attempt (e.g. attempt 1\n"
+ "while attempt 2 is rerunning). N may be 0 (latest) or negative\n"
+ "(e.g. -1 = one attempt prior to the latest).\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 0a84d0bd5c4..0b62d66b70a 100644
--- a/mage/src/mage/ci_report.clj
+++ b/mage/src/mage/ci_report.clj
@@ -297,11 +297,9 @@
;; 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
@@ -353,11 +351,10 @@
(defn- generate-report
"Generate markdown report.
pr-number and pr-info may be nil for branch/SHA reports."
- [pr-number pr-info checks logs-by-job-id {:keys [detailed? progress-log branch sha]}]
+ [pr-number pr-info checks logs-by-job-id {:keys [detailed? progress-log branch sha attempt]}]
(let [{:keys [failed pending passed]} (categorize-checks checks)
branch (or (:headRefName pr-info) branch)
sha (or (:headRefOid pr-info) sha)]
-
(if pr-number
(println (format "# CI Report for PR #%s: %s" pr-number (:title pr-info)))
(println (format "# CI Report for %s" (or branch sha))))
@@ -371,8 +368,9 @@
(when sha
(println (format "- **SHA:** `%s`" sha)))
(println (format "- **Mode:** %s" (if detailed? "Detailed (with expected/actual)" "Summary")))
+ (when attempt
+ (println (format "- **Attempt:** `%d`" attempt)))
(println)
-
;; Progress log section
(when (and progress-log (seq @progress-log))
(println "## Loading Data")
@@ -382,7 +380,6 @@
(println msg))
(println "```")
(println))
-
;; Summary table
(println "## Summary")
(println)
@@ -393,7 +390,6 @@
(println (format "| ⏳ Pending | %d |" (count pending)))
(println (format "| **Total** | **%d** |" (count checks)))
(println)
-
;; Status message
(cond
(zero? (count checks))
@@ -412,10 +408,8 @@
(do (println (format "⚠️ **CI has failures and is still running** (%d failed, %d pending)"
(count failed) (count pending)))
(println)))
-
(println "---")
(println)
-
;; Failed checks detail
(when (pos? (count failed))
(println "## ❌ Failed Checks")
@@ -426,7 +420,6 @@
(when-let [link (:link check)]
(println (format "🔗 %s" link))
(println))
-
;; Get logs for this check
(let [job-id (extract-job-id (:link check))
job-logs (get logs-by-job-id job-id)]
@@ -454,7 +447,6 @@
(println "_No test failures found in logs._"))))
(println "_Logs not yet available or run still in progress._")))
(println)))
-
;; Pending checks
(when (pos? (count pending))
(println "## ⏳ Pending Checks")
@@ -462,7 +454,6 @@
(doseq [check (sort-by :name pending)]
(println (format "- %s" (:name check))))
(println))
-
(println "---")
(println)
(println (format "_Generated by `mage ci-report` at %s UTC_"
@@ -532,6 +523,81 @@
:link (:html_url run)})
accumulated))))))
+(defn- workflow-runs-for-sha
+ "Fetch all workflow runs for a commit SHA. Each result includes :id and
+ :run_attempt (the latest attempt number for that run)."
+ [sha]
+ (loop [page 1
+ all []]
+ (if-let [result (gh-api (format "repos/%s/actions/runs?head_sha=%s&per_page=100&page=%d"
+ repo sha page))]
+ (let [runs (:workflow_runs result)
+ accumulated (into all runs)]
+ (if (and (seq runs) (< (count accumulated) (:total_count result)))
+ (recur (inc page) accumulated)
+ accumulated))
+ all)))
+
+(defn- jobs-for-attempt
+ "Fetch jobs belonging to a specific attempt of a workflow run."
+ [run-id attempt-number]
+ (loop [page 1
+ all []]
+ (if-let [result (gh-api (format "repos/%s/actions/runs/%s/attempts/%d/jobs?per_page=100&page=%d"
+ repo run-id attempt-number page))]
+ (let [jobs (:jobs result)
+ accumulated (into all jobs)]
+ (if (and (seq jobs) (< (count accumulated) (:total_count result)))
+ (recur (inc page) accumulated)
+ accumulated))
+ all)))
+
+(defn- job->check
+ "Convert a GitHub Actions job into the {:name :state :link} check shape."
+ [job]
+ {:name (:name job)
+ :state (normalize-check-state {:status (:status job)
+ :conclusion (:conclusion job)})
+ :link (:html_url job)})
+
+(defn- resolve-attempt
+ "Resolve a possibly-negative attempt index against max-attempt.
+ Positive N => N. 0 => max-attempt. Negative N => max-attempt + N
+ (so -1 means one attempt prior to the latest)."
+ [attempt-number max-attempt]
+ (if (pos? attempt-number)
+ attempt-number
+ (+ max-attempt attempt-number)))
+
+(defn- attempt-checks
+ "Fetch check-like data for a specific attempt across all workflow runs for a
+ commit. Skips workflow runs whose total attempt count is less than the
+ resolved attempt number. Returns {:checks vec :resolved-attempt N :max-attempt N}.
+
+ `attempt-number` may be negative or zero to index relative to the latest
+ attempt: 0 = latest, -1 = one prior to latest, etc."
+ [sha attempt-number]
+ (let [runs (workflow-runs-for-sha sha)
+ max-attempt (some->> runs (map :run_attempt) (apply max))]
+ (when (zero? (count runs))
+ (u/exit (format "Error: No workflow runs found for SHA %s" sha) 1))
+ (let [resolved (resolve-attempt attempt-number max-attempt)]
+ (when (< resolved 1)
+ (u/exit (format "Error: resolved attempt %d is < 1 (requested %d, max attempt %d)"
+ resolved attempt-number max-attempt) 1))
+ (when (> resolved max-attempt)
+ (u/exit (format "Error: resolved attempt %d exceeds max attempt %d (requested %d)"
+ resolved max-attempt attempt-number) 1))
+ (when (not= resolved attempt-number)
+ (log-progress (format "Resolved attempt %d to absolute attempt %d (max: %d)"
+ attempt-number resolved max-attempt)))
+ {:resolved-attempt resolved
+ :max-attempt max-attempt
+ :checks (->> runs
+ (filter (fn [run] (<= resolved (:run_attempt run))))
+ (mapcat (fn [run] (jobs-for-attempt (:id run) resolved)))
+ (mapv job->check))})))
+
(defn- resolve-branch-head
"Resolve a branch name to its latest CI commit SHA.
Uses the GitHub Actions runs API to find the most recent workflow run."
@@ -541,17 +607,25 @@
(get-in result [:workflow_runs 0 :head_sha])))
(defn- run-pr-report!
- "Generate report for a PR number. The original/primary code path."
- [pr-number detailed?]
+ "Generate report for a PR number. The original/primary code path.
+ When `attempt` is provided, fetches jobs for that specific workflow run attempt
+ instead of the latest check results."
+ [pr-number {:keys [detailed? attempt]}]
(log-progress (format "Fetching PR #%s info..." pr-number))
(let [info (pr-info pr-number)]
(when-not info
(binding [*out* *err*]
(println (format "Error: Could not fetch PR #%s" pr-number)))
- (System/exit 1))
+ (u/exit 1))
(log-success (format "Found PR on branch: %s" (:headRefName info)))
- (log-progress "Checking PR status...")
- (let [checks (pr-checks pr-number)]
+ (log-progress (if attempt
+ (format "Fetching attempt %d jobs for SHA %s..."
+ attempt (subs (:headRefOid info) 0 12))
+ "Checking PR status..."))
+ (let [{:keys [checks resolved-attempt]}
+ (if attempt
+ (attempt-checks (:headRefOid info) attempt)
+ {:checks (pr-checks pr-number)})]
(when-not checks
(u/exit (format "Error: Could not fetch checks for PR #%s (API error)" pr-number) 1))
(let [{:keys [failed pending]} (categorize-checks checks)]
@@ -566,19 +640,27 @@
(log-progress (str "Generating report" (when detailed? " (detailed mode)") "..."))
(println)
(generate-report pr-number info checks logs-by-job-id
- {:detailed? detailed?
- :progress-log *progress-log*})
+ {:detailed? detailed?
+ :progress-log *progress-log*
+ :attempt resolved-attempt})
(log-success "Done!"))))))
(defn- run-commit-report!
- "Generate report for a commit SHA (with optional branch name for display)."
- [sha branch detailed?]
- (log-progress (format "Fetching check runs for %s..." (subs sha 0 (min 12 (count sha)))))
- (let [checks (commit-check-runs sha)]
+ "Generate report for a commit SHA (with optional branch name for display).
+ When `attempt` is provided, fetches jobs for that specific workflow run attempt."
+ [sha branch {:keys [detailed? attempt]}]
+ (log-progress (if attempt
+ (format "Fetching attempt %d jobs for %s..."
+ attempt (subs sha 0 (min 12 (count sha))))
+ (format "Fetching check runs for %s..." (subs sha 0 (min 12 (count sha))))))
+ (let [{:keys [checks resolved-attempt]}
+ (if attempt
+ (attempt-checks sha attempt)
+ {:checks (commit-check-runs sha)})]
(when-not checks
(binding [*out* *err*]
(println (format "Error: Could not fetch checks for %s" sha)))
- (System/exit 1))
+ (u/exit 1))
(let [{:keys [failed pending]} (categorize-checks checks)]
(log-progress (format "Found %d failed, %d pending check(s)"
(count failed) (count pending)))
@@ -591,15 +673,18 @@
(log-progress (str "Generating report" (when detailed? " (detailed mode)") "..."))
(println)
(generate-report nil nil checks logs-by-job-id
- {:detailed? detailed?
+ {:detailed? detailed?
:progress-log *progress-log*
- :branch branch
- :sha sha})
+ :branch branch
+ :sha sha
+ :attempt resolved-attempt})
(log-success "Done!")))))
(defn generate-report! [{:keys [arguments options]}]
(binding [*progress-log* (atom [])]
(let [detailed? (:detailed options)
+ attempt (:attempt options)
+ opts {:detailed? detailed? :attempt attempt}
{:keys [type value]} (if (empty? arguments)
;; No args: try PR first, fall back to branch
(do
@@ -615,19 +700,19 @@
(do
(binding [*out* *err*]
(println "Error: No PR found and could not determine current branch")
- (println "Usage: mage ci-report [--detailed] <pr-number|branch|sha>"))
- (System/exit 1))))))
+ (println "Usage: mage ci-report [--detailed] [--attempt N] <pr-number|branch|sha>"))
+ (u/exit 1))))))
;; Explicit argument
(classify-arg (first arguments)))]
(case type
- :pr (run-pr-report! value detailed?)
+ :pr (run-pr-report! value opts)
:branch (do
(log-progress (format "Resolving branch '%s' to latest CI commit..." value))
(let [sha (resolve-branch-head value)]
(if sha
(do (log-success (format "Resolved to SHA: %s" (subs sha 0 (min 12 (count sha)))))
- (run-commit-report! sha value detailed?))
+ (run-commit-report! sha value opts))
(do (binding [*out* *err*]
(println (format "Error: No CI runs found for branch '%s'" value)))
- (System/exit 1)))))
- :sha (run-commit-report! value nil detailed?)))))
+ (u/exit 1)))))
+ :sha (run-commit-report! value nil opts)))))