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 #6341

Closed opened this
Closed without merging

scout/forge:scout/6324-an-issues-list-finds-the-open-pulls-that → main 4 commits fork 3 threads · 3 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
3 files changed +141 −4
src/core/pulls.ts +25 −3
@@ -1111,11 +1111,33 @@ export function mergedPullClosing(db: Database, repoId: string, number: number):
11111111 export function openPullsClosing(
11121112 db: Database,
11131113 repoId: string,
1114 numbers?: Iterable<number>,
11141115 ): Map<number, Pull> {
1116 const wanted = numbers === undefined ? undefined : new Set(numbers);
11151117 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<{ id: string; number: number; body: string }, [string]>(
1122 `SELECT issue.id AS id, issue.number AS number, issue.body AS body FROM issue JOIN pull ON pull.issue_id = issue.id
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 const chosen = new Map<string, { number: number; closes: number[] }>();
1128 const taken = new Set<number>();
1129 for (const row of rows) {
1130 const closes = closesFromBody(row.body).filter(
1131 (n) => !taken.has(n) && (wanted === undefined || wanted.has(n)),
1132 );
1133 if (closes.length === 0) continue;
1134 for (const n of closes) taken.add(n);
1135 chosen.set(row.id, { number: row.number, closes });

taken is set here, before joinMany has confirmed a pull row exists. A pull-less issue row would shadow an older real closer. Join pull in the SELECT, or only mark taken after hydration.

1136 }
1137 if (chosen.size === 0) return out;
1138 const issues = [...getIssuesByIds(db, [...chosen.keys()]).values()];
1139 for (const pull of joinMany(db, issues))
1140 for (const n of chosen.get(pull.id)!.closes) out.set(n, pull);
11191141 return out;
11201142 }
11211143
src/web/studio/lists.ts +1 −1
@@ -348,7 +348,7 @@ export async function tellsList(ctx: Ctx): Promise<Response> {
348348 );
349349 notePhase(ctx.request, "issues", performance.now() - at);
350350 at = performance.now();
351 const closingPulls = openPullsClosing(db, repo.id);
351 const closingPulls = openPullsClosing(db, repo.id, fetched.map((i) => i.number));

This is correct, but it is issues-list speed work. Nothing in #6263, #5930, #6217 or #5994 asks for it under FLOW ONLY. Hold it until flow is clear, or link it to the flow issue it serves along with the evidence.

352352 knowMergeClosings(
353353 wordCtx,
354354 repo.id,
test/pulls-closing.test.ts added +115
@@ -0,0 +1,115 @@
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 (pull #6341, issue #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 } 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 /** The pull rows `openPullsClosing` reads through `db`: what it built, not how long it took. */
21 function pullRowsRead(run: (db: App["db"]) => void): number {
22 let rows = 0;
23 const spy = new Proxy(app.db, {
24 get(target, key) {
25 if (key !== "query") return Reflect.get(target, key, target);
26 return (sql: string) => {
27 const q = target.query(sql);
28 if (!/FROM pull WHERE issue_id IN/.test(sql)) return q;
29 return new Proxy(q, {
30 get: (t, k) =>
31 k === "all"
32 ? (...args: never[]) => {
33 const found = (t.all as (...a: never[]) => unknown[])(...args);
34 rows += found.length;
35 return found;
36 }
37 : Reflect.get(t, k, t),
38 });
39 };
40 },
41 });
42 run(spy);
43 return rows;
44 }
45
46 let dataDir = "";
47 let app: App;
48 let repo: Repo;
49 const issues: number[] = [];
50
51 beforeAll(async () => {
52 dataDir = await mkdtemp(join(tmpdir(), "forge-pulls-closing-"));
53 app = createApp({ dataDir, port: 0 });
54 await adminCookie(app, { username: "alice", email: "[email protected]", password: "a-long-closing-passphrase" });
55 const alice = getUserByName(app.db, "alice")!;
56 const made = await createRepo(app, alice, { name: "garden", visibility: "public" });
57 if (!made.ok) throw new Error(String(made.error));
58 repo = made.value;
59 await commitToBranch(app, alice, repo, { message: "seed", files: [{ path: "README.md", content: "# garden\n" }] });
60 for (let i = 0; i < 50; i++) {
61 const made = createIssue(app.db, { repoId: repo.id, authorId: alice.id, title: `Thing ${i}` });
62 if (!made.ok) throw new Error(String(made.error));
63 issues.push(made.value.number);
64 }
65 // 150 open pulls; only the first three close an issue on the page.
66 for (let i = 0; i < 150; i++) {
67 const body = i < 3 ? `Closes #${issues[i * 10]}` : "";
68 const pull = createIssue(app.db, { repoId: repo.id, authorId: alice.id, title: `Pull ${i}`, body, kind: "pull" });
69 if (!pull.ok) throw new Error(String(pull.error));
70 app.db
71 .query(
72 `INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, head_repo_id)
73 VALUES (?1, 'main', ?2, 'a', 'b', ?3)`,
74 )
75 .run(pull.value.id, `b/${i}`, repo.id);
76 }
77 });
78
79 afterAll(async () => {
80 app.close?.();
81 await rm(dataDir, { recursive: true, force: true });
82 });
83
84 test("only the pulls that close a number on the page are hydrated", () => {
85 let map = new Map<number, { number: number }>();
86 const rows = pullRowsRead((db) => (map = openPullsClosing(db, repo.id, issues)));
87 expect([...map.keys()].sort((a, b) => a - b)).toEqual([issues[0]!, issues[10]!, issues[20]!]);
88 expect(new Set([...map.values()].map((p) => p.number)).size).toBe(3);
89 expect(rows).toBe(3);
90 });
91
92 test("an issue off the page is not looked for, and no numbers means none", () => {
93 let map = new Map<number, unknown>();
94 const rows = pullRowsRead((db) => (map = openPullsClosing(db, repo.id, [issues[0]!])));
95 expect([...map.keys()]).toEqual([issues[0]!]);
96 expect(rows).toBe(1);
97 expect(openPullsClosing(app.db, repo.id, []).size).toBe(0);
98 });
99
100 test("without numbers every closed number is found, as before", () => {
101 expect(openPullsClosing(app.db, repo.id).size).toBe(3);
102 });
103
104 test("an open pull-kind issue with no pull row does not hide an older real closer", () => {
105 const alice = getUserByName(app.db, "alice")!;
106 const target = issues[40]!;
107 const real = createIssue(app.db, { repoId: repo.id, authorId: alice.id, title: "Real", body: `Closes #${target}`, kind: "pull" });
108 if (!real.ok) throw new Error(String(real.error));
109 app.db
110 .query(`INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, head_repo_id) VALUES (?1, 'main', 'b/real', 'a', 'b', ?2)`)
111 .run(real.value.id, repo.id);
112 const bare = createIssue(app.db, { repoId: repo.id, authorId: alice.id, title: "Bare", body: `Closes #${target}`, kind: "pull" });
113 if (!bare.ok) throw new Error(String(bare.error));
114 expect(openPullsClosing(app.db, repo.id, [target]).get(target)?.number).toBe(real.value.number);
115 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs