From b549d052f33f82e8265190b72ec3764c29f8392a Mon Sep 17 00:00:00 2001 From: repo-worker-code Date: Mon, 2 Nov 2026 17:54:55 -0600 Subject: [PATCH] fix(pm-stack): prs reconcile stream reads pulls-shaped data (#4528) The prs stream sourced the issues endpoint, whose PR objects carry no draft/merged/merged_at, so first-sight PRs were stored draft=0/merged=0 forever. Source the stream from repos/{repo}/pulls instead; pulls rows also heal previously materialized false-default rows via the store's null-preserving update. Derive merged from merged_at when the pulls LIST shape omits the merged boolean. Regression tests for draft-first-sight, merged-first-sight and heal-after-gap. --- pm-stack/projector/src/projector.ts | 6 +- pm-stack/projector/src/reconcile.ts | 31 +++++++-- pm-stack/projector/test/reconcile.test.ts | 81 +++++++++++++++++++++++ 3 files changed, 112 insertions(+), 6 deletions(-) diff --git a/pm-stack/projector/src/projector.ts b/pm-stack/projector/src/projector.ts index bb6bbba78d..469c9a265d 100644 --- a/pm-stack/projector/src/projector.ts +++ b/pm-stack/projector/src/projector.ts @@ -52,6 +52,10 @@ function fromIssueOrPr(repo: string, obj: Record, kind: "issue" | " // emit null so the store preserves the stored values instead of // resetting them to defaults (#4158). const carriesMergeFields = "merged" in obj || "merged_at" in obj || "draft" in obj; + // The pulls LIST endpoint carries merged_at but no `merged` boolean + // (only the single-PR get and webhook payloads carry both), so derive + // merged from merged_at when the key is absent (#4528). + const merged = carriesMergeFields ? (obj.merged ?? (obj.merged_at != null) ? 1 : 0) : null; return { ref: itemRef(repo, number), repo, @@ -63,7 +67,7 @@ function fromIssueOrPr(repo: string, obj: Record, kind: "issue" | " state: obj.state === "closed" ? "closed" : "open", state_reason: iso(obj.state_reason), draft: carriesMergeFields ? (obj.draft ? 1 : 0) : null, - merged: carriesMergeFields ? (obj.merged ? 1 : 0) : null, + merged, author: iso(obj.user?.login), labels: jstr(labels), assignees: jstr(assignees), diff --git a/pm-stack/projector/src/reconcile.ts b/pm-stack/projector/src/reconcile.ts index b774fc6f02..7614160d7f 100644 --- a/pm-stack/projector/src/reconcile.ts +++ b/pm-stack/projector/src/reconcile.ts @@ -48,15 +48,13 @@ async function reconcileStream( return null; }; - if (stream === "issues" || stream === "prs") { + if (stream === "issues") { const path = `repos/${repo}/issues?${q}&filter=all`; const rows = await listResource>(auth, path, fetchFn); let cursor: string | null = null; for (const obj of rows) { - const isPr = Boolean(obj.pull_request); - if (stream === "prs" && !isPr) continue; - if (stream === "issues" && isPr) continue; - const row = fromIssueOrPr(repo, obj, isPr ? "pr" : "issue"); + if (Boolean(obj.pull_request)) continue; + const row = fromIssueOrPr(repo, obj, "issue"); await store.upsertItem(db, row); await mark(1); const u = obj.updated_at ?? obj.created_at; @@ -65,6 +63,29 @@ async function reconcileStream( return { repo, stream, written: written.n, cursor }; } + if (stream === "prs") { + // The issues endpoint never returns draft/merged/merged_at for its + // pull_request-link objects, so sourcing PR upserts from it stores + // false defaults on first sight (#4528). The pulls endpoint is + // pulls-shaped — it carries all three — so the prs stream reads it + // instead; non-null merge fields also heal previously materialized + // false-default rows via the store's null-preserving update. + // NB: unlike the issues endpoint, pulls does not accept `filter`. + const pq = since + ? `since=${encodeURIComponent(since)}&per_page=100&state=all` + : "per_page=100&state=all"; + const prs = await listResource>(auth, `repos/${repo}/pulls?${pq}`, fetchFn); + let cursor: string | null = null; + for (const pr of prs) { + const row = fromIssueOrPr(repo, pr, "pr"); + await store.upsertItem(db, row); + await mark(1); + const u = pr.updated_at ?? pr.created_at; + if (typeof u === "string" && (!cursor || u > cursor)) cursor = u; + } + return { repo, stream, written: written.n, cursor }; + } + if (stream === "issue_comments" || stream === "review_comments") { const kind = stream === "issue_comments" ? "issue_comment" : "review_comment"; // The repo-wide collections are /issues/comments and /pulls/comments — diff --git a/pm-stack/projector/test/reconcile.test.ts b/pm-stack/projector/test/reconcile.test.ts index c291279b47..e141e8153f 100644 --- a/pm-stack/projector/test/reconcile.test.ts +++ b/pm-stack/projector/test/reconcile.test.ts @@ -167,3 +167,84 @@ describe("reconcileRepos reviews stream per-PR guard (#2745)", () => { expect(await getSyncCursor(db, repo, "reviews")).toBe("2026-10-06T00:00:00Z"); }); }); + +describe("reconcileRepos prs stream sources pulls-shaped data (#4528)", () => { + const repo = "developing-today/prs-pulls-shape"; + // Pulls LIST objects carry draft + merged_at but no `merged` boolean. + const pullsPr = (n: number, shape: { draft: boolean; merged_at: string | null; updated: string }) => ({ + number: n, + html_url: `https://github.com/${repo}/pull/${n}`, + url: `https://api.github.com/repos/${repo}/pulls/${n}`, + title: `pr ${n}`, + state: shape.merged_at ? "closed" : "open", + user: { login: "drew" }, + labels: [], + assignees: [], + created_at: "2026-10-01T00:00:00Z", + updated_at: shape.updated, + closed_at: shape.merged_at, + draft: shape.draft, + merged_at: shape.merged_at, + }); + const fetchFnFor = (prs: Record[]) => + (async (url: string) => { + if (url.includes("/pulls?")) return jsonRes(prs); + return jsonRes([]); + }) as unknown as typeof fetch; + + test("draft-first-sight stores draft=1, merged-first-sight stores merged=1 + merged_at", async () => { + const result = await reconcileRepos( + db, + { PM_REPOS: repo, GITHUB_TOKEN: "test-token" }, + fetchFnFor([ + pullsPr(7, { draft: true, merged_at: null, updated: "2026-10-02T00:00:00Z" }), + pullsPr(8, { draft: false, merged_at: "2026-10-03T00:00:00Z", updated: "2026-10-03T00:00:00Z" }), + ]), + ); + expect(result.ok).toBe(true); + const prs = (result.streams as Record[]).find((s) => s.stream === "prs"); + expect(prs?.error).toBeUndefined(); + expect(prs?.written).toBe(2); + const items = await getItems(db, [`${repo}#7`, `${repo}#8`]); + const byNumber = Object.fromEntries(items.map((i) => [i.number, i])); + expect(byNumber[7].draft).toBe(1); + expect(byNumber[7].merged).toBe(0); + expect(byNumber[7].merged_at).toBeNull(); + expect(byNumber[8].draft).toBe(0); + expect(byNumber[8].merged).toBe(1); + expect(byNumber[8].merged_at).toBe("2026-10-03T00:00:00Z"); + }); + + test("heal-after-gap: false-default row is corrected on a later pulls pass", async () => { + // Simulate the old behavior: an issue-shaped PR object (no merge keys) + // materialized draft=0/merged=0/merged_at=null on first sight. + const issueShaped = { + number: 9, + html_url: `https://github.com/${repo}/pull/9`, + url: `https://api.github.com/repos/${repo}/issues/9`, + title: "pr 9", + state: "closed", + user: { login: "drew" }, + labels: [], + assignees: [], + pull_request: { url: `https://api.github.com/repos/${repo}/pulls/9` }, + created_at: "2026-10-01T00:00:00Z", + updated_at: "2026-10-02T00:00:00Z", + }; + const { fromIssueOrPr } = await import("../src/projector"); + const { upsertItem } = await import("../src/store"); + await upsertItem(db, fromIssueOrPr(repo, issueShaped, "pr")); + const before = (await getItems(db, [`${repo}#9`]))[0]; + expect([before.draft, before.merged, before.merged_at]).toEqual([0, 0, null]); + + await reconcileRepos( + db, + { PM_REPOS: repo, GITHUB_TOKEN: "test-token" }, + fetchFnFor([pullsPr(9, { draft: false, merged_at: "2026-10-04T00:00:00Z", updated: "2026-10-04T00:00:00Z" })]), + ); + const after = (await getItems(db, [`${repo}#9`]))[0]; + expect(after.draft).toBe(0); + expect(after.merged).toBe(1); + expect(after.merged_at).toBe("2026-10-04T00:00:00Z"); + }); +});