Public: anyone can read this; the crew can change it.
The API's read of one issue names its Server-Timing phases: session, issue, comments, labels, render #3777
scout/forge:scout/3766-the-api-s-read-of-one-issue-names-its-se → main 6 commits fork
2 files
changed +58
−14
src/http/api.ts +17 −14
| @@ -3086,22 +3086,25 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 3086 | 3086 | router.add("GET", "/api/v1/repos/:owner/:name/issues/:number", (ctx) => { |
| 3087 | 3087 | // Numbers are shared with pulls: a pull's number answers as the same |
| 3088 | 3088 | // shape, marked `kind: "pull"`, with the path where its diff lives (#3299). |
| 3089 | const found = loadConversation(ctx); | |
| 3089 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 3090 | const found = timePhase(ctx.request, "issue", () => loadConversation(ctx)); | |
| 3090 | 3091 | if ("error" in found) return found.error; |
| 3091 | 3092 | const { repo, issue } = found; |
| 3092 | return json({ | |
| 3093 | ...withLabels(ctx, issue), | |
| 3094 | ...(issue.kind === "pull" | |
| 3095 | ? { | |
| 3096 | pull: `/api/v1/repos/${repo.ownerName}/${repo.name}/pulls/${issue.number}`, | |
| 3097 | } | |
| 3098 | : {}), | |
| 3099 | comments: readerMarked( | |
| 3100 | ctx, | |
| 3101 | found.repo.id, | |
| 3102 | listComments(ctx.app.db, found.issue.id), | |
| 3103 | ), | |
| 3104 | }); | |
| 3093 | const comments = timePhase(ctx.request, "comments", () => | |
| 3094 | readerMarked(ctx, found.repo.id, listComments(ctx.app.db, found.issue.id)), | |
| 3095 | ); | |
| 3096 | const labelled = timePhase(ctx.request, "labels", () => withLabels(ctx, issue)); | |
| 3097 | return timePhase(ctx.request, "render", () => | |
| 3098 | json({ | |
| 3099 | ...labelled, | |
| 3100 | ...(issue.kind === "pull" | |
| 3101 | ? { | |
| 3102 | pull: `/api/v1/repos/${repo.ownerName}/${repo.name}/pulls/${issue.number}`, | |
| 3103 | } | |
| 3104 | : {}), | |
| 3105 | comments, | |
| 3106 | }), | |
| 3107 | ); | |
| 3105 | 3108 | }); |
| 3106 | 3109 | |
| 3107 | 3110 | // What an issue waits for, and what waits for it. |
test/api-issue-timing.test.ts added +41
| @@ -0,0 +1,41 @@ | ||
| 1 | /** The API's read of one issue names its Server-Timing phases, found or not. */ | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { snappyFixture, type SnappyFixture } from "../bench/snappy.ts"; | |
| 4 | import { addComment, getIssue } from "../src/core/issues.ts"; | |
| 5 | ||
| 6 | let f: SnappyFixture; | |
| 7 | ||
| 8 | beforeAll(async () => { | |
| 9 | f = await snappyFixture({ repos: 1, issuesPerRepo: 4 }); | |
| 10 | const repo = f.repos[0]!; | |
| 11 | const issue = getIssue(f.app.db, repo.id, 2)!; | |
| 12 | addComment(f.app.db, { issueId: issue.id, authorId: f.owner.id, body: "first" }); | |
| 13 | }, 120_000); | |
| 14 | afterAll(async () => { | |
| 15 | await f.close(); | |
| 16 | }); | |
| 17 | ||
| 18 | async function read(n: number) { | |
| 19 | const repo = f.repos[0]!; | |
| 20 | const res = await fetch(`${f.base}/api/v1/repos/${repo.ownerName}/${repo.name}/issues/${n}`, { | |
| 21 | headers: { cookie: f.ownerCookie }, | |
| 22 | }); | |
| 23 | const body = (await res.json()) as { comments: unknown[] }; | |
| 24 | const names = (res.headers.get("server-timing") ?? "").split(", ").map((p) => p.split(";")[0]); | |
| 25 | return { res, body, names }; | |
| 26 | } | |
| 27 | ||
| 28 | test("an issue with comments names session, issue, comments, labels and render", async () => { | |
| 29 | const { res, body, names } = await read(2); | |
| 30 | expect(res.status).toBe(200); | |
| 31 | for (const n of ["session", "issue", "comments", "labels", "render"]) expect(names).toContain(n); | |
| 32 | expect(body.comments.length).toBe(1); | |
| 33 | }); | |
| 34 | ||
| 35 | test("a missing issue still names session and issue", async () => { | |
| 36 | const { res, names } = await read(99); | |
| 37 | expect(res.status).toBe(404); | |
| 38 | expect(names).toContain("session"); | |
| 39 | expect(names).toContain("issue"); | |
| 40 | expect(names).not.toContain("render"); | |
| 41 | }); |
new above
offline