Public: anyone can read this; the crew can change it.
An issues list finds the open pulls that close its rows from their numbers and bodies alone, not 200 whole pulls #6338
scout/forge:scout/6324-an-issues-list-finds-the-open-pulls-that → main 1 commit fork
3 files
changed +100
−4
src/core/pulls.ts +24 −3
| @@ -1111,14 +1111,35 @@ export function mergedPullClosing(db: Database, repoId: string, number: number): | ||
| 1111 | 1111 | export function openPullsClosing( |
| 1112 | 1112 | db: Database, |
| 1113 | 1113 | repoId: string, |
| 1114 | numbers?: Iterable<number>, | |
| 1114 | 1115 | ): Map<number, Pull> { |
| 1116 | const wanted = numbers === undefined ? undefined : new Set(numbers); | |
| 1115 | 1117 | const out = new Map<number, Pull>(); |
| 1116 | for (const pull of listPulls(db, repoId, { sealed: true, state: "open", limit: 200 })) | |
| 1117 | for (const n of closesFromBody(pull.body)) | |
| 1118 | if (!out.has(n)) out.set(n, pull); | |
| 1118 | if (wanted?.size === 0) return out; | |
| 1119 | // Numbers and bodies only; a Pull is built for the ones that close a wanted number. | |
| 1120 | const rows = db | |
| 1121 | .query<{ number: number; body: string }, [string]>( | |
| 1122 | `SELECT issue.number AS number, issue.body AS body FROM issue | |
| 1123 | WHERE issue.repo_id = ?1 AND issue.kind = 'pull' AND issue.state = 'open' | |
| 1124 | ORDER BY issue.number DESC LIMIT 200`, | |
| 1125 | ) | |
| 1126 | .all(repoId); | |
| 1127 | for (const row of rows) { | |
| 1128 | const closes = closesFromBody(row.body).filter( | |
| 1129 | (n) => !out.has(n) && (wanted === undefined || wanted.has(n)), | |
| 1130 | ); | |
| 1131 | if (closes.length === 0) continue; | |
| 1132 | pullsHydrated.count++; | |
| 1133 | const pull = getPull(db, repoId, row.number); | |
| 1134 | if (pull === undefined) continue; | |
| 1135 | for (const n of closes) out.set(n, pull); | |
| 1136 | } | |
| 1119 | 1137 | return out; |
| 1120 | 1138 | } |
| 1121 | 1139 | |
| 1140 | /** How many pulls `openPullsClosing` has built: a counter for tests to assert work done. */ | |
| 1141 | export const pullsHydrated = { count: 0 }; | |
| 1142 | ||
| 1122 | 1143 | /** |
| 1123 | 1144 | * The person whose words a pull answers: the author of the issue it closes |
| 1124 | 1145 | * (or continues), when that is a person. Derived from the body and the issue |
src/web/studio/lists.ts +1 −1
| @@ -348,7 +348,7 @@ export async function tellsList(ctx: Ctx): Promise<Response> { | ||
| 348 | 348 | ); |
| 349 | 349 | notePhase(ctx.request, "issues", performance.now() - at); |
| 350 | 350 | at = performance.now(); |
| 351 | const closingPulls = openPullsClosing(db, repo.id); | |
| 351 | const closingPulls = openPullsClosing(db, repo.id, fetched.map((i) => i.number)); | |
| 352 | 352 | knowMergeClosings( |
| 353 | 353 | wordCtx, |
| 354 | 354 | repo.id, |
test/pulls-closing.test.ts added +75
| @@ -0,0 +1,75 @@ | ||
| 1 | /** | |
| 2 | * `openPullsClosing` reads numbers and bodies of the open pulls and builds a | |
| 3 | * `Pull` only for those that close a number it was asked about (#6324). | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { createApp, type App } from "../src/app.ts"; | |
| 10 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 11 | import { createIssue } from "../src/core/issues.ts"; | |
| 12 | import { openPullsClosing, pullsHydrated } from "../src/core/pulls.ts"; | |
| 13 | import type { Repo } from "../src/core/repos.ts"; | |
| 14 | import { getUserByName } from "../src/core/users.ts"; | |
| 15 | import { adminCookie } from "./helpers/admin.ts"; | |
| 16 | import { quietLogs } from "./helpers/globals.ts"; | |
| 17 | ||
| 18 | quietLogs(); | |
| 19 | ||
| 20 | let dataDir = ""; | |
| 21 | let app: App; | |
| 22 | let repo: Repo; | |
| 23 | const issues: number[] = []; | |
| 24 | ||
| 25 | beforeAll(async () => { | |
| 26 | dataDir = await mkdtemp(join(tmpdir(), "forge-pulls-closing-")); | |
| 27 | app = createApp({ dataDir, port: 0 }); | |
| 28 | await adminCookie(app, { username: "alice", email: "[email protected]", password: "a-long-closing-passphrase" }); | |
| 29 | const alice = getUserByName(app.db, "alice")!; | |
| 30 | const made = await createRepo(app, alice, { name: "garden", visibility: "public" }); | |
| 31 | if (!made.ok) throw new Error(String(made.error)); | |
| 32 | repo = made.value; | |
| 33 | await commitToBranch(app, alice, repo, { message: "seed", files: [{ path: "README.md", content: "# garden\n" }] }); | |
| 34 | for (let i = 0; i < 50; i++) { | |
| 35 | const made = createIssue(app.db, { repoId: repo.id, authorId: alice.id, title: `Thing ${i}` }); | |
| 36 | if (!made.ok) throw new Error(String(made.error)); | |
| 37 | issues.push(made.value.number); | |
| 38 | } | |
| 39 | // 150 open pulls; only the first three close an issue on the page. | |
| 40 | for (let i = 0; i < 150; i++) { | |
| 41 | const body = i < 3 ? `Closes #${issues[i * 10]}` : ""; | |
| 42 | const pull = createIssue(app.db, { repoId: repo.id, authorId: alice.id, title: `Pull ${i}`, body, kind: "pull" }); | |
| 43 | if (!pull.ok) throw new Error(String(pull.error)); | |
| 44 | app.db | |
| 45 | .query( | |
| 46 | `INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, head_repo_id) | |
| 47 | VALUES (?1, 'main', ?2, 'a', 'b', ?3)`, | |
| 48 | ) | |
| 49 | .run(pull.value.id, `b/${i}`, repo.id); | |
| 50 | } | |
| 51 | }); | |
| 52 | ||
| 53 | afterAll(async () => { | |
| 54 | app.close?.(); | |
| 55 | await rm(dataDir, { recursive: true, force: true }); | |
| 56 | }); | |
| 57 | ||
| 58 | test("only the pulls that close a number on the page are hydrated", () => { | |
| 59 | const before = pullsHydrated.count; | |
| 60 | const map = openPullsClosing(app.db, repo.id, issues); | |
| 61 | expect([...map.keys()].sort((a, b) => a - b)).toEqual([issues[0]!, issues[10]!, issues[20]!]); | |
| 62 | expect(new Set([...map.values()].map((p) => p.number)).size).toBe(3); | |
| 63 | expect(pullsHydrated.count - before).toBe(3); | |
| 64 | }); | |
| 65 | ||
| 66 | test("an issue off the page is not looked for, and no numbers means none", () => { | |
| 67 | const before = pullsHydrated.count; | |
| 68 | expect([...openPullsClosing(app.db, repo.id, [issues[0]!]).keys()]).toEqual([issues[0]!]); | |
| 69 | expect(pullsHydrated.count - before).toBe(1); | |
| 70 | expect(openPullsClosing(app.db, repo.id, []).size).toBe(0); | |
| 71 | }); | |
| 72 | ||
| 73 | test("without numbers every closed number is found, as before", () => { | |
| 74 | expect(openPullsClosing(app.db, repo.id).size).toBe(3); | |
| 75 | }); |
new above
offline