Public: anyone can read this; the crew can change it.

The API's pull list names its Server-Timing phases: session, list, merge, labels, render #3783

Parked opened this
Closed without merging

scout/forge:scout/3770-the-api-s-pull-list-names-its-server-tim → main 2 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
2 files changed +110 −13
src/http/api.ts +24 −13
@@ -8,6 +8,7 @@
88 * back to continue); errors are `{ error, message }`.
99 */
1010
11 import { notePhase, takeSessionTime, timePhase, timePhaseAsync } from "../core/route_timing.ts";
1112 import { goldenPath } from "../apps/golden_path.ts";
1213 import { upstreamPayload } from "../spawn/upstream.ts";
1314 import { setRole } from "../actions/roles.ts";
@@ -3590,25 +3591,35 @@ export function apiRoutes(router: Router<Handler>): void {
35903591 // ── pull requests — so automation drives the review/merge loop ───────
35913592
35923593 router.add("GET", "/api/v1/repos/:owner/:name/pulls", async (ctx) => {
3594 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
35933595 const repo = loadRepo(ctx);
35943596 if (!repo) return notFound();
35953597 const query = listQuery(ctx);
3596 const pulls = listPulls(ctx.app.db, repo.id, {
3597 ...query,
3598 ...sealedScope(ctx.app.db, repo, ctx.auth.user),
3599 });
3600 const states = await pullMergeStates(
3601 await Promise.all(
3602 pulls.map(async (p) => ({ dir: await pullDirOf(ctx, repo, p), pull: p })),
3598 const pulls = timePhase(ctx.request, "list", () =>
3599 listPulls(ctx.app.db, repo.id, {
3600 ...query,
3601 ...sealedScope(ctx.app.db, repo, ctx.auth.user),
3602 }),
3603 );
3604 const states = await timePhaseAsync(ctx.request, "merge", async () =>
3605 pullMergeStates(
3606 await Promise.all(
3607 pulls.map(async (p) => ({ dir: await pullDirOf(ctx, repo, p), pull: p })),
3608 ),
36033609 ),
36043610 );
3605 const siblings = variantSiblings(ctx.app.db, pulls);
3606 const items = withLabelsMany(ctx, pulls).map((item, i) => ({
3607 ...item,
3608 variantSiblings: siblings[i] ?? [],
3609 ...(states[i] ?? {}),
3611 const labelled = timePhase(ctx.request, "labels", () => ({
3612 siblings: variantSiblings(ctx.app.db, pulls),
3613 items: withLabelsMany(ctx, pulls),
36103614 }));
3611 return json(paged(items, query.limit, (p) => p.number));
3615 return timePhase(ctx.request, "render", () => {
3616 const items = labelled.items.map((item, i) => ({
3617 ...item,
3618 variantSiblings: labelled.siblings[i] ?? [],
3619 ...(states[i] ?? {}),
3620 }));
3621 return json(paged(items, query.limit, (p) => p.number));
3622 });
36123623 });
36133624
36143625 // A writer hands a child its own narrow credential for this repository
test/api-pulls-timing.test.ts added +86
@@ -0,0 +1,86 @@
1 /** The API's pull list names its Server-Timing phases (#3770). */
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 headers: Record<string, string> = {};
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 headers = { 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: "init",
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 ${branch}`,
49 files: [{ path: `${branch}.txt`, content: branch }],
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 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 const names = (res: Response): string[] =>
71 (res.headers.get("server-timing") ?? "").split(", ").map((p) => p.split(";")[0]!);
72
73 test("the pull list names session, list, merge, labels and render once each", async () => {
74 const res = await fetch(`${servers.url}/api/v1/repos/alice/r/pulls`, { headers });
75 expect(res.status).toBe(200);
76 const body = (await res.json()) as { items: unknown[] };
77 expect(body.items.length).toBe(2);
78 for (const n of ["session", "list", "merge", "labels", "render"])
79 expect(names(res).filter((x) => x === n).length).toBe(1);
80 });
81
82 test("an unknown repository's 404 still names session", async () => {
83 const res = await fetch(`${servers.url}/api/v1/repos/alice/nope/pulls`, { headers });
84 expect(res.status).toBe(404);
85 expect(names(res)).toContain("session");
86 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs