Public: anyone can read this; the crew can change it.
The API's pull review threads read names its Server-Timing phases: session, threads, render #4932
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/4926-the-api-s-pull-review-threads-read-names → main 1 commit fork
2 files
changed +82
−1
src/http/api.ts +3 −1
| @@ -4368,9 +4368,11 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 4368 | 4368 | "GET", |
| 4369 | 4369 | "/api/v1/repos/:owner/:name/pulls/:number/threads", |
| 4370 | 4370 | (ctx) => { |
| 4371 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 4371 | 4372 | const found = loadPull(ctx); |
| 4372 | 4373 | if ("error" in found) return found.error; |
| 4373 | return json(pullThreads(ctx.app, found.pull)); | |
| 4374 | const threads = timePhase(ctx.request, "threads", () => pullThreads(ctx.app, found.pull)); | |
| 4375 | return timePhase(ctx.request, "render", () => json(threads)); | |
| 4374 | 4376 | }, |
| 4375 | 4377 | ); |
| 4376 | 4378 |
test/api-threads-timing.test.ts added +79
| @@ -0,0 +1,79 @@ | ||
| 1 | /** The API's pull review threads name their Server-Timing phases (#4926). */ | |
| 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-threads-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 threads read names session, threads, render in order", async () => { | |
| 75 | const got = await phases(`/api/v1/repos/alice/r/pulls/${number}/threads`); | |
| 76 | expect(got.status).toBe(200); | |
| 77 | expect(got.names).toEqual(["session", "threads", "render"]); | |
| 78 | expect(Array.isArray(JSON.parse(got.body))).toBe(true); | |
| 79 | }); |
new above
offline