Public: anyone can read this; the crew can change it.
A repository's forks list names its Server-Timing phases: session, forks, render #4996
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/4985-a-repository-s-forks-list-names-its-serv → main 1 commit fork
2 files
changed +56
−1
src/http/api.ts +3 −1
| @@ -2488,9 +2488,11 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 2488 | 2488 | }); |
| 2489 | 2489 | |
| 2490 | 2490 | router.add("GET", "/api/v1/repos/:owner/:name/forks", (ctx) => { |
| 2491 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 2491 | 2492 | const repo = loadRepo(ctx); |
| 2492 | 2493 | if (!repo) return notFound(); |
| 2493 | return json(visibleForks(ctx.app.db, repo.id, ctx.auth.user)); | |
| 2494 | const forks = timePhase(ctx.request, "forks", () => visibleForks(ctx.app.db, repo.id, ctx.auth.user)); | |
| 2495 | return timePhase(ctx.request, "render", () => json(forks)); | |
| 2494 | 2496 | }); |
| 2495 | 2497 | |
| 2496 | 2498 | router.add("POST", "/api/v1/repos/:owner/:name/forks", async (ctx) => { |
test/forks-api-timing.test.ts added +53
| @@ -0,0 +1,53 @@ | ||
| 1 | /** The forks API list names its phases in Server-Timing. */ | |
| 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 { serve, type Servers } from "../src/http/server.ts"; | |
| 8 | import { seedAdmin } from "./helpers/admin.ts"; | |
| 9 | ||
| 10 | let dataDir = ""; | |
| 11 | let app: App, servers: Servers, bearer = ""; | |
| 12 | ||
| 13 | beforeAll(async () => { | |
| 14 | dataDir = await mkdtemp(join(tmpdir(), "forge-fkapi-")); | |
| 15 | app = createApp({ dataDir, port: 0 }); | |
| 16 | servers = await serve(app); | |
| 17 | await seedAdmin(app, { username: "alice", email: "[email protected]", password: "correct-horse-battery" }); | |
| 18 | const minted = await fetch(`${servers.url}/api/v1/tokens`, { | |
| 19 | method: "POST", | |
| 20 | headers: { "content-type": "application/json" }, | |
| 21 | body: JSON.stringify({ adminConfirm: true, username: "alice", password: "correct-horse-battery", name: "cli", scope: "admin" }), | |
| 22 | }); | |
| 23 | bearer = ((await minted.json()) as { token: string }).token; | |
| 24 | }); | |
| 25 | ||
| 26 | afterAll(async () => { | |
| 27 | servers.stop(); | |
| 28 | app.close(); | |
| 29 | await rm(dataDir, { recursive: true, force: true }); | |
| 30 | }); | |
| 31 | ||
| 32 | const names = (res: Response) => | |
| 33 | (res.headers.get("server-timing") ?? "").split(", ").map((p) => p.split(";")[0]!).filter((n) => n !== "app"); | |
| 34 | ||
| 35 | test("the forks list names session, forks, render in order", async () => { | |
| 36 | const made = await fetch(`${servers.url}/api/v1/repos`, { | |
| 37 | method: "POST", | |
| 38 | headers: { authorization: `Bearer ${bearer}`, "content-type": "application/json" }, | |
| 39 | body: JSON.stringify({ name: "yard" }), | |
| 40 | }); | |
| 41 | expect(made.status).toBe(201); | |
| 42 | const res = await fetch(`${servers.url}/api/v1/repos/alice/yard/forks`, { headers: { authorization: `Bearer ${bearer}` } }); | |
| 43 | await res.arrayBuffer(); | |
| 44 | expect(res.status).toBe(200); | |
| 45 | expect(names(res)).toEqual(["session", "forks", "render"]); | |
| 46 | }); | |
| 47 | ||
| 48 | test("a refused forks read still names session", async () => { | |
| 49 | const res = await fetch(`${servers.url}/api/v1/repos/alice/nope/forks`, { headers: { authorization: `Bearer ${bearer}` } }); | |
| 50 | await res.arrayBuffer(); | |
| 51 | expect(res.status).toBe(404); | |
| 52 | expect(names(res)).toContain("session"); | |
| 53 | }); |
new above
offline