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

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

shipped production is serving it opened this has it

Landed in pull #17265 · run

opened this

Evidence: at 22:33 UTC, GET /api/v1/repos/trevato/forge/pulls listed #17249, #17248, #17252 and #17253 as check: "green". #17249's test check 01m8st3mdj7xp44hkxfzcc5zdj had been running since 22:20.

Cause: the list route in src/http/api/pulls.ts builds its check word from latestRunsForIssues, which is MAX(id) over every check kind for each issue. The preview deploy (kind=deploy, env=pull-N) finishes in seconds and is newer than the test check, so it speaks for the head. The single-pull route reads checkSummary, which #17258 fixes (#17256). The list is a second door, and #17258 doesn't touch it. On main, the list also reads green for a failed check that has a later ok deploy on top of it.

Fix (build it on #17258 once that lands). One batched read using the existing checksForHeads:

src/core/runs.ts

function newestOfEachKind(runs: readonly Run[]): Run[] {
  const newest = new Map<string, Run>();
  for (const run of runs) {
    const key = `${run.kind}:${run.env}`;
    if (!newest.has(key)) newest.set(key, run);
  }
  return [...newest.values()];
}
export function verdictForHead(db: Database, issueId: string, headOid: string): Run[] {
  return newestOfEachKind(checksForHead(db, issueId, headOid));
}
function summaryOf(verdict: readonly Run[]): CheckSummary {
  const latest =
    verdict.find((run) => run.state === "failed" && !isCut(run)) ??
    verdict.find((run) => run.state !== "ok") ??
    verdict[0];
  return latest
    ? { state: latest.state === "failed" && isCut(latest) ? "cancelled" : latest.state, run: latest }
    : { state: "none", run: null };
}
export function checkSummary(db: Database, issueId: string, headOid: string): CheckSummary {
  return summaryOf(verdictForHead(db, issueId, headOid)); // keep the existing comments
}
/** `checkSummary` for many pulls at once, so a list's check mark says what the pull's page says. */
export function checkSummaries(db: Database, heads: ReadonlyMap<string, string>): Map<string, CheckSummary> {
  const runs = checksForHeads(db, heads);
  return new Map([...heads.keys()].map((id) => [id, summaryOf(newestOfEachKind(runs.get(id) ?? []))]));
}

src/http/api/pulls.ts, list route:

import { checkSummaries, checkSummary } from "../../core/runs.ts";
…
const checks = checkSummaries(ctx.app.db, new Map(pulls.map((p) => [p.id, p.headOid])));
…
check: checkWord(checks.get(item.id)?.run ?? undefined, item.headOid),

If latestRunsForIssues has no other callers after this, delete it (unused-exports). A studio list that shows a pass/fail mark from it has the same gap. Move only those.

Test: in the next comment. It fails on main (the list reads "green" at every step). Change no timeouts or limits.

commented

Test for this, test/pull-list-check-under-deploy.test.ts. It uses the same harness as #17258's test and approve-here.test.ts (forgeHttp(app).handle):

/**
 * The pull list's check word reads a head as `checkSummary` does: a preview deploy recorded after
 * the check, finished in seconds with no tests, never makes the list say green for a check still to come.
 */
import { afterAll, beforeAll, expect, test } from "bun:test";
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { openPull } from "../src/actions/pulls.ts";
import { commitToBranch, createRepo } from "../src/actions/repos.ts";
import { createApp, type App } from "../src/app.ts";
import { getPull } from "../src/core/pulls.ts";
import type { Repo } from "../src/core/repos.ts";
import { checkSummary, claimRun, createRun, finishRun } from "../src/core/runs.ts";
import { createUser, type User } from "../src/core/users.ts";
import { forgeHttp } from "../src/http/server.ts";

let dataDir: string;
let app: App;
let alice: User;
let repo: Repo;

beforeAll(async () => {
  dataDir = await mkdtemp(join(tmpdir(), "forge-list-check-"));
  app = createApp({ dataDir, port: 0, host: "forge.test" });
  const a = await createUser(app.db, {
    origin: "seed", trusted: true, username: "alice", email: "[email protected]",
    password: "a-good-passphrase", adminIfFirst: true,
  });
  if (!a.ok) throw new Error(a.error);
  alice = a.value;
  const made = await createRepo(app, alice, { name: "lib", visibility: "public" });
  if (!made.ok) throw new Error(String(made.error));
  repo = made.value;
  await commitToBranch(app, alice, repo, { message: "Start", files: [{ path: "a.ts", content: "export const a = 0;\n" }] });
  await commitToBranch(app, alice, repo, { branch: "feature/x", base: "main", message: "X", files: [{ path: "x.ts", content: "export const x = 1;\n" }] });
});

afterAll(async () => {
  app.close();
  await rm(dataDir, { recursive: true, force: true });
});

test("the list says running, then red, then green, as the pull's own verdict does", async () => {
  const opened = await openPull(app, alice, repo, { title: "X", body: "", baseBranch: "main", headBranch: "feature/x" });
  if (!opened.ok) throw new Error(String(opened.error));
  const pull = getPull(app.db, repo.id, opened.value.number)!;
  const record = (kind: "check" | "deploy") =>
    createRun(app.db, { repoId: repo.id, kind, env: `pull-${pull.number}`, issueId: pull.id, commitOid: pull.headOid, reason: "pull" });
  const http = forgeHttp(app);
  const listed = async () => {
    const res = await http.handle(new Request("http://forge.test/api/v1/repos/alice/lib/pulls?fields=number,check"));
    const body = (await res.json()) as { items: { number: number; check: string }[] };
    return body.items.find((p) => p.number === pull.number)?.check;
  };

  const check = record("check");
  const deploy = record("deploy");
  claimRun(app.db, deploy.id);
  finishRun(app.db, deploy.id, { state: "ok", summary: "running · no tests declared, none run" });
  expect(checkSummary(app.db, pull.id, pull.headOid).state).toBe("queued");
  expect(await listed()).toBe("running");

  claimRun(app.db, check.id);
  expect(await listed()).toBe("running");
  finishRun(app.db, check.id, { state: "failed", summary: "1 test failed" });
  expect(await listed()).toBe("red");

  const again = record("check");
  claimRun(app.db, again.id);
  finishRun(app.db, again.id, { state: "ok", summary: "check passed in 1s" });
  expect(await listed()).toBe("green");
});

If createApp or forgeHttp needs a different close in this file, follow approve-here.test.ts. Open the pull against main once #17258 lands, since both change checkSummary.

Sign in to join this conversation.

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

Keyboard shortcuts

Docs