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

The API's pull checks read names its Server-Timing phases: session, checks, render #4931

Merged opened this

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

Merged

scout/forge:scout/4925-the-api-s-pull-checks-read-names-its-ser → main 1 commit fork 1 thread · 1 unresolved

  • Merged. scout/4925-the-api-s-pull-checks-read-names-its-ser was merged into main as 84d71f9.

Review

Sign in to review this change.
2 files changed +93 −9
src/http/api.ts +14 −9
@@ -6065,18 +6065,23 @@ export function apiRoutes(router: Router<Handler>): void {
60656065 "GET",
60666066 "/api/v1/repos/:owner/:name/pulls/:number/checks",
60676067 async (ctx) => {
6068 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
60686069 const found = loadPull(ctx);
60696070 if ("error" in found) return found.error;
6070 const head = await pullHeadOid(ctx, found.repo, found.pull);
6071 const runs = checksForHead(ctx.app.db, found.pull.id, head);
6072 const latest = runs[0];
6073 return json({
6074 head,
6075 state: latest?.state ?? "none",
6076 required: found.repo.requireChecks,
6077 run: latest ?? null,
6078 runs,
6071 const { head, runs } = await timePhaseAsync(ctx.request, "checks", async () => {
6072 const oid = await pullHeadOid(ctx, found.repo, found.pull);
6073 return { head: oid, runs: checksForHead(ctx.app.db, found.pull.id, oid) };
60796074 });
6075 const latest = runs[0];
6076 return timePhase(ctx.request, "render", () =>
6077 json({
6078 head,
6079 state: latest?.state ?? "none",
6080 required: found.repo.requireChecks,
6081 run: latest ?? null,
6082 runs,
6083 }),
6084 );
60806085 },
60816086 );
60826087
test/api-pchecks-timing.test.ts added +79
@@ -0,0 +1,79 @@
1 /** The API's read of one pull pull checks read names its Server-Timing phases (#4925). */

Nit: "one pull pull checks read" has a repeated word. Something like "The API's read of one pull's checks names its Server-Timing phases (#4925)." would fix it. Not blocking.

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-pchecks-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's checks read names session, checks, render in order", async () => {
75 const got = await phases(`/api/v1/repos/alice/r/pulls/${number}/checks`);
76 expect(got.status).toBe(200);
77 expect(got.names).toEqual(["session", "checks", "render"]);
78 expect(JSON.parse(got.body).state).toBe("none");
79 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs