Public: anyone can read this; the crew can change it.
The API's read of one pull names its Server-Timing phases: session, pull, head, diff, checks, render #3769
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/3765-the-api-s-read-of-one-pull-names-its-ser → main 1 commit fork
2 files
changed +120
−26
src/http/api.ts +35 −26
| @@ -15,6 +15,7 @@ import { listPeople, NOT_A_MEMBER } from "../core/roles.ts"; | ||
| 15 | 15 | import { setAccountCeilings, setAccountGrant } from "../actions/model_grant.ts"; |
| 16 | 16 | import { storageLimitMessage } from "../core/storage.ts"; |
| 17 | 17 | import { voteVariant } from "../actions/variants.ts"; |
| 18 | import { notePhase, takeSessionTime, timePhase, timePhaseAsync } from "../core/route_timing.ts"; | |
| 18 | 19 | import { parentForgeFor } from "../core/parent_releases.ts"; |
| 19 | 20 | import { readFlow } from "../flow/read.ts"; |
| 20 | 21 | import { listPinned as listPinnedPanels, movePinned, pinPanel, unpinPanel, type PinError } from "../actions/board-pins.ts"; |
| @@ -3672,41 +3673,49 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 3672 | 3673 | }); |
| 3673 | 3674 | |
| 3674 | 3675 | router.add("GET", "/api/v1/repos/:owner/:name/pulls/:number", async (ctx) => { |
| 3675 | const found = loadPull(ctx); | |
| 3676 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 3677 | const found = timePhase(ctx.request, "pull", () => loadPull(ctx)); | |
| 3676 | 3678 | if ("error" in found) return found.error; |
| 3677 | 3679 | // A fork may have pushed since the pull opened; review what is being |
| 3678 | 3680 | // proposed now, not the snapshot taken when it was. |
| 3679 | await refreshPullHead(ctx.app, found.repo, found.pull); | |
| 3681 | await timePhaseAsync(ctx.request, "head", () => | |
| 3682 | refreshPullHead(ctx.app, found.repo, found.pull), | |
| 3683 | ); | |
| 3680 | 3684 | // Include the diff so a reviewer (human or agent) reads the change without |
| 3681 | 3685 | // a second round trip, and the review gate beside it so a client knows |
| 3682 | 3686 | // whether merging is even on offer. |
| 3683 | const { status, diff } = await pullDiff( | |
| 3684 | await pullDirOf(ctx, found.repo, found.pull), | |
| 3685 | found.pull, | |
| 3686 | { db: ctx.app.db, requireReview: found.repo.requireReview }, | |
| 3687 | const { status, diff } = await timePhaseAsync(ctx.request, "diff", async () => | |
| 3688 | pullDiff(await pullDirOf(ctx, found.repo, found.pull), found.pull, { | |
| 3689 | db: ctx.app.db, | |
| 3690 | requireReview: found.repo.requireReview, | |
| 3691 | }), | |
| 3687 | 3692 | ); |
| 3688 | const hold = getHold(ctx.app.db, found.pull.id); | |
| 3689 | 3693 | const live = found.pull.state === "open" && found.pull.mergedAt === null; |
| 3690 | const checkStaleNow = | |
| 3691 | live && | |
| 3692 | checkStale( | |
| 3693 | checkSummary(ctx.app.db, found.pull.id, status.headOid).run, | |
| 3694 | await resolveOid( | |
| 3695 | await pullDirOf(ctx, found.repo, found.pull), | |
| 3696 | `refs/heads/${found.pull.baseBranch}`, | |
| 3694 | const checkStaleNow = await timePhaseAsync( | |
| 3695 | ctx.request, | |
| 3696 | "checks", | |
| 3697 | async () => | |
| 3698 | live && | |
| 3699 | checkStale( | |
| 3700 | checkSummary(ctx.app.db, found.pull.id, status.headOid).run, | |
| 3701 | await resolveOid( | |
| 3702 | await pullDirOf(ctx, found.repo, found.pull), | |
| 3703 | `refs/heads/${found.pull.baseBranch}`, | |
| 3704 | ), | |
| 3697 | 3705 | ), |
| 3698 | ); | |
| 3699 | return json({ | |
| 3700 | ...withLabels(ctx, found.pull), | |
| 3701 | checkStale: checkStaleNow, | |
| 3702 | variantSiblings: variantSiblings(ctx.app.db, [found.pull])[0] ?? [], | |
| 3703 | ...(found.pull.state === "open" && found.pull.mergedAt === null | |
| 3704 | ? { mergeable: status.mergeable, conflicts: status.conflicts } | |
| 3705 | : {}), | |
| 3706 | proposal: pullProposalOf(ctx.app.db, found.pull.id) ?? null, | |
| 3707 | status, | |
| 3708 | diff, | |
| 3709 | hold: hold === undefined ? null : holdJson(hold), | |
| 3706 | ); | |
| 3707 | return timePhase(ctx.request, "render", () => { | |
| 3708 | const hold = getHold(ctx.app.db, found.pull.id); | |
| 3709 | return json({ | |
| 3710 | ...withLabels(ctx, found.pull), | |
| 3711 | checkStale: checkStaleNow, | |
| 3712 | variantSiblings: variantSiblings(ctx.app.db, [found.pull])[0] ?? [], | |
| 3713 | ...(live ? { mergeable: status.mergeable, conflicts: status.conflicts } : {}), | |
| 3714 | proposal: pullProposalOf(ctx.app.db, found.pull.id) ?? null, | |
| 3715 | status, | |
| 3716 | diff, | |
| 3717 | hold: hold === undefined ? null : holdJson(hold), | |
| 3718 | }); | |
| 3710 | 3719 | }); |
| 3711 | 3720 | }); |
| 3712 | 3721 |
test/api-pull-timing.test.ts added +85
| @@ -0,0 +1,85 @@ | ||
| 1 | /** The API's read of one pull names its Server-Timing phases (#3765). */ | |
| 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 | let number = 0; | |
| 21 | const cells = process.env["FORGE_CELLS"]; | |
| 22 | ||
| 23 | beforeAll(async () => { | |
| 24 | process.env["FORGE_CELLS"] = "off"; | |
| 25 | dataDir = await mkdtemp(join(tmpdir(), "forge-api-pull-timing-")); | |
| 26 | app = createApp({ dataDir, port: 0 }); | |
| 27 | servers = await serve(app); | |
| 28 | const made = await createUser(app.db, { | |
| 29 | origin: "seed", | |
| 30 | trusted: true, | |
| 31 | username: "alice", | |
| 32 | email: "[email protected]", | |
| 33 | password: "a-good-passphrase", | |
| 34 | adminIfFirst: true, | |
| 35 | }); | |
| 36 | if (!made.ok) throw new Error(String(made.error)); | |
| 37 | const alice = made.value; | |
| 38 | cookie = `forge_session=${createSession(app.db, alice.id).token}`; | |
| 39 | const repo = await createRepo(app, alice, { name: "r", visibility: "public" }); | |
| 40 | if (!repo.ok) throw new Error(String(repo.error)); | |
| 41 | for (const [branch, file] of [[undefined, "forge.json"], ["topic", "t.txt"]] as const) { | |
| 42 | const done = await commitToBranch(app, alice, repo.value, { | |
| 43 | ...(branch === undefined ? {} : { branch }), | |
| 44 | message: "change", | |
| 45 | files: [{ path: file, content: file === "forge.json" ? "{}" : "x" }], | |
| 46 | }); | |
| 47 | if (!done.ok) throw new Error(JSON.stringify(done.error)); | |
| 48 | } | |
| 49 | const pull = await openPull(app, alice, repo.value, { | |
| 50 | title: "t", | |
| 51 | body: "", | |
| 52 | baseBranch: "main", | |
| 53 | headBranch: "topic", | |
| 54 | }); | |
| 55 | if (!pull.ok) throw new Error(JSON.stringify(pull.error)); | |
| 56 | number = pull.value.number; | |
| 57 | }); | |
| 58 | ||
| 59 | afterAll(async () => { | |
| 60 | await servers.stop(); | |
| 61 | app.close(); | |
| 62 | if (cells === undefined) delete process.env["FORGE_CELLS"]; | |
| 63 | else process.env["FORGE_CELLS"] = cells; | |
| 64 | await rm(dataDir, { recursive: true, force: true }); | |
| 65 | }); | |
| 66 | ||
| 67 | async function phases(path: string): Promise<{ status: number; names: string[]; body: string }> { | |
| 68 | const res = await fetch(`${servers.url}${path}`, { headers: { cookie } }); | |
| 69 | const body = await res.text(); | |
| 70 | const names = (res.headers.get("server-timing") ?? "").split(", ").map((p) => p.split(";")[0]!); | |
| 71 | return { status: res.status, names: names.slice(names.indexOf("app") + 1), body }; | |
| 72 | } | |
| 73 | ||
| 74 | test("the API read of an open pull names its six phases in order", async () => { | |
| 75 | const got = await phases(`/api/v1/repos/alice/r/pulls/${number}`); | |
| 76 | expect(got.status).toBe(200); | |
| 77 | expect(got.names).toEqual(["session", "pull", "head", "diff", "checks", "render"]); | |
| 78 | expect(JSON.parse(got.body).number).toBe(number); | |
| 79 | }); | |
| 80 | ||
| 81 | test("a missing pull still names session and pull", async () => { | |
| 82 | const got = await phases("/api/v1/repos/alice/r/pulls/9999"); | |
| 83 | expect(got.status).toBe(404); | |
| 84 | expect(got.names).toEqual(["session", "pull"]); | |
| 85 | }); |
new above
offline