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

A repository's branches and tags answer `{ items, next_cursor }` #5058

Parked opened this
Closed without merging

scout/forge:scout/5041-a-repository-s-branches-and-tags-answer → main 1 commit fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
8 files changed +168 −33
src/http/api.ts +38 −14
@@ -192,6 +192,8 @@ import {
192192 labelsForIssues,
193193 listComments,
194194 listIssues,
195 LIST_LIMIT,
196 LIST_LIMIT_MAX,
195197 listLabels,
196198 pageLimit,
197199 type Issue,
@@ -1041,6 +1043,32 @@ function listQuery(ctx: Ctx) {
10411043 };
10421044 }
10431045
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
10441072 /** `?limit=&cursor=` for repo lists (the cursor is a repo id). */
10451073 function repoQuery(ctx: Ctx) {
10461074 const q = ctx.url.searchParams;
@@ -2747,19 +2775,17 @@ export function apiRoutes(router: Router<Handler>): void {
27472775 const listed = await timePhaseAsync(ctx.request, "git", () =>
27482776 listBranches(dirOf(ctx, repo), repo.defaultBranch),
27492777 );
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) => ({
27542780 name: b.name,
27552781 oid: b.oid,
27562782 isDefault: b.isDefault,
27572783 ahead: b.ahead,
27582784 behind: b.behind,
27592785 protected: b.isDefault && repo.protectDefault,
2760 })),
2761 ),
2762 );
2786 }))
2787 : [];
2788 return timePhase(ctx.request, "render", () => refPage(ctx, items));
27632789 });
27642790
27652791 router.add("POST", "/api/v1/repos/:owner/:name/branches", async (ctx) => {
@@ -2806,17 +2832,15 @@ export function apiRoutes(router: Router<Handler>): void {
28062832 const refs = await timePhaseAsync(ctx.request, "git", () =>
28072833 listRefs(dirOf(ctx, repo), "refs/tags/"),
28082834 );
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) => ({
28132837 name: r.name.replace("refs/tags/", ""),
28142838 oid: r.oid,
28152839 // Annotated tags point at a tag object; this is the commit beneath.
28162840 commit: r.peeled ?? r.oid,
2817 })),
2818 ),
2819 );
2841 }))
2842 : [];
2843 return timePhase(ctx.request, "render", () => refPage(ctx, items));
28202844 });
28212845
28222846 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 () => {
5757 const got = await phases("/api/v1/repos/alice/r/branches");
5858 expect(got.status).toBe(200);
5959 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);
6161 });
6262
6363 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 () => {
6262 const got = await phases("/api/v1/repos/alice/r/tags");
6363 expect(got.status).toBe(200);
6464 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 });
6666 });
6767
6868 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", () => {
120120 });
121121
122122 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 );
124126
125127 const create = (payload: unknown) =>
126128 api("/api/v1/repos/alice/app/branches", {
@@ -384,12 +386,14 @@ describe("branches on a mirror go through the origin", () => {
384386 });
385387
386388 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);
388392
389393 const upstreamBranches = async (): Promise<{ name: string }[]> =>
390 read<{ name: string }[]>(
394 read<{ items: { name: string }[] }>(
391395 await fetch(`${originServers.url}/api/v1/repos/octo/gears/branches`),
392 );
396 ).then((page) => page.items);
393397
394398 test("creating a branch lands it upstream, then here", async () => {
395399 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", () => {
363363 expect(b.ahead).toBeNull();
364364 expect(b.behind).toBeNull();
365365 }
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;
369371 expect(rows.every((b) => b.ahead === null)).toBe(true);
370372 });
371373 });
@@ -411,9 +413,9 @@ describe("edits on a mirror: the origin's refusal is final", () => {
411413 .split("\n")
412414 .filter((r) => r !== "");
413415 const upstreamBranches = async (): Promise<string[]> =>
414 read<{ name: string }[]>(
416 read<{ items: { name: string }[] }>(
415417 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());
417419
418420 beforeAll(async () => {
419421 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", () => {
342342 const dir = join(app.reposRoot, "alice", "pub.git");
343343 expect(await git(["symbolic-ref", "HEAD"], dir)).toBe("refs/heads/feature");
344344 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");
346346 const back = await call("PATCH", "/api/v1/repos/alice/pub", {
347347 token: aliceAdmin,
348348 body: { defaultBranch: "main" },
@@ -915,10 +915,11 @@ describe("parity: full control through actions and the API", () => {
915915 await git(["tag", "light"], dir);
916916 await git(["push", "origin", "--tags"], dir);
917917 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"]);
919920 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);
922923
923924 const commits = await call("GET", "/api/v1/repos/alice/pub/commits");
924925 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)", () => {
6969 });
7070
7171 test("branches lists heads with the default flagged", async () => {
72 const branches = (await (
72 const { items: branches } = (await (
7373 await api("/api/v1/repos/dev/lib/branches")
7474 ).json()) as {
75 name: string;
76 isDefault: boolean;
77 }[];
75 items: { name: string; isDefault: boolean }[];
76 };
7877 const names = branches.map((b) => b.name).sort();
7978 expect(names).toEqual(["feature", "main"]);
8079 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 }

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs