Public: anyone can read this; the crew can change it.
The API's commits and branches reads name their Server-Timing phases: session, git, render #3887
scout/forge:scout/3789-the-api-s-commits-and-branches-reads-nam → main 2 commits fork
2 files
changed +89
−14
src/http/api.ts +21 −14
| @@ -253,6 +253,7 @@ import { appLimitWords, appsOwnedBy } from "../actions/app_limit.ts"; | ||
| 253 | 253 | import { getQuota } from "../settings/quotas.ts"; |
| 254 | 254 | import { isSafeRef, isSafeTreePath } from "../git/guard.ts"; |
| 255 | 255 | import { listBranches } from "../git/branches.ts"; |
| 256 | import { notePhase, takeSessionTime, timePhase, timePhaseAsync } from "../core/route_timing.ts"; | |
| 256 | 257 | import { |
| 257 | 258 | appView, |
| 258 | 259 | appVisitors, |
| @@ -2606,19 +2607,23 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 2606 | 2607 | // protected — the three facts a branch list is asked for. `ahead`/`behind` |
| 2607 | 2608 | // are null when the repo has more branches than the comparison cap. |
| 2608 | 2609 | router.add("GET", "/api/v1/repos/:owner/:name/branches", async (ctx) => { |
| 2610 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 2609 | 2611 | const repo = loadRepo(ctx); |
| 2610 | 2612 | if (!repo) return notFound(); |
| 2611 | const listed = await listBranches(dirOf(ctx, repo), repo.defaultBranch); | |
| 2612 | if (!listed.ok) return json([]); | |
| 2613 | return json( | |
| 2614 | listed.value.branches.map((b) => ({ | |
| 2615 | name: b.name, | |
| 2616 | oid: b.oid, | |
| 2617 | isDefault: b.isDefault, | |
| 2618 | ahead: b.ahead, | |
| 2619 | behind: b.behind, | |
| 2620 | protected: b.isDefault && repo.protectDefault, | |
| 2621 | })), | |
| 2613 | const listed = await timePhaseAsync(ctx.request, "git", () => | |
| 2614 | listBranches(dirOf(ctx, repo), repo.defaultBranch), | |
| 2615 | ); | |
| 2616 | return timePhase(ctx.request, "render", () => | |
| 2617 | json( | |
| 2618 | !listed.ok ? [] : listed.value.branches.map((b) => ({ | |
| 2619 | name: b.name, | |
| 2620 | oid: b.oid, | |
| 2621 | isDefault: b.isDefault, | |
| 2622 | ahead: b.ahead, | |
| 2623 | behind: b.behind, | |
| 2624 | protected: b.isDefault && repo.protectDefault, | |
| 2625 | })), | |
| 2626 | ), | |
| 2622 | 2627 | ); |
| 2623 | 2628 | }); |
| 2624 | 2629 | |
| @@ -2675,6 +2680,7 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 2675 | 2680 | }); |
| 2676 | 2681 | |
| 2677 | 2682 | router.add("GET", "/api/v1/repos/:owner/:name/commits", async (ctx) => { |
| 2683 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 2678 | 2684 | const repo = loadRepo(ctx); |
| 2679 | 2685 | if (!repo) return notFound(); |
| 2680 | 2686 | const ref = ctx.url.searchParams.get("ref") || repo.defaultBranch; |
| @@ -2683,9 +2689,10 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 2683 | 2689 | Math.max(Number(ctx.url.searchParams.get("limit")) || 30, 1), |
| 2684 | 2690 | 100, |
| 2685 | 2691 | ); |
| 2686 | const log = await commitLog(dirOf(ctx, repo), ref, { limit }); | |
| 2687 | if (!log.ok) return json([]); | |
| 2688 | return json(log.value); | |
| 2692 | const log = await timePhaseAsync(ctx.request, "git", () => | |
| 2693 | commitLog(dirOf(ctx, repo), ref, { limit }), | |
| 2694 | ); | |
| 2695 | return timePhase(ctx.request, "render", () => json(log.ok ? log.value : [])); | |
| 2689 | 2696 | }); |
| 2690 | 2697 | |
| 2691 | 2698 | router.add("GET", "/api/v1/repos/:owner/:name/commits/:oid", async (ctx) => { |
test/api-git-timing.test.ts added +68
| @@ -0,0 +1,68 @@ | ||
| 1 | /** The API's commits and branches reads name session, git and render in Server-Timing (#3789). */ | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { mkdtempSync, rmSync } from "node:fs"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createRepo } from "../src/actions/repos.ts"; | |
| 7 | import { createApp, type App } from "../src/app.ts"; | |
| 8 | import { createToken } from "../src/core/tokens.ts"; | |
| 9 | import { createUser } from "../src/core/users.ts"; | |
| 10 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 11 | import { proveAll } from "./helpers/verified.ts"; | |
| 12 | import { quietLogs } from "./helpers/globals.ts"; | |
| 13 | ||
| 14 | quietLogs(); | |
| 15 | ||
| 16 | let root = ""; | |
| 17 | let app: App; | |
| 18 | let servers: Servers; | |
| 19 | let token = ""; | |
| 20 | ||
| 21 | beforeAll(async () => { | |
| 22 | root = mkdtempSync(join(tmpdir(), "forge-api-git-timing-")); | |
| 23 | app = createApp({ dataDir: root, port: 0, forgeRepo: "alice/forge" }); | |
| 24 | const u = await createUser(app.db, { | |
| 25 | origin: "seed", | |
| 26 | trusted: true, | |
| 27 | username: "alice", | |
| 28 | email: "[email protected]", | |
| 29 | password: "a-good-passphrase", | |
| 30 | adminIfFirst: true, | |
| 31 | }); | |
| 32 | if (!u.ok) throw new Error(u.error); | |
| 33 | proveAll(app); | |
| 34 | const made = await createRepo(app, { id: u.value.id, username: "alice" } as never, { name: "forge" }); | |
| 35 | if (!made.ok) throw new Error(made.error); | |
| 36 | token = createToken(app.db, u.value.id, "read", "ro").token; | |
| 37 | servers = await serve(app); | |
| 38 | }); | |
| 39 | afterAll(() => { | |
| 40 | servers.stop(); | |
| 41 | app.close(); | |
| 42 | rmSync(root, { recursive: true, force: true }); | |
| 43 | }); | |
| 44 | ||
| 45 | const get = (path: string) => | |
| 46 | fetch(`${servers.url}/api/v1/repos/alice/${path}`, { headers: { authorization: `Bearer ${token}` } }); | |
| 47 | const names = (res: Response) => | |
| 48 | (res.headers.get("server-timing") ?? "").split(", ").map((p) => p.split(";")[0]); | |
| 49 | ||
| 50 | for (const route of ["branches", "commits"]) { | |
| 51 | test(`GET /api/v1/repos/:owner/:name/${route} names session, git and render`, async () => { | |
| 52 | const res = await get(`forge/${route}`); | |
| 53 | expect(res.status).toBe(200); | |
| 54 | expect(names(res)).toEqual(expect.arrayContaining(["app", "session", "git", "render"])); | |
| 55 | }); | |
| 56 | ||
| 57 | test(`a missing repo's ${route} still names session`, async () => { | |
| 58 | const res = await get(`nope/${route}`); | |
| 59 | expect(res.status).toBe(404); | |
| 60 | expect(names(res)).toContain("session"); | |
| 61 | expect(names(res)).not.toContain("git"); | |
| 62 | }); | |
| 63 | } | |
| 64 | ||
| 65 | test("a bad ref is still a 422", async () => { | |
| 66 | const res = await get("forge/commits?ref=" + encodeURIComponent("a..b")); | |
| 67 | expect(res.status).toBe(422); | |
| 68 | }); |
new above
offline