Public: anyone can read this; the crew can change it.
The API's pull review states read names its Server-Timing phases: session, reviews, render #4933
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/4927-the-api-s-pull-review-states-read-names → main 1 commit fork
2 files
changed +82
−1
src/http/api.ts +3 −1
| @@ -4228,11 +4228,13 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 4228 | 4228 | "GET", |
| 4229 | 4229 | "/api/v1/repos/:owner/:name/pulls/:number/review-states", |
| 4230 | 4230 | (ctx) => { |
| 4231 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 4231 | 4232 | const found = loadPull(ctx); |
| 4232 | 4233 | if ("error" in found) return found.error; |
| 4233 | return json( | |
| 4234 | const reviews = timePhase(ctx.request, "reviews", () => | |
| 4234 | 4235 | readerMarked(ctx, found.repo.id, listReviews(ctx.app.db, found.pull.id)), |
| 4235 | 4236 | ); |
| 4237 | return timePhase(ctx.request, "render", () => json(reviews)); | |
| 4236 | 4238 | }, |
| 4237 | 4239 | ); |
| 4238 | 4240 |
test/api-rstates-timing.test.ts added +79
| @@ -0,0 +1,79 @@ | ||
| 1 | /** The API's review states read names its Server-Timing phases (#4927). */ | |
| 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-rstates-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 review states list names session, reviews, render in order", async () => { | |
| 75 | const got = await phases(`/api/v1/repos/alice/r/pulls/${number}/review-states`); | |
| 76 | expect(got.status).toBe(200); | |
| 77 | expect(got.names).toEqual(["session", "reviews", "render"]); | |
| 78 | expect(Array.isArray(JSON.parse(got.body))).toBe(true); | |
| 79 | }); |
new above
offline