From b3e5ac4200876990f08fe512c927df4ee4c01210 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Sirois Date: Mon, 20 Apr 2026 19:58:30 +0400 Subject: [PATCH] fix: distinguish missing baseline from Site API fetch error fetchPreviousRun now returns a discriminated PreviousRunResult so callers can tell a genuine 404 ("no baseline on branch") apart from a 5xx/timeout/network failure. main.ts emits the existing "no baseline" guidance only on not-found, and a distinct transient-error warning (with the reason) when the fetch itself fails. Co-Authored-By: Claude Opus 4.7 (1M context) --- src/main.ts | 12 ++++++++++-- src/reporters/site-api.ts | 15 ++++++++++----- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/main.ts b/src/main.ts index 176acc9..6cb51af 100644 --- a/src/main.ts +++ b/src/main.ts @@ -74,20 +74,28 @@ async function runInCI( if (siteApiEndpoint && repo) { const comparisonBranch = config.comparisonBranch ?? process.env.GITHUB_BASE_REF ?? branch; - previousRun = await fetchPreviousRun( + const result = await fetchPreviousRun( siteApiEndpoint, repo, comparisonBranch, runId ?? undefined, ); reportContext.comparisonBranch = comparisonBranch; - if (!previousRun) { + if (result.kind === "found") { + previousRun = result.run; + } else if (result.kind === "not-found") { log.info( "main", `No baseline found on branch "${comparisonBranch}". Comparison will be skipped. ` + `To establish a baseline, run the analyzer on pushes to "${comparisonBranch}" ` + `(add "push: branches: [${comparisonBranch}]" to your workflow trigger).`, ); + } else { + log.warn( + "main", + `Failed to fetch baseline for branch "${comparisonBranch}" (${result.reason}). ` + + `Comparison will be skipped. This is likely a transient Site API issue — re-run the check to retry.`, + ); } } if (previousRun) { diff --git a/src/reporters/site-api.ts b/src/reporters/site-api.ts index 94f404f..5ddfcaa 100644 --- a/src/reporters/site-api.ts +++ b/src/reporters/site-api.ts @@ -276,12 +276,17 @@ export async function postToSiteApi( } } +export type PreviousRunResult = + | { kind: "found"; run: PreviousRun } + | { kind: "not-found" } + | { kind: "error"; reason: string }; + export async function fetchPreviousRun( endpoint: string, repo: string, branch?: string, excludeId?: string, -): Promise { +): Promise { const params = new URLSearchParams({ repo }); if (branch) params.set("branch", branch); if (excludeId) params.set("excludeId", excludeId); @@ -294,15 +299,15 @@ export async function fetchPreviousRun( }); if (response.status === 404) { console.log("No previous run found"); - return null; + return { kind: "not-found" }; } if (!response.ok) { console.warn(`Failed to fetch previous run: ${response.status}`); - return null; + return { kind: "error", reason: `HTTP ${response.status}` }; } - return (await response.json()) as PreviousRun; + return { kind: "found", run: (await response.json()) as PreviousRun }; } catch (err) { console.warn(`Failed to fetch previous run: ${err}`); - return null; + return { kind: "error", reason: `${err}` }; } }