Skip to content

Commit 44e11de

Browse files
fix(review): only count real D1 writes in backfillContributorGateHistory inserted (#7804)
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent ead55ca commit 44e11de

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

src/review/contributor-gate-history-backfill.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,14 @@ export async function backfillContributorGateHistory(env: Env, opts: { limit?: n
8282
continue;
8383
}
8484
try {
85-
await env.DB.prepare(
85+
const result = await env.DB.prepare(
8686
`INSERT INTO contributor_gate_history (id, login, source, project, target_id, decision, head_sha, created_at)
8787
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
8888
ON CONFLICT(id) DO NOTHING`,
8989
)
9090
.bind(`contrib:${login}:${c.source}:${c.targetId}@${c.headSha ?? "none"}`, login, c.source, c.project, c.targetId, c.decision, c.headSha, c.createdAt)
9191
.run();
92-
inserted += 1;
92+
if (result.meta.changes > 0) inserted += 1;
9393
} catch (error) {
9494
console.warn(JSON.stringify({ event: "contributor_gate_history_backfill_write_error", project: c.project, targetId: c.targetId, message: errorMessage(error).slice(0, 200) }));
9595
}

test/unit/contributor-gate-history-backfill.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,36 @@ describe("backfillContributorGateHistory (#fairness-analytics)", () => {
149149
warn.mockRestore();
150150
});
151151

152+
it("REGRESSION: does not count an ON CONFLICT DO NOTHING no-op toward inserted", async () => {
153+
const candidate = {
154+
project: "owner/repo",
155+
targetId: "owner/repo#50",
156+
decision: "merge",
157+
headSha: "sha50",
158+
source: "gittensory-native",
159+
authorLogin: "octocat",
160+
createdAt: "2026-06-25T12:00:00.000Z",
161+
};
162+
const env = {
163+
DB: {
164+
prepare: (sql: string) => ({
165+
bind: (..._args: unknown[]) => {
166+
if (/FROM review_audit/.test(sql)) {
167+
return { all: async () => ({ results: [candidate] }) };
168+
}
169+
if (/INSERT INTO contributor_gate_history/.test(sql)) {
170+
return { run: async () => ({ meta: { changes: 0 } }) };
171+
}
172+
throw new Error(`unexpected sql: ${sql}`);
173+
},
174+
}),
175+
},
176+
} as unknown as Env;
177+
178+
const result = await backfillContributorGateHistory(env);
179+
expect(result).toEqual({ scanned: 1, inserted: 0, skippedNoAuthor: 0, hasMore: false });
180+
});
181+
152182
it("is best-effort per row: a write failure on one candidate is logged and does not stop the rest of the batch", async () => {
153183
const env = createTestEnv();
154184
await insertPullRequest(env, "owner/repo", 20, "octocat");

0 commit comments

Comments
 (0)