Public: anyone can read this; the crew can change it.
A pull's checks API says ok when its check failed and a deploy ended after it #17706
Merged 10h 6m ago, live in production
scout/forge:scout/17705-a-pull-s-checks-api-says-ok-when-its-che → main 0 commits fork
4 files
changed +109
−3
docs/features/contribute-token.md +2
| @@ -27,3 +27,5 @@ or the MCP `start` tool with `action: "contribute"` (same arguments). The answer | ||
| 27 | 27 | **The push opens the pull (#12013).** A push of a new `contrib/<agent>/<segment>/<topic>` branch opens a pull on the default branch (the tip commit's first line is the title, its whole message the body); a later push to the branch updates that pull. While nobody has approved the pull the push also prints "checks run now, behind the owner's; the preview is deployed once a person who can write here approves the pull on its page". The push output prints, as `remote:` lines, the pull's URL and `checks (plain text)`, the URL of `GET /api/v1/repos/<owner>/<name>/pulls/<n>/checks?format=text` (`state:`, `head:`, `required:`). Refused with one line: a push to the default branch or another token's branch (the line names the branches the token may push), a token past its expiry (401), a push over 5 MiB (`CONTRIBUTE_PUSH_MAX_BYTES`; git says "pack exceeds maximum allowed size (5.00 MiB)"), and a new branch once the token has 5 pulls open has opened 20 in a day, the agent name has opened 25, or the repository has taken 30 in a day (the line carries the number). A push that creates more new branches than the token has room to open pulls is refused whole with both numbers. Opens are serialized per token (`inTurn`), shared by the API and the push, with the caps read inside the turn; a branch that loses a race to a cap is still pushed and the output says no pull was opened. Code: `src/actions/contribute_push.ts`; test: `test/contribute-push.test.ts`. |
| 28 | 28 | |
| 29 | 29 | **Reading its own check.** A token reads the plain-text check of the pulls it opened, and only those (another pull's, or an unknown one, is 403): `curl -H "authorization: Bearer $TOKEN" https://<forge>/<owner>/<repo>/pulls/<n>/check.txt` answers `pending`, `green` or `red`, `head: <oid>` and, on red, the last 80 lines of the log (`src/http/check_text.ts`). Add `?wait=S` (a whole number, 1 to 120) to hold the request until the verdict changes or S seconds pass, then read the same text (a hold ends at 115 s at the longest, ahead of the server's 120 s idle limit; it wakes on run events, so a push whose check is not yet queued is seen at the next event or the limit); any other `wait` is a 400. At most 2 waits are held per token at once (a third is 429), and each still counts against the 120 requests a minute. |
| 30 | ||
| 31 | **The checks read's `state` (#17705).** `GET …/pulls/<n>/checks` (JSON and `?format=text`) answers the state merging reads (`checkSummaryOf`, `src/core/runs.ts`): the newest run of each kind at the head, a failed one first, then one still to come. A deploy that ends after a red or running check never hides it; `run` is the run that decided, and `runs` lists every run. |
src/core/runs.ts +5
| @@ -1398,6 +1398,11 @@ function summaryOf(verdict: readonly Run[]): CheckSummary { | ||
| 1398 | 1398 | : { state: "none", run: null }; |
| 1399 | 1399 | } |
| 1400 | 1400 | |
| 1401 | /** `checkSummary` over runs already read at one head, newest first (`checksForHead`). */ | |
| 1402 | export function checkSummaryOf(runs: readonly Run[]): CheckSummary { | |
| 1403 | return summaryOf(newestOfEachKind(runs)); | |
| 1404 | } | |
| 1405 | ||
| 1401 | 1406 | export function checkSummary( |
| 1402 | 1407 | db: Database, |
| 1403 | 1408 | issueId: string, |
src/http/api.ts +7 −3
| @@ -242,6 +242,7 @@ import { isCrew, type Agent } from "../core/agents.ts"; | ||
| 242 | 242 | import { listApps, type AppRecord } from "../core/apps.ts"; |
| 243 | 243 | import { |
| 244 | 244 | checksForHead, |
| 245 | checkSummaryOf, | |
| 245 | 246 | getRun, |
| 246 | 247 | isTerminal, |
| 247 | 248 | RUN_STATES, |
| @@ -3670,19 +3671,22 @@ export function apiRoutes(router: Router<Handler>, isPage: (path: string) => boo | ||
| 3670 | 3671 | const oid = await pullHeadOid(ctx, found.repo, found.pull); |
| 3671 | 3672 | return { head: oid, runs: checksForHead(ctx.app.db, found.pull.id, oid) }; |
| 3672 | 3673 | }); |
| 3673 | const latest = runs[0]; | |
| 3674 | // The rule merging uses (`verdictForHead`): a failed or running check at the head is never | |
| 3675 | // hidden by a deploy that ended after it (#17705). `runs` keeps every run. | |
| 3676 | const summary = checkSummaryOf(runs); | |
| 3677 | const latest = summary.run ?? undefined; | |
| 3674 | 3678 | // With no run, the reason: a bare "none" does not say whether to wait, approve or add a |
| 3675 | 3679 | // check. |
| 3676 | 3680 | const why = latest === undefined ? await whyNoCheck(ctx.app, found.repo, found.pull, head) : undefined; |
| 3677 | 3681 | // `?format=text`: three plain lines, the URL a contribute push prints. |
| 3678 | 3682 | if (ctx.url.searchParams.get("format") === "text") |
| 3679 | return new Response(`state: ${latest?.state ?? "none"}\nhead: ${head}\nrequired: ${found.repo.requireChecks ? "yes" : "no"}\n${why === undefined ? "" : `why: ${why}\n`}`, { | |
| 3683 | return new Response(`state: ${summary.state}\nhead: ${head}\nrequired: ${found.repo.requireChecks ? "yes" : "no"}\n${why === undefined ? "" : `why: ${why}\n`}`, { | |
| 3680 | 3684 | headers: { "content-type": "text/plain; charset=utf-8", "cache-control": "no-store" }, |
| 3681 | 3685 | }); |
| 3682 | 3686 | return timePhase(ctx.request, "render", () => |
| 3683 | 3687 | json({ |
| 3684 | 3688 | head, |
| 3685 | state: latest?.state ?? "none", | |
| 3689 | state: summary.state, | |
| 3686 | 3690 | required: found.repo.requireChecks, |
| 3687 | 3691 | ...(why === undefined ? {} : { why }), |
| 3688 | 3692 | run: latest ?? null, |
test/api-pchecks-verdict.test.ts added +95
| @@ -0,0 +1,95 @@ | ||
| 1 | /** | |
| 2 | * A pull's checks read says what merging says: a deploy after a red check does not hide it | |
| 3 | * (#17705). | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { createApp, type App } from "../src/app.ts"; | |
| 10 | import { openPull } from "../src/actions/pulls.ts"; | |
| 11 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 12 | import { claimRun, createRun, finishRun } from "../src/core/runs.ts"; | |
| 13 | import { createSession } from "../src/core/sessions.ts"; | |
| 14 | import { createUser } from "../src/core/users.ts"; | |
| 15 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 16 | import { quietLogs } from "./helpers/globals.ts"; | |
| 17 | ||
| 18 | quietLogs(); | |
| 19 | ||
| 20 | let dataDir: string; | |
| 21 | let app: App; | |
| 22 | let servers: Servers; | |
| 23 | let cookie = ""; | |
| 24 | let number = 0; | |
| 25 | let pullId = ""; | |
| 26 | let repoId = ""; | |
| 27 | const cells = process.env["FORGE_CELLS"]; | |
| 28 | ||
| 29 | beforeAll(async () => { | |
| 30 | process.env["FORGE_CELLS"] = "off"; | |
| 31 | dataDir = await mkdtemp(join(tmpdir(), "forge-api-pchecks-verdict-")); | |
| 32 | app = createApp({ dataDir, port: 0 }); | |
| 33 | servers = await serve(app); | |
| 34 | const made = await createUser(app.db, { | |
| 35 | origin: "seed", | |
| 36 | trusted: true, | |
| 37 | username: "alice", | |
| 38 | email: "[email protected]", | |
| 39 | password: "a-good-passphrase", | |
| 40 | adminIfFirst: true, | |
| 41 | }); | |
| 42 | if (!made.ok) throw new Error(String(made.error)); | |
| 43 | const alice = made.value; | |
| 44 | cookie = `forge_session=${createSession(app.db, alice.id).token}`; | |
| 45 | const repo = await createRepo(app, alice, { name: "r", visibility: "public" }); | |
| 46 | if (!repo.ok) throw new Error(String(repo.error)); | |
| 47 | repoId = repo.value.id; | |
| 48 | for (const [branch, file] of [[undefined, "forge.json"], ["topic", "t.txt"]] as const) { | |
| 49 | const done = await commitToBranch(app, alice, repo.value, { | |
| 50 | ...(branch === undefined ? {} : { branch }), | |
| 51 | message: "change", | |
| 52 | files: [{ path: file, content: file === "forge.json" ? "{}" : "x" }], | |
| 53 | }); | |
| 54 | if (!done.ok) throw new Error(JSON.stringify(done.error)); | |
| 55 | } | |
| 56 | const pull = await openPull(app, alice, repo.value, { | |
| 57 | title: "t", | |
| 58 | body: "", | |
| 59 | baseBranch: "main", | |
| 60 | headBranch: "topic", | |
| 61 | }); | |
| 62 | if (!pull.ok) throw new Error(JSON.stringify(pull.error)); | |
| 63 | number = pull.value.number; | |
| 64 | pullId = pull.value.id; | |
| 65 | }); | |
| 66 | ||
| 67 | afterAll(async () => { | |
| 68 | await servers.stop(); | |
| 69 | app.close(); | |
| 70 | if (cells === undefined) delete process.env["FORGE_CELLS"]; | |
| 71 | else process.env["FORGE_CELLS"] = cells; | |
| 72 | await rm(dataDir, { recursive: true, force: true }); | |
| 73 | }); | |
| 74 | ||
| 75 | const path = () => `/api/v1/repos/alice/r/pulls/${number}/checks`; | |
| 76 | const read = async (query = "") => fetch(`${servers.url}${path()}${query}`, { headers: { cookie } }); | |
| 77 | ||
| 78 | test("a failed check, then a deploy that is ok, answers failed", async () => { | |
| 79 | const head = ((await (await read()).json()) as { head: string }).head; | |
| 80 | const at = { repoId, issueId: pullId, env: `pull-${number}`, commitOid: head }; | |
| 81 | const check = createRun(app.db, { ...at, kind: "check" }); | |
| 82 | claimRun(app.db, check.id); | |
| 83 | finishRun(app.db, check.id, { state: "failed", summary: "1 of 22716 tests failed" }); | |
| 84 | const deploy = createRun(app.db, { ...at, kind: "deploy" }); | |
| 85 | claimRun(app.db, deploy.id); | |
| 86 | finishRun(app.db, deploy.id, { state: "ok", summary: "deployed" }); | |
| 87 | ||
| 88 | const body = (await (await read()).json()) as { state: string; run: { id: string }; runs: unknown[] }; | |
| 89 | expect(body.state).toBe("failed"); | |
| 90 | expect(body.run.id).toBe(check.id); | |
| 91 | expect(body.runs).toHaveLength(2); | |
| 92 | ||
| 93 | const text = await (await read("?format=text")).text(); | |
| 94 | expect(text).toContain("state: failed\n"); | |
| 95 | }); |
new above
offline