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

A repository's forks, labels and webhooks answer `{ items, next_cursor }` #4853

Parked opened this
Closed without merging

scout/forge:scout/4843-a-repository-s-forks-labels-and-webhooks → main 3 commits fork 1 thread · 1 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
6 files changed +193 −13
src/http/api.ts +46 −6
@@ -1067,6 +1067,40 @@ function keyedWindow(ctx: Ctx): { limit: number; cursor: string | null } | Respo
10671067 return { limit, cursor };
10681068 }
10691069
1070 /**
1071 * `?limit=&cursor=` for a list read whole and cut here: limit is a whole
outdated

High: when the cursor is not in rows, findIndex returns -1, so start is 0 and the response is page one again. A stale or invented cursor should end the walk, either with an empty page or with a 422 invalid_cursor. It should not rewind. Add a test for an unknown cursor.

1072 * number from 1 to 200 (default 50), the cursor an id from a previous page.
1073 * Anything else is the 422 to answer.
1074 */
1075 function pageParams(
1076 ctx: Ctx,
1077 ): { limit: number; cursor: string | null } | Response {
1078 const q = ctx.url.searchParams;
1079 const rawLimit = q.get("limit");
1080 const limit =
1081 rawLimit === null ? 50 : /^\d{1,4}$/.test(rawLimit) ? Number(rawLimit) : 0;
1082 if (limit < 1 || limit > 200)
1083 return apiError("invalid_limit", 422, "limit is a whole number from 1 to 200");
1084 const cursor = q.get("cursor");
1085 if (cursor !== null && !/^[A-Za-z0-9_-]{1,64}$/.test(cursor))
1086 return apiError("invalid_cursor", 422, "cursor is the next_cursor of a previous page");
1087 return { limit, cursor };
1088 }
1089
1090 /** One page of `rows` (already in order) after the row whose id is the cursor. */
1091 function pageOf<T extends { id: string }>(
1092 rows: T[],
1093 page: { limit: number; cursor: string | null },
1094 ): { items: T[]; next_cursor: string | null } {
1095 // A cursor no row answers to (a deleted row, an invented id) ends the walk.
1096 const at = page.cursor === null ? -1 : rows.findIndex((r) => r.id === page.cursor);
1097 if (page.cursor !== null && at === -1) return { items: [], next_cursor: null };
1098 const start = at + 1;
1099 const items = rows.slice(start, start + page.limit);
1100 const more = start + page.limit < rows.length;
1101 return { items, next_cursor: more ? items[items.length - 1]!.id : null };
1102 }
1103
10701104 /** The page of `rows` (sorted by name) after the cursor: `{ items, next_cursor }`. */
10711105 function keyedPage<T>(
10721106 rows: T[],
@@ -2592,8 +2626,10 @@ export function apiRoutes(router: Router<Handler>): void {
25922626 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
25932627 const repo = loadRepo(ctx);
25942628 if (!repo) return notFound();
2595 const forks = timePhase(ctx.request, "forks", () => visibleForks(ctx.app.db, repo.id, ctx.auth.user));
2596 return timePhase(ctx.request, "render", () => json(forks));
2629 const page = pageParams(ctx);
2630 if (page instanceof Response) return page;
2631 const rows = timePhase(ctx.request, "forks", () => visibleForks(ctx.app.db, repo.id, ctx.auth.user));
2632 return timePhase(ctx.request, "render", () => json(pageOf(rows, page)));
25972633 });
25982634
25992635 router.add("POST", "/api/v1/repos/:owner/:name/forks", async (ctx) => {
@@ -3128,8 +3164,10 @@ export function apiRoutes(router: Router<Handler>): void {
31283164 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
31293165 const repo = loadRepo(ctx);
31303166 if (!repo) return notFound();
3131 const labels = timePhase(ctx.request, "labels", () => listLabels(ctx.app.db, repo.id));
3132 return timePhase(ctx.request, "render", () => json(labels));
3167 const page = pageParams(ctx);
3168 if (page instanceof Response) return page;
3169 const rows = timePhase(ctx.request, "labels", () => listLabels(ctx.app.db, repo.id));
3170 return timePhase(ctx.request, "render", () => json(pageOf(rows, page)));
31333171 });
31343172
31353173 router.add("POST", "/api/v1/repos/:owner/:name/labels", async (ctx) => {
@@ -4612,8 +4650,10 @@ export function apiRoutes(router: Router<Handler>): void {
46124650 if (denied) return denied;
46134651 const repo = loadRepo(ctx);
46144652 if (!repo || !canAdmin(ctx.app.db, repo, ctx.auth.user)) return notFound();
4615 const hooks = timePhase(ctx.request, "hooks", () => listWebhooks(ctx.app.db, repo.id));
4616 return timePhase(ctx.request, "render", () => json(hooks));
4653 const page = pageParams(ctx);
4654 if (page instanceof Response) return page;
4655 const rows = timePhase(ctx.request, "hooks", () => listWebhooks(ctx.app.db, repo.id));
4656 return timePhase(ctx.request, "render", () => json(pageOf(rows, page)));
46174657 });
46184658
46194659 router.add("POST", "/api/v1/repos/:owner/:name/hooks", async (ctx) => {
test/fork-counts.test.ts +1 −1
@@ -112,7 +112,7 @@ describe("fork counts hold only the forks the viewer may read", () => {
112112 const api = await fetch(`${base}/api/v1/repos/alice/lib/forks`, {
113113 headers: { cookie: cookies[who]! },
114114 });
115 expect(await api.json()).toEqual([]);
115 expect(await api.json()).toEqual({ items: [], next_cursor: null });
116116 }
117117 const owners = await page("/alice/lib/forks", "bob");
118118 expect(owners).toContain("1 fork");
test/list-pages.test.ts added +140
@@ -0,0 +1,140 @@
1 /**
2 * A repository's forks, labels and webhooks answer `{ items, next_cursor }`
3 * (#4843): paged by `limit` and `cursor`, a bad limit a 422, and each route
4 * keeps its own permission and filter.
5 */
6 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
7 import { mkdtemp, rm } from "node:fs/promises";
8 import { tmpdir } from "node:os";
9 import { join } from "node:path";
10 import { commitToBranch, createRepo, forkRepo, updateRepoSettings } from "../src/actions/repos.ts";
11 import { createApp, type App } from "../src/app.ts";
12 import { createLabel } from "../src/core/issues.ts";
13 import type { Repo } from "../src/core/repos.ts";
14 import { createToken } from "../src/core/tokens.ts";
15 import { createUser, type User } from "../src/core/users.ts";
16 import { createWebhook } from "../src/core/webhooks.ts";
17 import { serve, type Servers } from "../src/http/server.ts";
18 import { promote } from "./helpers/members.ts";
19
20 describe("forks, labels and webhooks are pages", () => {
21 let dataDir: string;
22 let app: App;
23 let servers: Servers;
24 let lib: Repo;
25 const tokens: Record<string, string> = {};
26
27 beforeAll(async () => {
28 dataDir = await mkdtemp(join(tmpdir(), "forge-list-pages-"));
29 app = createApp({ dataDir, port: 0 });
30 servers = await serve(app);
31 const users: Record<string, User> = {};
32 for (const [name, first] of [["alice", true], ["bob", false], ["carol", false], ["dave", false]] as const) {
33 const made = await createUser(app.db, {
34 origin: "seed",
35 trusted: true,
36 username: name,
37 email: `${name}@forge.local`,
38 password: "a-good-passphrase",
39 adminIfFirst: first,
40 });
41 if (!made.ok) throw new Error(made.error);
42 users[name] = made.value;
43 tokens[name] = createToken(app.db, made.value.id, "t", "admin").token;
44 }
45 promote(app.db, users["bob"]!, users["carol"]!);
46 const made = await createRepo(app, users["alice"]!, { name: "lib", visibility: "public" });
47 if (!made.ok) throw new Error(String(made.error));
48 lib = made.value;
49 const seeded = await commitToBranch(app, users["alice"]!, lib, {
50 message: "Start",
51 files: [{ path: "README.md", content: "# lib\n" }],
52 });
53 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
54 const bobs = await forkRepo(app, users["bob"]!, lib);
55 if (!bobs.ok) throw new Error(String(bobs.error));
56 const carols = await forkRepo(app, users["carol"]!, lib);
57 if (!carols.ok) throw new Error(String(carols.error));
58 const closed = await updateRepoSettings(app, users["carol"]!, carols.value, { visibility: "private" });
59 if (!closed.ok) throw new Error(String(closed.error));
60 for (const name of ["bug", "chore", "docs"]) createLabel(app.db, lib.id, name, "aabbcc");
61 for (const n of [1, 2, 3])
62 if (!createWebhook(app.db, { repoId: lib.id, url: `https://example.com/h${n}` }).ok)
63 throw new Error("hook");
64 }, 60_000);
65
66 afterAll(async () => {
67 servers.stop();
68 app.close();
69 await rm(dataDir, { recursive: true, force: true });
70 });
71
72 const get = async (path: string, who?: string) => {
73 const res = await fetch(`${servers.url}/api/v1/repos/alice/lib${path}`, {
74 headers: who ? { authorization: `Bearer ${tokens[who]}` } : {},
75 });
76 return { status: res.status, body: (await res.json()) as any };
77 };
78
79 test("forks: the shape, and a private fork is not shown to a stranger", async () => {
80 const stranger = await get("/forks", "dave");
81 expect(stranger.status).toBe(200);
82 expect(stranger.body.next_cursor).toBeNull();
83 expect(stranger.body.items.map((f: any) => f.ownerName)).toEqual(["bob"]);
84 const owner = await get("/forks", "carol");
85 expect(owner.body.items.map((f: any) => f.ownerName).sort()).toEqual(["bob", "carol"]);
86 });
87
88 test("forks: paging walks every fork once", async () => {
89 const first = await get("/forks?limit=1", "carol");
90 expect(first.body.items).toHaveLength(1);
91 expect(first.body.next_cursor).not.toBeNull();
92 const second = await get(`/forks?limit=1&cursor=${first.body.next_cursor}`, "carol");
93 expect(second.body.items).toHaveLength(1);
94 expect(second.body.next_cursor).toBeNull();
95 expect(second.body.items[0].id).not.toBe(first.body.items[0].id);
96 });
97
98 test("labels: the shape and paging", async () => {
99 const all = await get("/labels");
100 expect(all.body.items.map((l: any) => l.name)).toEqual(["bug", "chore", "docs"]);
101 expect(all.body.next_cursor).toBeNull();
102 const first = await get("/labels?limit=2");
103 expect(first.body.items.map((l: any) => l.name)).toEqual(["bug", "chore"]);
104 const rest = await get(`/labels?limit=2&cursor=${first.body.next_cursor}`);
105 expect(rest.body.items.map((l: any) => l.name)).toEqual(["docs"]);
106 expect(rest.body.next_cursor).toBeNull();
107 });
108
109 test("hooks: the shape, paging, admin only, and no secret", async () => {
110 const all = await get("/hooks", "alice");
111 expect(all.status).toBe(200);
112 expect(all.body.items).toHaveLength(3);
113 expect(JSON.stringify(all.body)).not.toContain("whsec");
114 expect(all.body.items[0]).not.toHaveProperty("secret");
115 const first = await get("/hooks?limit=2", "alice");
116 expect(first.body.items).toHaveLength(2);
117 const rest = await get(`/hooks?limit=2&cursor=${first.body.next_cursor}`, "alice");
118 expect(rest.body.items).toHaveLength(1);
119 expect(rest.body.next_cursor).toBeNull();
120 expect((await get("/hooks", "dave")).status).toBe(404);
121 expect((await get("/hooks")).status).toBeGreaterThanOrEqual(401);
122 });
123
124 test("a cursor no row answers to ends the walk with an empty page", async () => {
125 for (const [path, who] of [["/forks", "carol"], ["/labels", "alice"], ["/hooks", "alice"]] as const) {
126 const res = await get(`${path}?cursor=01notarealid`, who);
127 expect(res.status).toBe(200);
128 expect(res.body).toEqual({ items: [], next_cursor: null });
129 }
130 });
131
132 test("a bad limit or cursor is a 422 on all three", async () => {
133 for (const path of ["/forks", "/labels", "/hooks"])
134 for (const bad of ["limit=0", "limit=201", "limit=abc", "limit=-1", "limit=1.5", "cursor=a%20b"]) {
135 const res = await get(`${path}?${bad}`, "alice");
136 expect(res.status).toBe(422);
137 expect(res.body.error).toBe(bad.startsWith("cursor") ? "invalid_cursor" : "invalid_limit");
138 }
139 });
140 });
test/origins-sync.test.ts +2 −2
@@ -323,10 +323,10 @@ describe("origin sync: incremental, event-shaped, and polite", () => {
323323 // The second sweep sent If-None-Match and got a 304 back — and the labels
324324 // survived, because a 304 must serve the cached body, not an empty one.
325325 expect(fake.state.conditionalHits).toBeGreaterThan(hitsBefore);
326 const labels = await read<{ name: string }[]>(
326 const labels = await read<{ items: { name: string }[] }>(
327327 await api("/api/v1/repos/alice/sprockets/labels"),
328328 );
329 expect(labels.map((l) => l.name)).toEqual(["bug"]);
329 expect(labels.items.map((l) => l.name)).toEqual(["bug"]);
330330 });
331331
332332 test("the worker's due sweep is time-driven and bounded", async () => {
test/origins.test.ts +2 −2
@@ -418,8 +418,8 @@ describe("origins: connecting, mirroring, writing through", () => {
418418 // Labels, issues and pulls carry the origin's numbers and its authors.
419419 const labels = (await (
420420 await api("/api/v1/repos/alice/widgets/labels")
421 ).json()) as { name: string }[];
422 expect(labels.map((l) => l.name).sort()).toEqual(["bug", "chore"]);
421 ).json()) as { items: { name: string }[] };
422 expect(labels.items.map((l) => l.name).sort()).toEqual(["bug", "chore"]);
423423
424424 const issues = (await (
425425 await api("/api/v1/repos/alice/widgets/issues?state=all")
test/parity.test.ts +2 −2
@@ -392,7 +392,7 @@ describe("parity: full control through actions and the API", () => {
392392 });
393393 expect(queue.status).toBe(201);
394394 expect(
395 (await call("GET", "/api/v1/repos/alice/pub/labels")).list.map(
395 ((await call("GET", "/api/v1/repos/alice/pub/labels")).json.items as Json[]).map(
396396 (l) => l.name,
397397 ),
398398 ).toEqual(["bug", "queue"]);
@@ -710,7 +710,7 @@ describe("parity: full control through actions and the API", () => {
710710 );
711711 expect((detail.json.diff as Json).files).toHaveLength(1);
712712 const forks = await call("GET", "/api/v1/repos/alice/pub/forks");
713 expect(forks.list.map((f) => f.ownerName)).toEqual(["bob"]);
713 expect((forks.json.items as Json[]).map((f) => f.ownerName)).toEqual(["bob"]);
714714 });
715715
716716 test("numbers from another repo are not_found here", async () => {

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs