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

The API's read of one commit names its Server-Timing phases: session, git, diff, render #3916

Merged opened this

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

Merged

scout/forge:scout/3900-the-api-s-read-of-one-commit-names-its-s → main 2 commits fork

  • Merged. scout/3900-the-api-s-read-of-one-commit-names-its-s was merged into main as 3695edf.

Review

Sign in to review this change.
2 files changed +92 −6
src/http/api.ts +11 −6
@@ -2725,19 +2725,24 @@ export function apiRoutes(router: Router<Handler>): void {
27252725 });
27262726
27272727 router.add("GET", "/api/v1/repos/:owner/:name/commits/:oid", async (ctx) => {
2728 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
27282729 const repo = loadRepo(ctx);
27292730 if (!repo) return notFound();
27302731 const rev = ctx.params.oid!;
27312732 if (!isSafeRef(rev)) return notFound();
27322733 const dir = dirOf(ctx, repo);
2733 const log = await commitLog(dir, rev, { limit: 1 });
2734 const log = await timePhaseAsync(ctx.request, "git", () =>
2735 commitLog(dir, rev, { limit: 1 }),
2736 );
27342737 const commit = log.ok ? log.value[0] : undefined;
27352738 if (!commit) return notFound();
2736 const diff = await diffCommit(dir, commit.oid);
2737 return json({
2738 ...commit,
2739 diff: diff.ok ? diff.value : { files: [], additions: 0, deletions: 0 },
2740 });
2739 const diff = await timePhaseAsync(ctx.request, "diff", () => diffCommit(dir, commit.oid));
2740 return timePhase(ctx.request, "render", () =>
2741 json({
2742 ...commit,
2743 diff: diff.ok ? diff.value : { files: [], additions: 0, deletions: 0 },
2744 }),
2745 );
27412746 });
27422747
27432748 // The default branch's own check of a full commit id: what a release
test/api-commit-timing.test.ts added +81
@@ -0,0 +1,81 @@
1 /** The API's read of one commit names its Server-Timing phases (#3900). */
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 { commitToBranch, 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 let oid = "";
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-commit-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 const done = await commitToBranch(app, alice, repo.value, {
41 message: "change",
42 files: [{ path: "forge.json", content: "{}" }],
43 });
44 if (!done.ok) throw new Error(JSON.stringify(done.error));
45 const res = await fetch(`${servers.url}/api/v1/repos/alice/r/commits?ref=main`, { headers: { cookie } });
46 oid = ((await res.json()) as { oid: string }[])[0]!.oid;
47 });
48
49 afterAll(async () => {
50 await servers.stop();
51 app.close();
52 if (cells === undefined) delete process.env["FORGE_CELLS"];
53 else process.env["FORGE_CELLS"] = cells;
54 await rm(dataDir, { recursive: true, force: true });
55 });
56
57 async function phases(path: string): Promise<{ status: number; names: string[]; body: string }> {
58 const res = await fetch(`${servers.url}${path}`, { headers: { cookie } });
59 const body = await res.text();
60 const names = (res.headers.get("server-timing") ?? "").split(", ").map((p) => p.split(";")[0]!);
61 return { status: res.status, names: names.slice(names.indexOf("app") + 1), body };
62 }
63
64 test("the API read of one commit names session, git, diff, render", async () => {
65 const got = await phases(`/api/v1/repos/alice/r/commits/${oid}`);
66 expect(got.status).toBe(200);
67 expect(got.names).toEqual(["session", "git", "diff", "render"]);
68 expect(JSON.parse(got.body).oid).toBe(oid);
69 });
70
71 test("a missing repository, an unsafe rev and an unknown commit name session", async () => {
72 const missing = await phases(`/api/v1/repos/alice/nope/commits/${oid}`);
73 expect(missing.status).toBe(404);
74 expect(missing.names).toEqual(["session"]);
75 const unsafe = await phases("/api/v1/repos/alice/r/commits/--output=x");
76 expect(unsafe.status).toBe(404);
77 expect(unsafe.names).toEqual(["session"]);
78 const unknown = await phases(`/api/v1/repos/alice/r/commits/${"0".repeat(40)}`);
79 expect(unknown.status).toBe(404);
80 expect(unknown.names).toEqual(["session", "git"]);
81 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs