Public: anyone can read this; the crew can change it.
The API's following read names its Server-Timing phases: session, relation, chain, upstream, render #3930
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/3922-the-api-s-following-read-names-its-serve → main 2 commits fork
2 files
changed +102
−10
src/http/api.ts +20 −10
| @@ -2434,22 +2434,32 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 2434 | 2434 | // ── following. A fork's standing against its parent, read from git each |
| 2435 | 2435 | // time; taking updates is git's part, converging is the crew's. |
| 2436 | 2436 | router.add("GET", "/api/v1/repos/:owner/:name/following", async (ctx) => { |
| 2437 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 2437 | 2438 | const repo = loadRepo(ctx); |
| 2438 | 2439 | if (!repo) return notFound(); |
| 2439 | const relation = await relationOf(ctx.app, ctx.auth.user, repo); | |
| 2440 | const chain = await chainOfRepo(ctx.app, repo); | |
| 2441 | if (relation === undefined) | |
| 2442 | return chain === undefined | |
| 2443 | ? apiError("not_a_fork", 404, "this repository follows nothing") | |
| 2444 | : json({ chain: chain.lines, severedAt: chain.severedAt, upstream: await upstreamPayload(ctx.app) }); | |
| 2440 | const relation = await timePhaseAsync(ctx.request, "relation", () => | |
| 2441 | relationOf(ctx.app, ctx.auth.user, repo), | |
| 2442 | ); | |
| 2443 | const chain = await timePhaseAsync(ctx.request, "chain", () => chainOfRepo(ctx.app, repo)); | |
| 2444 | if (relation === undefined) { | |
| 2445 | if (chain === undefined) return apiError("not_a_fork", 404, "this repository follows nothing"); | |
| 2446 | const upstream = await timePhaseAsync(ctx.request, "upstream", () => upstreamPayload(ctx.app)); | |
| 2447 | return timePhase(ctx.request, "render", () => | |
| 2448 | json({ chain: chain.lines, severedAt: chain.severedAt, upstream }), | |
| 2449 | ); | |
| 2450 | } | |
| 2445 | 2451 | const converge = |
| 2446 | 2452 | relation.state === "diverged" && !relation.clean |
| 2447 | ? convergeState(ctx.app, repo, relation.parent) | |
| 2453 | ? timePhase(ctx.request, "converge", () => convergeState(ctx.app, repo, relation.parent)) | |
| 2448 | 2454 | : undefined; |
| 2449 | return json({ | |
| 2455 | const upstream = | |
| 2456 | chain === undefined | |
| 2457 | ? null | |
| 2458 | : await timePhaseAsync(ctx.request, "upstream", () => upstreamPayload(ctx.app)); | |
| 2459 | return timePhase(ctx.request, "render", () => json({ | |
| 2450 | 2460 | chain: chain?.lines ?? null, |
| 2451 | 2461 | severedAt: chain?.severedAt ?? null, |
| 2452 | upstream: chain === undefined ? null : await upstreamPayload(ctx.app), | |
| 2462 | upstream, | |
| 2453 | 2463 | parent: `${relation.parent.ownerName}/${relation.parent.name}`, |
| 2454 | 2464 | parentBranch: relation.parent.defaultBranch, |
| 2455 | 2465 | // A parent whose origin was disconnected: the counts below are against |
| @@ -2482,7 +2492,7 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 2482 | 2492 | ? converge.agent.user.username |
| 2483 | 2493 | : null, |
| 2484 | 2494 | }, |
| 2485 | }); | |
| 2495 | })); | |
| 2486 | 2496 | }); |
| 2487 | 2497 | |
| 2488 | 2498 | router.add( |
test/api-following-timing.test.ts added +82
| @@ -0,0 +1,82 @@ | ||
| 1 | /** The API's following read names its Server-Timing phases (#3922). */ | |
| 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, forkRepo } 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-following-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 | const fork = await forkRepo(app, alice, repo.value, "f"); | |
| 45 | if (!fork.ok) throw new Error(JSON.stringify(fork.error)); | |
| 46 | }); | |
| 47 | ||
| 48 | afterAll(async () => { | |
| 49 | await servers.stop(); | |
| 50 | app.close(); | |
| 51 | if (cells === undefined) delete process.env["FORGE_CELLS"]; | |
| 52 | else process.env["FORGE_CELLS"] = cells; | |
| 53 | await rm(dataDir, { recursive: true, force: true }); | |
| 54 | }); | |
| 55 | ||
| 56 | async function phases(path: string): Promise<{ status: number; names: string[]; body: string }> { | |
| 57 | const res = await fetch(`${servers.url}${path}`, { headers: { cookie } }); | |
| 58 | const body = await res.text(); | |
| 59 | const names = (res.headers.get("server-timing") ?? "").split(", ").map((p) => p.split(";")[0]!); | |
| 60 | return { status: res.status, names: names.slice(names.indexOf("app") + 1), body }; | |
| 61 | } | |
| 62 | ||
| 63 | test("the following read of a fork names session, relation, chain and render", async () => { | |
| 64 | const got = await phases("/api/v1/repos/alice/f/following"); | |
| 65 | expect(got.status).toBe(200); | |
| 66 | expect(got.names.slice(0, 3)).toEqual(["session", "relation", "chain"]); | |
| 67 | expect(got.names).toContain("render"); | |
| 68 | expect(JSON.parse(got.body).parent).toBe("alice/r"); | |
| 69 | }); | |
| 70 | ||
| 71 | test("a repository that follows nothing names session, relation and chain", async () => { | |
| 72 | const got = await phases("/api/v1/repos/alice/r/following"); | |
| 73 | expect(got.status).toBe(404); | |
| 74 | expect(got.names).toEqual(["session", "relation", "chain"]); | |
| 75 | expect(JSON.parse(got.body).error).toBe("not_a_fork"); | |
| 76 | }); | |
| 77 | ||
| 78 | test("a missing repository still names session", async () => { | |
| 79 | const got = await phases("/api/v1/repos/alice/nope/following"); | |
| 80 | expect(got.status).toBe(404); | |
| 81 | expect(got.names).toEqual(["session"]); | |
| 82 | }); |
new above
offline