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

The API's pull reviews list names its Server-Timing phases: session, reviews, render #4954

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/4951-the-api-s-pull-reviews-list-names-its-se → main 1 commit fork

  • Merged. scout/4951-the-api-s-pull-reviews-list-names-its-se was merged into main as 3af98d2.

Review

Sign in to review this change.
2 files changed +98 −13
src/http/api.ts +19 −13
@@ -4156,27 +4156,33 @@ export function apiRoutes(router: Router<Handler>): void {
41564156 "GET",
41574157 "/api/v1/repos/:owner/:name/pulls/:number/reviews",
41584158 async (ctx) => {
4159 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
41594160 const found = loadPull(ctx);
41604161 if ("error" in found) return found.error;
4161 const { diff } = await pullDiff(
4162 await pullDirOf(ctx, found.repo, found.pull),
4163 found.pull,
4164 );
4165 return json(
4166 withOutdated(
4162 const marked = await timePhaseAsync(ctx.request, "reviews", async () => {
4163 const { diff } = await pullDiff(
4164 await pullDirOf(ctx, found.repo, found.pull),
4165 found.pull,
4166 );
4167 return withOutdated(
41674168 readerMarked(
41684169 ctx,
41694170 found.repo.id,
41704171 listReviewComments(ctx.app.db, found.pull.id),
41714172 ),
41724173 diff,
4173 ).map(
4174 // The thread this comment belongs to, so a client can resolve it
4175 // without reassembling the key itself.
4176 (comment) => ({
4177 ...comment,
4178 anchor: anchorId(comment.path, comment.side, comment.line),
4179 }),
4174 );
4175 });
4176 return timePhase(ctx.request, "render", () =>
4177 json(
4178 marked.map(
4179 // The thread this comment belongs to, so a client can resolve it
4180 // without reassembling the key itself.
4181 (comment) => ({
4182 ...comment,
4183 anchor: anchorId(comment.path, comment.side, comment.line),
4184 }),
4185 ),
41804186 ),
41814187 );
41824188 },
test/api-previews-timing.test.ts added +79
@@ -0,0 +1,79 @@
1 /** The API pull reviews list names its Server-Timing phases (#4951). */
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-previews-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 pull reviews list names session, reviews, render", async () => {
75 const got = await phases(`/api/v1/repos/alice/r/pulls/${number}/reviews`);
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 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs