Public: anyone can read this; the crew can change it.

The pulls list says green while a pull's tests are still running #17265

opened this in answer to

Merged 39h 31m ago, live in production

torque/forge:torque/17262-the-pulls-list-says-green-while-a-pull-s → main 0 commits fork 1 thread · 1 unresolved

  • Merged. torque/17262-the-pulls-list-says-green-while-a-pull-s was merged into main as 3369f01.

Review

Sign in to review this change.
4 files changed +130 −30
src/core/runs.ts +33 −19
@@ -1322,6 +1322,18 @@ export function checksForHead(
13221322 .map(toRun);
13231323 }
13241324
1325 /**
1326 * `verdictForHead` over runs already read, newest first: the newest of each kind and environment.
1327 */
1328 export function newestOfEachKind(runs: readonly Run[]): Run[] {
1329 const newest = new Map<string, Run>();
1330 for (const run of runs) {
1331 const key = `${run.kind}:${run.env}`;
1332 if (!newest.has(key)) newest.set(key, run);
1333 }
1334 return [...newest.values()];
1335 }
1336
13251337 /**
13261338 * What this head says now: the newest run of each kind (and environment)
13271339 * at it. A re-run on the same commit supersedes the red it was run for, so
@@ -1332,35 +1344,23 @@ export function verdictForHead(
13321344 issueId: string,
13331345 headOid: string,
13341346 ): Run[] {
1335 const newest = new Map<string, Run>();
1336 for (const run of checksForHead(db, issueId, headOid)) {
1337 const key = `${run.kind}:${run.env}`;
1338 if (!newest.has(key)) newest.set(key, run);
1339 }
1340 return [...newest.values()];
1347 return newestOfEachKind(checksForHead(db, issueId, headOid));
13411348 }
13421349
13431350 export interface CheckSummary {
13441351 /**
1345 * `failed` when the newest run of any kind at this head is red (a cut one is not); else the
1346 * newest run's state; `none` when there is none.
1352 * `failed` when the newest run of any kind at this head is red (a cut one is not); else a run
1353 * still to come; else the newest run's state; `none` when there is none.
13471354 */
13481355 readonly state: RunState | "none";
13491356 readonly run: Run | null;
13501357 }
13511358
1352 export function checkSummary(
1353 db: Database,
1354 issueId: string,
1355 headOid: string,
1356 ): CheckSummary {
1359 function summaryOf(verdict: readonly Run[]): CheckSummary {
13571360 // The newest run of each kind speaks, as it does for the merge queue (`verdictForHead`): a
1358 // preview that deployed after the check went red does not make the head green. Only the newest
1359 // row was read, so a red check under a later deploy read "Check passed" on the page, the API and
1360 // MCP while the queue, reading every kind, never admitted it.
1361 const verdict = verdictForHead(db, issueId, headOid);
1362 // A head reads ok only when the newest run of every kind is ok: a preview deploy that finishes
1363 // in seconds must not cover a check still queued or running (#17256).
1361 // preview that deployed after the check went red, or while it is still to come, does not make
1362 // the head green. A head reads ok only when the newest run of every kind is ok: a preview deploy
1363 // that finishes in seconds must not cover a check still queued or running (#17256).
13641364 const latest =
13651365 verdict.find((run) => run.state === "failed" && !isCut(run)) ??
13661366 verdict.find((run) => run.state !== "ok") ??
@@ -1371,6 +1371,20 @@ export function checkSummary(
13711371 : { state: "none", run: null };
13721372 }
13731373
1374 export function checkSummary(
1375 db: Database,
1376 issueId: string,
1377 headOid: string,
1378 ): CheckSummary {
1379 return summaryOf(verdictForHead(db, issueId, headOid));
1380 }
1381
1382 /** `checkSummary` for many pulls at once, so a list's check mark says what the pull's page says. */
1383 export function checkSummaries(db: Database, heads: ReadonlyMap<string, string>): Map<string, CheckSummary> {
1384 const runs = checksForHeads(db, heads);
1385 return new Map([...heads.keys()].map((id) => [id, summaryOf(newestOfEachKind(runs.get(id) ?? []))]));
1386 }
1387
13741388 /**
13751389 * The run a hand-back to a builder may quote as the failure (#12968): the newest check or deploy
13761390 * of this head that failed, and was not cut by the forge. A deploy that succeeded, a check that
src/flow/read.ts +2 −8
@@ -9,7 +9,7 @@ import { getIssuesByIds, LIST_LIMIT_MAX, listIssues, type Issue } from "../core/
99 import { closesFromBody, joinMany, listPulls, type Pull } from "../core/pulls.ts";
1010 import { listReviews, reviewSummariesFor, reviewSummary } from "../core/reviews.ts";
1111 import type { Repo } from "../core/repos.ts";
12 import { checksForHeads, verdictForHead, type Run } from "../core/runs.ts";
12 import { checksForHeads, newestOfEachKind, verdictForHead } from "../core/runs.ts";
1313 import {
1414 flowStage,
1515 type FlowChecks,
@@ -267,7 +267,7 @@ export function openPullStages(db: Database, repo: Repo, scope: Scope, now: Date
267267 openedAt: pull.created_at,
268268 closedAt: null,
269269 mergedAt: null,
270 checks: foldRuns(newestPerKind(runs.get(pull.id) ?? [])),
270 checks: foldRuns(newestOfEachKind(runs.get(pull.id) ?? [])),
271271 approvals: summary?.approvals ?? 0,
272272 changesRequested: summary?.changesRequested ?? 0,
273273 requireReview: repo.requireReview,
@@ -283,10 +283,4 @@ export function openPullStages(db: Database, repo: Repo, scope: Scope, now: Date
283283 }
284284 return out;
285285 }
286 /** `verdictForHead` over runs already read, newest first: the newest of each kind and environment. */
287 function newestPerKind(runs: readonly Run[]): Run[] {
288 const newest = new Map<string, Run>();
289 for (const run of runs) if (!newest.has(`${run.kind}:${run.env}`)) newest.set(`${run.kind}:${run.env}`, run);
290 return [...newest.values()];
291 }
292286 const STAGE_ORDER = ["issue", "pull", "check", "review", "land", "live"] as const;
src/http/api/pulls.ts +7 −3
@@ -83,7 +83,8 @@ import { getAppByRepo } from "../../core/apps.ts";
8383 import { crewRole } from "../../core/agents.ts";
8484 import {
8585 checkSummary,
86 latestRunsForIssues,
86 checkSummaries,
87 type RunState,
8788 } from "../../core/runs.ts";
8889 import { checkStale } from "../../checks/stale.ts";
8990 import { resolveOid } from "../../git/merge.ts";
@@ -185,7 +186,7 @@ export function pullRoutes(router: Router<Handler>): void {
185186 const siblings = variantSiblings(ctx.app.db, pulls);
186187 // The check word and preview address the pull page shows, so a client
187188 // lists "where to try it" without a request per pull.
188 const checks = latestRunsForIssues(ctx.app.db, pulls.map((p) => p.id));
189 const checks = checkSummaries(ctx.app.db, new Map(pulls.map((p) => [p.id, p.headOid])));
189190 // An address only where the pull page's preview card shows one: the
190191 // environments `envsFor` hands this viewer (a private app shows none).
191192 const record = getAppByRepo(ctx.app.db, repo.id);
@@ -193,10 +194,13 @@ export function pullRoutes(router: Router<Handler>): void {
193194 const live = new Set(
194195 record === undefined ? [] : envsFor(ctx.app, ctx.auth.user, record).map((e) => e.env),
195196 );
197 // The summary's state, not the run's own: a cut check reads cancelled there, as on the page.
198 const wordInput = (sum: { state: RunState | "none" } | undefined, head: string) =>
199 sum === undefined || sum.state === "none" ? undefined : { state: sum.state, commitOid: head };
outdated

Medium: checks.get(item.id)?.run hands the raw run to checkWord, so a cut failed check reads red here while the pull page reads cancelled. Build the word from the summary's state so the list and the page agree, and test a cut check.

196200 return withLabelsMany(ctx, pulls).map((item, i) => ({
197201 ...item,
198202 author: item.originAuthor ?? item.authorName,
199 check: checkWord(checks.get(item.id), item.headOid),
203 check: checkWord(wordInput(checks.get(item.id), item.headOid), item.headOid),
200204 preview:
201205 record !== undefined && runtime !== undefined && item.state === "open" && item.mergedAt === null && live.has(previewEnv(item.number))
202206 ? runtime.urlFor(record, previewEnv(item.number))
test/pull-list-check-under-deploy.test.ts added +88
@@ -0,0 +1,88 @@
1 /**
2 * The pull list's check word reads a head as `checkSummary` does: a preview deploy recorded after
3 * the check, finished in seconds with no tests, never makes the list say green for a check still to
4 * come.
5 */
6 import { afterAll, beforeAll, expect, test } from "bun:test";
7 import { mkdtemp, rm } from "node:fs/promises";
8 import { tmpdir } from "node:os";
9 import { join } from "node:path";
10 import { openPull } from "../src/actions/pulls.ts";
11 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
12 import { createApp, type App } from "../src/app.ts";
13 import { getPull } from "../src/core/pulls.ts";
14 import type { Repo } from "../src/core/repos.ts";
15 import { checkSummary, claimRun, createRun, finishRun } from "../src/core/runs.ts";
16 import { createUser, type User } from "../src/core/users.ts";
17 import { forgeHttp } from "../src/http/server.ts";
18
19 let dataDir: string;
20 let app: App;
21 let alice: User;
22 let repo: Repo;
23
24 beforeAll(async () => {
25 dataDir = await mkdtemp(join(tmpdir(), "forge-list-check-"));
26 app = createApp({ dataDir, port: 0, host: "forge.test" });
27 const a = await createUser(app.db, {
28 origin: "seed", trusted: true, username: "alice", email: "[email protected]",
29 password: "a-good-passphrase", adminIfFirst: true,
30 });
31 if (!a.ok) throw new Error(a.error);
32 alice = a.value;
33 const made = await createRepo(app, alice, { name: "lib", visibility: "public" });
34 if (!made.ok) throw new Error(String(made.error));
35 repo = made.value;
36 await commitToBranch(app, alice, repo, { message: "Start", files: [{ path: "a.ts", content: "export const a = 0;\n" }] });
37 await commitToBranch(app, alice, repo, { branch: "feature/x", base: "main", message: "X", files: [{ path: "x.ts", content: "export const x = 1;\n" }] });
38 });
39
40 afterAll(async () => {
41 app.close();
42 await rm(dataDir, { recursive: true, force: true });
43 });
44
45 test("the list says running, then red, then green, as the pull's own verdict does", async () => {
46 const opened = await openPull(app, alice, repo, { title: "X", body: "", baseBranch: "main", headBranch: "feature/x" });
47 if (!opened.ok) throw new Error(String(opened.error));
48 const pull = getPull(app.db, repo.id, opened.value.number)!;
49 const record = (kind: "check" | "deploy") =>
50 createRun(app.db, { repoId: repo.id, kind, env: `pull-${pull.number}`, issueId: pull.id, commitOid: pull.headOid, reason: "pull" });
51 const http = forgeHttp(app);
52 const listed = async () => {
53 const res = await http.handle(new Request("http://forge.test/api/v1/repos/alice/lib/pulls?fields=number,check"));
54 const body = (await res.json()) as { items: { number: number; check: string }[] };
55 return body.items.find((p) => p.number === pull.number)?.check;
56 };
57
58 const check = record("check");
59 const deploy = record("deploy");
60 claimRun(app.db, deploy.id);
61 finishRun(app.db, deploy.id, { state: "ok", summary: "running · no tests declared, none run" });
62 expect(checkSummary(app.db, pull.id, pull.headOid).state).toBe("queued");
63 expect(await listed()).toBe("running");
64
65 claimRun(app.db, check.id);
66 expect(await listed()).toBe("running");
67 finishRun(app.db, check.id, { state: "failed", summary: "1 test failed" });
68 expect(await listed()).toBe("red");
69
70 const again = record("check");
71 claimRun(app.db, again.id);
72 finishRun(app.db, again.id, { state: "ok", summary: "check passed in 1s" });
73 expect(await listed()).toBe("green");
74 });
75
76 test("a cut check reads none on the list, as cancelled does on the pull's page", async () => {
77 await commitToBranch(app, alice, repo, { branch: "feature/y", base: "main", message: "Y", files: [{ path: "y.ts", content: "export const y = 1;\n" }] });
78 const opened = await openPull(app, alice, repo, { title: "Y", body: "", baseBranch: "main", headBranch: "feature/y" });
79 if (!opened.ok) throw new Error(String(opened.error));
80 const pull = getPull(app.db, repo.id, opened.value.number)!;
81 const run = createRun(app.db, { repoId: repo.id, kind: "check", env: `pull-${pull.number}`, issueId: pull.id, commitOid: pull.headOid, reason: "pull" });
82 claimRun(app.db, run.id);
83 finishRun(app.db, run.id, { state: "failed", summary: "interrupted by a restart; queued again" });
84 expect(checkSummary(app.db, pull.id, pull.headOid).state).toBe("cancelled");
85 const res = await forgeHttp(app).handle(new Request("http://forge.test/api/v1/repos/alice/lib/pulls?fields=number,check"));
86 const body = (await res.json()) as { items: { number: number; check: string }[] };
87 expect(body.items.find((p) => p.number === pull.number)?.check).toBe("none");
88 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs