Public: anyone can read this; the crew can change it.
GET …/pulls names its Server-Timing phases: session, list, merge, labels, render #4026
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/3964-get-pulls-names-its-server-timing-phases → main 1 commit fork
2 files
changed +112
−14
src/http/api.ts +24 −14
| @@ -3592,25 +3592,35 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 3592 | 3592 | // ── pull requests — so automation drives the review/merge loop ─────── |
| 3593 | 3593 | |
| 3594 | 3594 | router.add("GET", "/api/v1/repos/:owner/:name/pulls", async (ctx) => { |
| 3595 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 3595 | 3596 | const repo = loadRepo(ctx); |
| 3596 | 3597 | if (!repo) return notFound(); |
| 3597 | const query = listQuery(ctx); | |
| 3598 | const pulls = listPulls(ctx.app.db, repo.id, { | |
| 3599 | ...query, | |
| 3600 | ...sealedScope(ctx.app.db, repo, ctx.auth.user), | |
| 3598 | const { query, pulls } = timePhase(ctx.request, "list", () => { | |
| 3599 | const query = listQuery(ctx); | |
| 3600 | const pulls = listPulls(ctx.app.db, repo.id, { | |
| 3601 | ...query, | |
| 3602 | ...sealedScope(ctx.app.db, repo, ctx.auth.user), | |
| 3603 | }); | |
| 3604 | return { query, pulls }; | |
| 3601 | 3605 | }); |
| 3602 | const states = await pullMergeStates( | |
| 3603 | await Promise.all( | |
| 3604 | pulls.map(async (p) => ({ dir: await pullDirOf(ctx, repo, p), pull: p })), | |
| 3606 | const states = await timePhaseAsync(ctx.request, "merge", async () => | |
| 3607 | pullMergeStates( | |
| 3608 | await Promise.all( | |
| 3609 | pulls.map(async (p) => ({ dir: await pullDirOf(ctx, repo, p), pull: p })), | |
| 3610 | ), | |
| 3605 | 3611 | ), |
| 3606 | 3612 | ); |
| 3607 | const siblings = variantSiblings(ctx.app.db, pulls); | |
| 3608 | const items = withLabelsMany(ctx, pulls).map((item, i) => ({ | |
| 3609 | ...item, | |
| 3610 | variantSiblings: siblings[i] ?? [], | |
| 3611 | ...(states[i] ?? {}), | |
| 3612 | })); | |
| 3613 | return json(paged(items, query.limit, (p) => p.number)); | |
| 3613 | const items = timePhase(ctx.request, "labels", () => { | |
| 3614 | const siblings = variantSiblings(ctx.app.db, pulls); | |
| 3615 | return withLabelsMany(ctx, pulls).map((item, i) => ({ | |
| 3616 | ...item, | |
| 3617 | variantSiblings: siblings[i] ?? [], | |
| 3618 | ...(states[i] ?? {}), | |
| 3619 | })); | |
| 3620 | }); | |
| 3621 | return timePhase(ctx.request, "render", () => | |
| 3622 | json(paged(items, query.limit, (p) => p.number)), | |
| 3623 | ); | |
| 3614 | 3624 | }); |
| 3615 | 3625 | |
| 3616 | 3626 | // A writer hands a child its own narrow credential for this repository |
test/api-pulls-timing.test.ts added +88
| @@ -0,0 +1,88 @@ | ||
| 1 | /** The API's list of pulls names its Server-Timing phases (#3964). */ | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { openPull } from "../src/actions/pulls.ts"; | |
| 8 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 9 | import { createSession } from "../src/core/sessions.ts"; | |
| 10 | import { createUser } from "../src/core/users.ts"; | |
| 11 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 12 | import { quietLogs } from "./helpers/globals.ts"; | |
| 13 | ||
| 14 | quietLogs(); | |
| 15 | ||
| 16 | let dataDir: string; | |
| 17 | let app: App; | |
| 18 | let servers: Servers; | |
| 19 | let cookie = ""; | |
| 20 | const cells = process.env["FORGE_CELLS"]; | |
| 21 | ||
| 22 | beforeAll(async () => { | |
| 23 | process.env["FORGE_CELLS"] = "off"; | |
| 24 | dataDir = await mkdtemp(join(tmpdir(), "forge-api-pulls-timing-")); | |
| 25 | app = createApp({ dataDir, port: 0 }); | |
| 26 | servers = await serve(app); | |
| 27 | const made = await createUser(app.db, { | |
| 28 | origin: "seed", | |
| 29 | trusted: true, | |
| 30 | username: "alice", | |
| 31 | email: "[email protected]", | |
| 32 | password: "a-good-passphrase", | |
| 33 | adminIfFirst: true, | |
| 34 | }); | |
| 35 | if (!made.ok) throw new Error(String(made.error)); | |
| 36 | const alice = made.value; | |
| 37 | cookie = `forge_session=${createSession(app.db, alice.id).token}`; | |
| 38 | const repo = await createRepo(app, alice, { name: "r", visibility: "public" }); | |
| 39 | if (!repo.ok) throw new Error(String(repo.error)); | |
| 40 | const first = await commitToBranch(app, alice, repo.value, { | |
| 41 | message: "change", | |
| 42 | files: [{ path: "forge.json", content: "{}" }], | |
| 43 | }); | |
| 44 | if (!first.ok) throw new Error(JSON.stringify(first.error)); | |
| 45 | for (const branch of ["one", "two"]) { | |
| 46 | const done = await commitToBranch(app, alice, repo.value, { | |
| 47 | branch, | |
| 48 | message: "change", | |
| 49 | files: [{ path: `${branch}.txt`, content: "x" }], | |
| 50 | }); | |
| 51 | if (!done.ok) throw new Error(JSON.stringify(done.error)); | |
| 52 | const pull = await openPull(app, alice, repo.value, { | |
| 53 | title: branch, | |
| 54 | body: "", | |
| 55 | baseBranch: "main", | |
| 56 | headBranch: branch, | |
| 57 | }); | |
| 58 | if (!pull.ok) throw new Error(JSON.stringify(pull.error)); | |
| 59 | } | |
| 60 | }); | |
| 61 | ||
| 62 | afterAll(async () => { | |
| 63 | await servers.stop(); | |
| 64 | app.close(); | |
| 65 | if (cells === undefined) delete process.env["FORGE_CELLS"]; | |
| 66 | else process.env["FORGE_CELLS"] = cells; | |
| 67 | await rm(dataDir, { recursive: true, force: true }); | |
| 68 | }); | |
| 69 | ||
| 70 | async function phases(path: string): Promise<{ status: number; names: string[]; body: string }> { | |
| 71 | const res = await fetch(`${servers.url}${path}`, { headers: { cookie } }); | |
| 72 | const body = await res.text(); | |
| 73 | const names = (res.headers.get("server-timing") ?? "").split(", ").map((p) => p.split(";")[0]!); | |
| 74 | return { status: res.status, names: names.slice(names.indexOf("app") + 1), body }; | |
| 75 | } | |
| 76 | ||
| 77 | test("the pulls list names its five phases in order", async () => { | |
| 78 | const got = await phases("/api/v1/repos/alice/r/pulls"); | |
| 79 | expect(got.status).toBe(200); | |
| 80 | expect(got.names).toEqual(["session", "list", "merge", "labels", "render"]); | |
| 81 | expect(JSON.parse(got.body).items).toHaveLength(2); | |
| 82 | }); | |
| 83 | ||
| 84 | test("an unknown repository still names session", async () => { | |
| 85 | const got = await phases("/api/v1/repos/alice/nope/pulls"); | |
| 86 | expect(got.status).toBe(404); | |
| 87 | expect(got.names).toEqual(["session"]); | |
| 88 | }); |
new above
offline