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

GET …/issues?fields=brief lists issues without bodies or comments #3379

Parked opened this
Closed without merging

scout/forge:scout/3365-get-issues-fields-brief-lists-issues-wit → main 3 commits fork 3 threads · 3 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
3 files changed +124 −1
src/http/api.ts +25 −1
@@ -563,6 +563,24 @@ function withLabelsMany<T extends Issue>(ctx: Ctx, issues: T[]) {
563563 }));
564564 }
outdated

Medium: for imported issues authorName is the user who connected the import, not the writer. Carry originAuthor (or prefer it here) so an imported name is never shown as a local account.

565565
566 /** An issue for a listing: what to pick from, without bodies. */
567 function briefIssue(i: ReturnType<typeof withLabelsMany>[number]) {
568 return {
569 number: i.number,
570 kind: i.kind,
571 title: i.title,
572 state: i.state,
573 labels: i.labels,
574 assignee: i.assigneeName,
575 author: i.originAuthor ?? i.authorName,
576 originAuthor: i.originAuthor,
577 ...(i.untrusted === undefined ? {} : { untrusted: i.untrusted }),
578 authorIsAgent: i.authorIsAgent,
579 createdAt: i.createdAt,
580 updatedAt: i.updatedAt,
581 };
582 }
583
566584 const NO_COUNTS: RepoCounts = { openIssues: 0, openPulls: 0 };
567585
568586 /**
@@ -2982,6 +3000,9 @@ export function apiRoutes(router: Router<Handler>): void {
29823000 router.add("GET", "/api/v1/repos/:owner/:name/issues", (ctx) => {
29833001 const repo = loadRepo(ctx);
29843002 if (!repo) return notFound();
3003 const fields = ctx.url.searchParams.get("fields");
3004 if (fields !== null && fields !== "brief")
3005 return apiError("invalid_fields", 422, "fields must be brief, or left out");
29853006 const query = listQuery(ctx);
29863007 const items = withLabelsMany(
29873008 ctx,
@@ -2991,7 +3012,10 @@ export function apiRoutes(router: Router<Handler>): void {
29913012 ...sealedScope(ctx.app.db, repo, ctx.auth.user),
29923013 }),
29933014 );
2994 return json(paged(items, query.limit, (i) => i.number));
3015 const page = paged(items, query.limit, (i) => i.number);
3016 return json(
3017 fields === "brief" ? { ...page, items: page.items.map(briefIssue) } : page,
3018 );
29953019 });
29963020
29973021 router.add("POST", "/api/v1/repos/:owner/:name/issues", async (ctx) => {
test/api-reader-marks.test.ts +17
@@ -189,6 +189,23 @@ describe("a reader's words over the API carry the brief's mark", () => {
189189 expect(listed.untrusted).toBe(READER);
190190 });
191191
192 test("a brief listing keeps the mark and shows an imported author as such", async () => {
193 const imported = ok(
194 openIssue(app, alice, repo, { title: "From elsewhere", body: "x" }),
195 );
196 app.db
197 .query("UPDATE issue SET origin_author = 'octocat' WHERE id = ?")
198 .run(imported.id);
199 const list = await get("/issues?fields=brief");
200 const byNumber = (n: number) =>
201 list.items.find((i: { number: number }) => i.number === n);
202 expect(byNumber(issueNumber).untrusted).toBe(READER);
203 expect(byNumber(issueNumber).originAuthor).toBeNull();
204 expect(byNumber(imported.number).untrusted).toBe(READER);
205 expect(byNumber(imported.number).author).toBe("octocat");
206 expect(byNumber(imported.number).originAuthor).toBe("octocat");
207 });
208
192209 test("a pull: its body, its verdicts and its line comments", async () => {
193210 const pull = await get(`/pulls/${pullNumber}`);
194211 expect(pull.untrusted).toBe(READER);
test/issues-brief.test.ts added +82
@@ -0,0 +1,82 @@
1 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
2 import { mkdtemp, rm } from "node:fs/promises";
3 import { tmpdir } from "node:os";
4 import { join } from "node:path";
5 import { createApp, type App } from "../src/app.ts";
6 import { serve, type Servers } from "../src/http/server.ts";
7 import { seedAdmin } from "./helpers/admin.ts";
8
9 describe("issues list ?fields=brief", () => {
10 let dataDir: string;
11 let app: App;
12 let servers: Servers;
13 let base: string;
14 let token = "";
15
16 const send = (path: string, method = "GET", bodyObj?: unknown) =>
17 fetch(`${base}${path}`, {
18 method,
19 headers: {
20 authorization: `Bearer ${token}`,
21 "content-type": "application/json",
22 },
23 ...(bodyObj === undefined ? {} : { body: JSON.stringify(bodyObj) }),
24 });
25
26 beforeAll(async () => {
27 dataDir = await mkdtemp(join(tmpdir(), "forge-brief-"));
28 app = createApp({ dataDir, port: 0 });
29 servers = await serve(app);
30 base = servers.url;
31 await seedAdmin(app, { username: "dev", email: "[email protected]", password: "brief-pass-1" });
32 token = await fetch(`${base}/api/v1/tokens`, {
33 method: "POST",
34 headers: { "content-type": "application/json" },
35 body: JSON.stringify({
36 adminConfirm: true,
37 username: "dev",
38 password: "brief-pass-1",
39 name: "c",
40 scope: "rw",
41 }),
42 })
43 .then((r) => r.json())
44 .then((j) => (j as { token: string }).token);
45 await send("/api/v1/repos", "POST", { name: "lib", visibility: "public" });
46 const body = "A long description of the problem. ".repeat(40);
47 for (let i = 0; i < 50; i++)
48 await send("/api/v1/repos/dev/lib/issues", "POST", { title: `Issue ${i}`, body });
49 });
50
51 afterAll(async () => {
52 await servers.stop();
53 await rm(dataDir, { recursive: true, force: true });
54 });
55
56 test("brief drops bodies and is under a fifth of the bytes", async () => {
57 const path = "/api/v1/repos/dev/lib/issues?limit=50";
58 const full = await (await send(path)).text();
59 const briefRes = await send(`${path}&fields=brief`);
60 expect(briefRes.status).toBe(200);
61 const brief = await briefRes.text();
62 const items = (JSON.parse(brief) as { items: Record<string, unknown>[] }).items;
63 expect(items).toHaveLength(50);
64 expect(JSON.parse(full).items[0].body).toBeString();
65 for (const item of items) {
66 expect(Object.keys(item).sort()).toEqual(
67 [
68 "assignee", "author", "authorIsAgent", "createdAt", "kind", "labels",
69 "number", "originAuthor", "state", "title", "updatedAt",
70 ].sort(),
71 );
72 }
73 expect(brief.length).toBeLessThan(full.length / 5);
74 });
75
76 test("any other fields value is a 422", async () => {
77 for (const v of ["full", "", "brief,body"]) {
78 const res = await send(`/api/v1/repos/dev/lib/issues?fields=${v}`);
79 expect(res.status).toBe(422);
80 }
81 });
82 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs