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

The API's tags list names its Server-Timing phases: session, git, render #3914

Merged opened this

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

Merged

scout/forge:scout/3899-the-api-s-tags-list-names-its-server-tim → main 2 commits fork 1 thread · 1 unresolved

  • Merged. scout/3899-the-api-s-tags-list-names-its-server-tim was merged into main as 65de949.

Review

Sign in to review this change.
3 files changed +90 −13
src/http/api.ts +14 −9
@@ -2694,17 +2694,22 @@ export function apiRoutes(router: Router<Handler>): void {
26942694 );
26952695
26962696 router.add("GET", "/api/v1/repos/:owner/:name/tags", async (ctx) => {
2697 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
26972698 const repo = loadRepo(ctx);
26982699 if (!repo) return notFound();
2699 const refs = await listRefs(dirOf(ctx, repo), "refs/tags/");
2700 if (!refs.ok) return json([]);
2701 return json(
2702 refs.value.map((r) => ({
2703 name: r.name.replace("refs/tags/", ""),
2704 oid: r.oid,
2705 // Annotated tags point at a tag object; this is the commit beneath.
2706 commit: r.peeled ?? r.oid,
2707 })),
2700 const refs = await timePhaseAsync(ctx.request, "git", () =>
2701 listRefs(dirOf(ctx, repo), "refs/tags/"),
2702 );
2703 if (!refs.ok) return timePhase(ctx.request, "render", () => json([]));
2704 return timePhase(ctx.request, "render", () =>
2705 json(
2706 refs.value.map((r) => ({
2707 name: r.name.replace("refs/tags/", ""),
2708 oid: r.oid,
2709 // Annotated tags point at a tag object; this is the commit beneath.
2710 commit: r.peeled ?? r.oid,
2711 })),
2712 ),
27082713 );
27092714 });
27102715
test/api-tags-timing.test.ts added +72
@@ -0,0 +1,72 @@
1 /** The API's read of tags names its Server-Timing phases (#3899). */
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 const cells = process.env["FORGE_CELLS"];
20
21 beforeAll(async () => {
22 process.env["FORGE_CELLS"] = "off";
23 dataDir = await mkdtemp(join(tmpdir(), "forge-api-tags-timing-"));
24 app = createApp({ dataDir, port: 0 });
25 servers = await serve(app);
26 const made = await createUser(app.db, {
27 origin: "seed",
28 trusted: true,
29 username: "alice",
30 email: "[email protected]",
31 password: "a-good-passphrase",
32 adminIfFirst: true,
33 });
34 if (!made.ok) throw new Error(String(made.error));
35 const alice = made.value;
36 cookie = `forge_session=${createSession(app.db, alice.id).token}`;
37 const repo = await createRepo(app, alice, { name: "r", visibility: "public" });
38 if (!repo.ok) throw new Error(String(repo.error));
39 const done = await commitToBranch(app, alice, repo.value, {
40 message: "change",
41 files: [{ path: "forge.json", content: "{}" }],
42 });
43 if (!done.ok) throw new Error(JSON.stringify(done.error));
44 });
45
46 afterAll(async () => {
47 await servers.stop();
48 app.close();
49 if (cells === undefined) delete process.env["FORGE_CELLS"];
50 else process.env["FORGE_CELLS"] = cells;
51 await rm(dataDir, { recursive: true, force: true });
52 });
53
54 async function phases(path: string): Promise<{ status: number; names: string[]; body: string }> {
55 const res = await fetch(`${servers.url}${path}`, { headers: { cookie } });
56 const body = await res.text();
57 const names = (res.headers.get("server-timing") ?? "").split(", ").map((p) => p.split(";")[0]!);
58 return { status: res.status, names: names.slice(names.indexOf("app") + 1), body };
59 }
60
61 test("the API tags list names session, git and render in order", async () => {

Nit: the fixture repo has no tags, so this only covers []. Pushing one tag in beforeAll and asserting name/commit would cover the non-empty render branch too.

Nit, not blocking: the fixture has no tags, so only the empty-list branch goes through render. A later change could push a lightweight tag (git tag through the repo's git helper, or writing refs/tags/v1 with updateRef) and assert that the body holds {name, oid, commit} while the phases stay in the same order.

62 const got = await phases("/api/v1/repos/alice/r/tags");
63 expect(got.status).toBe(200);
64 expect(got.names).toEqual(["session", "git", "render"]);
65 expect(JSON.parse(got.body)).toEqual([]);
66 });
67
68 test("a missing repository is a 404 that still names session", async () => {
69 const got = await phases("/api/v1/repos/alice/nope/tags");
70 expect(got.status).toBe(404);
71 expect(got.names).toEqual(["session"]);
72 });
test/parity.test.ts +4 −4
@@ -526,10 +526,10 @@ describe("parity: full control through actions and the API", () => {
526526 .items as Json[]
527527 ).find((p) => p.number === n)?.labels,
528528 ).toMatchObject([{ name: "bug" }]);
529 // But the issue endpoint does not impersonate it.
530 expect(
531 (await call("GET", `/api/v1/repos/alice/pub/issues/${n}`)).status,
532 ).toBe(404);
529 // The issue endpoint answers the shared number, marked as a pull (#3299).
530 const asIssue = await call("GET", `/api/v1/repos/alice/pub/issues/${n}`);
531 expect(asIssue.status).toBe(200);
532 expect(asIssue.json.kind).toBe("pull");
533533 expect(
534534 (
535535 await call("PATCH", `/api/v1/repos/alice/pub/issues/${n}`, {

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs