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

The API's commit check read names its Server-Timing phases: session, check, render #4934

Merged opened this

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

Merged

scout/forge:scout/4928-the-api-s-commit-check-read-names-its-se → main 1 commit fork

  • Merged. scout/4928-the-api-s-commit-check-read-names-its-se was merged into main as 8357069.

Review

Sign in to review this change.
2 files changed +70 −2
src/http/api.ts +7 −2
@@ -2857,11 +2857,16 @@ export function apiRoutes(router: Router<Handler>): void {
28572857 // The default branch's check covering a full commit id: what a release
28582858 // waits on before it builds (#2794, #3966). Never cached: `pending` turns.
28592859 router.add("GET", "/api/v1/repos/:owner/:name/commits/:oid/check", async (ctx) => {
2860 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
28602861 const repo = loadRepo(ctx);
28612862 if (!repo) return notFound();
2862 const found = await checkOfCommit(ctx.app, ctx.auth.user, repo, ctx.params.oid ?? "");
2863 const found = await timePhaseAsync(ctx.request, "check", () =>
2864 checkOfCommit(ctx.app, ctx.auth.user, repo, ctx.params.oid ?? ""),
2865 );
28632866 if (!found.ok) return notFound();
2864 return json(found.value, 200, { "cache-control": "no-store" });
2867 return timePhase(ctx.request, "render", () =>
2868 json(found.value, 200, { "cache-control": "no-store" }),
2869 );
28652870 });
28662871
28672872 // The path is optional: no path lists the root tree; a directory lists its
test/api-ccheck-timing.test.ts added +63
@@ -0,0 +1,63 @@
1 /** The API commit check names its Server-Timing phases (#4928). */
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 { createRepo } from "../src/actions/repos.ts";
8 import { createSession } from "../src/core/sessions.ts";
9 import { createUser } from "../src/core/users.ts";
10 import { serve, type Servers } from "../src/http/server.ts";
11 import { quietLogs } from "./helpers/globals.ts";
12
13 quietLogs();
14
15 let dataDir: string;
16 let app: App;
17 let servers: Servers;
18 let cookie = "";
19
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-ccheck-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 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 });
41 afterAll(async () => {
42 await servers.stop();
43 app.close();
44 if (cells === undefined) delete process.env["FORGE_CELLS"];
45 else process.env["FORGE_CELLS"] = cells;
46 await rm(dataDir, { recursive: true, force: true });
47 });
48
49 async function phases(path: string): Promise<{ status: number; names: string[]; body: string }> {
50 const res = await fetch(`${servers.url}${path}`, { headers: { cookie } });
51 const body = await res.text();
52 const names = (res.headers.get("server-timing") ?? "").split(", ").map((p) => p.split(";")[0]!);
53 return { status: res.status, names: names.slice(names.indexOf("app") + 1), body };
54 }
55
56 test("the commit check names session, check, render in order", async () => {
57 const oid = "a".repeat(40);
58 const got = await phases(`/api/v1/repos/alice/r/commits/${oid}/check`);
59 expect(got.status).toBe(200);
60 expect(got.names).toEqual(["session", "check", "render"]);
61 const missing = await phases("/api/v1/repos/alice/nope/commits/" + oid + "/check");
62 expect(missing.names).toEqual(["session"]);
63 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs