Public: anyone can read this; the crew can change it.
A repository's branches and tags answer `{ items, next_cursor }` #5058
scout/forge:scout/5041-a-repository-s-branches-and-tags-answer → main 1 commit fork
8 files
changed +168
−33
src/http/api.ts +38 −14
| @@ -192,6 +192,8 @@ import { | ||
| 192 | 192 | labelsForIssues, |
| 193 | 193 | listComments, |
| 194 | 194 | listIssues, |
| 195 | LIST_LIMIT, | |
| 196 | LIST_LIMIT_MAX, | |
| 195 | 197 | listLabels, |
| 196 | 198 | pageLimit, |
| 197 | 199 | type Issue, |
| @@ -1041,6 +1043,32 @@ function listQuery(ctx: Ctx) { | ||
| 1041 | 1043 | }; |
| 1042 | 1044 | } |
| 1043 | 1045 | |
| 1046 | /** | |
| 1047 | * One page of a ref list (branches, tags), in the order given: `limit` is a | |
| 1048 | * whole number from 1 to LIST_LIMIT_MAX (default LIST_LIMIT), `cursor` the | |
| 1049 | * `next_cursor` of the page before, which is the last ref's name. | |
| 1050 | */ | |
| 1051 | function refPage<T extends { name: string }>(ctx: Ctx, all: T[]): Response { | |
| 1052 | const q = ctx.url.searchParams; | |
| 1053 | const raw = q.get("limit"); | |
| 1054 | const limit = raw === null ? LIST_LIMIT : /^\d{1,4}$/.test(raw) ? Number(raw) : 0; | |
| 1055 | if (limit < 1 || limit > LIST_LIMIT_MAX) | |
| 1056 | return apiError("invalid_limit", 422, `limit is a whole number from 1 to ${LIST_LIMIT_MAX}`); | |
| 1057 | const cursor = q.get("cursor"); | |
| 1058 | let start = 0; | |
| 1059 | if (cursor !== null) { | |
| 1060 | const at = all.findIndex((r) => r.name === cursor); | |
| 1061 | if (at < 0) | |
| 1062 | return apiError("invalid_cursor", 422, "cursor is the next_cursor of a page before"); | |
| 1063 | start = at + 1; | |
| 1064 | } | |
| 1065 | const items = all.slice(start, start + limit); | |
| 1066 | return json({ | |
| 1067 | items, | |
| 1068 | next_cursor: start + limit < all.length ? items[items.length - 1]!.name : null, | |
| 1069 | }); | |
| 1070 | } | |
| 1071 | ||
| 1044 | 1072 | /** `?limit=&cursor=` for repo lists (the cursor is a repo id). */ |
| 1045 | 1073 | function repoQuery(ctx: Ctx) { |
| 1046 | 1074 | const q = ctx.url.searchParams; |
| @@ -2747,19 +2775,17 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 2747 | 2775 | const listed = await timePhaseAsync(ctx.request, "git", () => |
| 2748 | 2776 | listBranches(dirOf(ctx, repo), repo.defaultBranch), |
| 2749 | 2777 | ); |
| 2750 | if (!listed.ok) return timePhase(ctx.request, "render", () => json([])); | |
| 2751 | return timePhase(ctx.request, "render", () => | |
| 2752 | json( | |
| 2753 | listed.value.branches.map((b) => ({ | |
| 2778 | const items = listed.ok | |
| 2779 | ? listed.value.branches.map((b) => ({ | |
| 2754 | 2780 | name: b.name, |
| 2755 | 2781 | oid: b.oid, |
| 2756 | 2782 | isDefault: b.isDefault, |
| 2757 | 2783 | ahead: b.ahead, |
| 2758 | 2784 | behind: b.behind, |
| 2759 | 2785 | protected: b.isDefault && repo.protectDefault, |
| 2760 | })), | |
| 2761 | ), | |
| 2762 | ); | |
| 2786 | })) | |
| 2787 | : []; | |
| 2788 | return timePhase(ctx.request, "render", () => refPage(ctx, items)); | |
| 2763 | 2789 | }); |
| 2764 | 2790 | |
| 2765 | 2791 | router.add("POST", "/api/v1/repos/:owner/:name/branches", async (ctx) => { |
| @@ -2806,17 +2832,15 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 2806 | 2832 | const refs = await timePhaseAsync(ctx.request, "git", () => |
| 2807 | 2833 | listRefs(dirOf(ctx, repo), "refs/tags/"), |
| 2808 | 2834 | ); |
| 2809 | if (!refs.ok) return timePhase(ctx.request, "render", () => json([])); | |
| 2810 | return timePhase(ctx.request, "render", () => | |
| 2811 | json( | |
| 2812 | refs.value.map((r) => ({ | |
| 2835 | const items = refs.ok | |
| 2836 | ? refs.value.map((r) => ({ | |
| 2813 | 2837 | name: r.name.replace("refs/tags/", ""), |
| 2814 | 2838 | oid: r.oid, |
| 2815 | 2839 | // Annotated tags point at a tag object; this is the commit beneath. |
| 2816 | 2840 | commit: r.peeled ?? r.oid, |
| 2817 | })), | |
| 2818 | ), | |
| 2819 | ); | |
| 2841 | })) | |
| 2842 | : []; | |
| 2843 | return timePhase(ctx.request, "render", () => refPage(ctx, items)); | |
| 2820 | 2844 | }); |
| 2821 | 2845 | |
| 2822 | 2846 | router.add("GET", "/api/v1/repos/:owner/:name/commits", async (ctx) => { |
test/api-branches-timing.test.ts +1 −1
| @@ -57,7 +57,7 @@ test("the branches list names session, git, render in order", async () => { | ||
| 57 | 57 | const got = await phases("/api/v1/repos/alice/r/branches"); |
| 58 | 58 | expect(got.status).toBe(200); |
| 59 | 59 | expect(got.names).toEqual(["session", "git", "render"]); |
| 60 | expect(Array.isArray(JSON.parse(got.body))).toBe(true); | |
| 60 | expect(Array.isArray(JSON.parse(got.body).items)).toBe(true); | |
| 61 | 61 | }); |
| 62 | 62 | |
| 63 | 63 | test("a missing repository is a 404 naming session", async () => { |
test/api-tags-timing.test.ts +1 −1
| @@ -62,7 +62,7 @@ test("the API tags list names session, git and render in order", async () => { | ||
| 62 | 62 | const got = await phases("/api/v1/repos/alice/r/tags"); |
| 63 | 63 | expect(got.status).toBe(200); |
| 64 | 64 | expect(got.names).toEqual(["session", "git", "render"]); |
| 65 | expect(JSON.parse(got.body)).toEqual([]); | |
| 65 | expect(JSON.parse(got.body)).toEqual({ items: [], next_cursor: null }); | |
| 66 | 66 | }); |
| 67 | 67 | |
| 68 | 68 | test("a missing repository is a 404 that still names session", async () => { |
test/branches.test.ts +8 −4
| @@ -120,7 +120,9 @@ describe("branches on a native repo", () => { | ||
| 120 | 120 | }); |
| 121 | 121 | |
| 122 | 122 | const branches = async (): Promise<BranchRow[]> => |
| 123 | read<BranchRow[]>(await api("/api/v1/repos/alice/app/branches")); | |
| 123 | read<{ items: BranchRow[] }>(await api("/api/v1/repos/alice/app/branches")).then( | |
| 124 | (page) => page.items, | |
| 125 | ); | |
| 124 | 126 | |
| 125 | 127 | const create = (payload: unknown) => |
| 126 | 128 | api("/api/v1/repos/alice/app/branches", { |
| @@ -384,12 +386,14 @@ describe("branches on a mirror go through the origin", () => { | ||
| 384 | 386 | }); |
| 385 | 387 | |
| 386 | 388 | const localBranches = async (): Promise<BranchRow[]> => |
| 387 | read<BranchRow[]>(await api("/api/v1/repos/alice/gears/branches")); | |
| 389 | read<{ items: BranchRow[] }>( | |
| 390 | await api("/api/v1/repos/alice/gears/branches"), | |
| 391 | ).then((page) => page.items); | |
| 388 | 392 | |
| 389 | 393 | const upstreamBranches = async (): Promise<{ name: string }[]> => |
| 390 | read<{ name: string }[]>( | |
| 394 | read<{ items: { name: string }[] }>( | |
| 391 | 395 | await fetch(`${originServers.url}/api/v1/repos/octo/gears/branches`), |
| 392 | ); | |
| 396 | ).then((page) => page.items); | |
| 393 | 397 | |
| 394 | 398 | test("creating a branch lands it upstream, then here", async () => { |
| 395 | 399 | const created = await api("/api/v1/repos/alice/gears/branches", { |
test/edits-security.test.ts +7 −5
| @@ -363,9 +363,11 @@ describe("edits: refusals that must leave nothing behind", () => { | ||
| 363 | 363 | expect(b.ahead).toBeNull(); |
| 364 | 364 | expect(b.behind).toBeNull(); |
| 365 | 365 | } |
| 366 | const rows = await read<{ ahead: number | null }[]>( | |
| 367 | await api("/api/v1/repos/alice/app/branches"), | |
| 368 | ); | |
| 366 | const rows = ( | |
| 367 | await read<{ items: { ahead: number | null }[] }>( | |
| 368 | await api("/api/v1/repos/alice/app/branches?limit=200"), | |
| 369 | ) | |
| 370 | ).items; | |
| 369 | 371 | expect(rows.every((b) => b.ahead === null)).toBe(true); |
| 370 | 372 | }); |
| 371 | 373 | }); |
| @@ -411,9 +413,9 @@ describe("edits on a mirror: the origin's refusal is final", () => { | ||
| 411 | 413 | .split("\n") |
| 412 | 414 | .filter((r) => r !== ""); |
| 413 | 415 | const upstreamBranches = async (): Promise<string[]> => |
| 414 | read<{ name: string }[]>( | |
| 416 | read<{ items: { name: string }[] }>( | |
| 415 | 417 | await fetch(`${originServers.url}/api/v1/repos/octo/gears/branches`), |
| 416 | ).then((rows) => rows.map((b) => b.name).sort()); | |
| 418 | ).then(({ items }) => items.map((b) => b.name).sort()); | |
| 417 | 419 | |
| 418 | 420 | beforeAll(async () => { |
| 419 | 421 | dataDir = await mkdtemp(join(tmpdir(), "forge-mr-")); |
test/parity.test.ts +5 −4
| @@ -342,7 +342,7 @@ describe("parity: full control through actions and the API", () => { | ||
| 342 | 342 | const dir = join(app.reposRoot, "alice", "pub.git"); |
| 343 | 343 | expect(await git(["symbolic-ref", "HEAD"], dir)).toBe("refs/heads/feature"); |
| 344 | 344 | const branches = await call("GET", "/api/v1/repos/alice/pub/branches"); |
| 345 | expect(branches.list.find((b) => b.isDefault)?.name).toBe("feature"); | |
| 345 | expect((branches.json.items as Json[]).find((b) => b.isDefault)?.name).toBe("feature"); | |
| 346 | 346 | const back = await call("PATCH", "/api/v1/repos/alice/pub", { |
| 347 | 347 | token: aliceAdmin, |
| 348 | 348 | body: { defaultBranch: "main" }, |
| @@ -915,10 +915,11 @@ describe("parity: full control through actions and the API", () => { | ||
| 915 | 915 | await git(["tag", "light"], dir); |
| 916 | 916 | await git(["push", "origin", "--tags"], dir); |
| 917 | 917 | const tags = await call("GET", "/api/v1/repos/alice/pub/tags"); |
| 918 | expect(tags.list.map((t) => t.name).sort()).toEqual(["light", "v1"]); | |
| 918 | const tagItems = tags.json.items as Json[]; | |
| 919 | expect(tagItems.map((t) => t.name).sort()).toEqual(["light", "v1"]); | |
| 919 | 920 | const head = await git(["rev-parse", "HEAD"], dir); |
| 920 | for (const t of tags.list) expect(t.commit).toBe(head); | |
| 921 | expect(tags.list.find((t) => t.name === "v1")?.oid).not.toBe(head); | |
| 921 | for (const t of tagItems) expect(t.commit).toBe(head); | |
| 922 | expect(tagItems.find((t) => t.name === "v1")?.oid).not.toBe(head); | |
| 922 | 923 | |
| 923 | 924 | const commits = await call("GET", "/api/v1/repos/alice/pub/commits"); |
| 924 | 925 | const oid = commits.list[0]!.oid as string; |
test/read-api.test.ts +3 −4
| @@ -69,12 +69,11 @@ describe("read-back API (contents, branches, commits)", () => { | ||
| 69 | 69 | }); |
| 70 | 70 | |
| 71 | 71 | test("branches lists heads with the default flagged", async () => { |
| 72 | const branches = (await ( | |
| 72 | const { items: branches } = (await ( | |
| 73 | 73 | await api("/api/v1/repos/dev/lib/branches") |
| 74 | 74 | ).json()) as { |
| 75 | name: string; | |
| 76 | isDefault: boolean; | |
| 77 | }[]; | |
| 75 | items: { name: string; isDefault: boolean }[]; | |
| 76 | }; | |
| 78 | 77 | const names = branches.map((b) => b.name).sort(); |
| 79 | 78 | expect(names).toEqual(["feature", "main"]); |
| 80 | 79 | expect(branches.find((b) => b.name === "main")?.isDefault).toBe(true); |
test/ref-pages.test.ts added +105
| @@ -0,0 +1,105 @@ | ||
| 1 | /** Branches and tags answer `{ items, next_cursor }`, paged by `limit` and `cursor` (#5041). */ | |
| 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-ref-pages-")); | |
| 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 | const dir = join(app.reposRoot, "alice", "r.git"); | |
| 46 | const head = ( | |
| 47 | await Bun.spawn(["git", "rev-parse", "HEAD"], { cwd: dir, stdout: "pipe" }).stdout.text() | |
| 48 | ).trim(); | |
| 49 | for (const n of ["a", "b", "c", "d", "e"]) { | |
| 50 | for (const ref of [`refs/heads/x/${n}`, `refs/tags/t${n}`]) { | |
| 51 | await Bun.spawn(["git", "update-ref", ref, head], { cwd: dir }).exited; | |
| 52 | } | |
| 53 | } | |
| 54 | }); | |
| 55 | ||
| 56 | afterAll(async () => { | |
| 57 | await servers.stop(); | |
| 58 | app.close(); | |
| 59 | if (cells === undefined) delete process.env["FORGE_CELLS"]; | |
| 60 | else process.env["FORGE_CELLS"] = cells; | |
| 61 | await rm(dataDir, { recursive: true, force: true }); | |
| 62 | }); | |
| 63 | ||
| 64 | type Page = { items: { name: string }[]; next_cursor: string | null }; | |
| 65 | const get = (path: string) => fetch(`${servers.url}/api/v1/repos/alice/r/${path}`, { headers: { cookie } }); | |
| 66 | ||
| 67 | for (const kind of ["branches", "tags"] as const) { | |
| 68 | test(`${kind} answer { items, next_cursor }`, async () => { | |
| 69 | const res = await get(kind); | |
| 70 | expect(res.status).toBe(200); | |
| 71 | const page = (await res.json()) as Page; | |
| 72 | expect(Array.isArray(page.items)).toBe(true); | |
| 73 | expect(page.items.length).toBeGreaterThanOrEqual(5); | |
| 74 | expect(page.next_cursor).toBeNull(); | |
| 75 | }); | |
| 76 | ||
| 77 | test(`${kind} page with limit and next_cursor to the end`, async () => { | |
| 78 | const all = ((await (await get(kind)).json()) as Page).items.map((i) => i.name); | |
| 79 | const seen: string[] = []; | |
| 80 | let cursor: string | null = null; | |
| 81 | let pages = 0; | |
| 82 | do { | |
| 83 | const res = await get(`${kind}?limit=2${cursor === null ? "" : `&cursor=${encodeURIComponent(cursor)}`}`); | |
| 84 | expect(res.status).toBe(200); | |
| 85 | const page = (await res.json()) as Page; | |
| 86 | expect(page.items.length).toBeLessThanOrEqual(2); | |
| 87 | seen.push(...page.items.map((i) => i.name)); | |
| 88 | cursor = page.next_cursor; | |
| 89 | pages++; | |
| 90 | } while (cursor !== null && pages < 20); | |
| 91 | expect(seen).toEqual(all); | |
| 92 | expect(pages).toBe(Math.ceil(all.length / 2)); | |
| 93 | }); | |
| 94 | ||
| 95 | test(`${kind} refuse a bad limit or cursor`, async () => { | |
| 96 | for (const bad of ["0", "-1", "201", "abc", "1.5", ""]) { | |
| 97 | const res = await get(`${kind}?limit=${bad}`); | |
| 98 | expect(res.status).toBe(422); | |
| 99 | expect(((await res.json()) as { error: string }).error).toBe("invalid_limit"); | |
| 100 | } | |
| 101 | expect((await get(`${kind}?limit=200`)).status).toBe(200); | |
| 102 | const res = await get(`${kind}?cursor=no-such-ref`); | |
| 103 | expect(res.status).toBe(422); | |
| 104 | }); | |
| 105 | } |
new above
offline