Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/review/contributor-gate-history-backfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ export async function backfillContributorGateHistory(env: Env, opts: { limit?: n
continue;
}
try {
await env.DB.prepare(
const result = await env.DB.prepare(
`INSERT INTO contributor_gate_history (id, login, source, project, target_id, decision, head_sha, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(id) DO NOTHING`,
)
.bind(`contrib:${login}:${c.source}:${c.targetId}@${c.headSha ?? "none"}`, login, c.source, c.project, c.targetId, c.decision, c.headSha, c.createdAt)
.run();
inserted += 1;
if (result.meta.changes > 0) inserted += 1;
} catch (error) {
console.warn(JSON.stringify({ event: "contributor_gate_history_backfill_write_error", project: c.project, targetId: c.targetId, message: errorMessage(error).slice(0, 200) }));
}
Expand Down
30 changes: 30 additions & 0 deletions test/unit/contributor-gate-history-backfill.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,36 @@ describe("backfillContributorGateHistory (#fairness-analytics)", () => {
warn.mockRestore();
});

it("REGRESSION: does not count an ON CONFLICT DO NOTHING no-op toward inserted", async () => {
const candidate = {
project: "owner/repo",
targetId: "owner/repo#50",
decision: "merge",
headSha: "sha50",
source: "gittensory-native",
authorLogin: "octocat",
createdAt: "2026-06-25T12:00:00.000Z",
};
const env = {
DB: {
prepare: (sql: string) => ({
bind: (..._args: unknown[]) => {
if (/FROM review_audit/.test(sql)) {
return { all: async () => ({ results: [candidate] }) };
}
if (/INSERT INTO contributor_gate_history/.test(sql)) {
return { run: async () => ({ meta: { changes: 0 } }) };
}
throw new Error(`unexpected sql: ${sql}`);
},
}),
},
} as unknown as Env;

const result = await backfillContributorGateHistory(env);
expect(result).toEqual({ scanned: 1, inserted: 0, skippedNoAuthor: 0, hasMore: false });
});

it("is best-effort per row: a write failure on one candidate is logged and does not stop the rest of the batch", async () => {
const env = createTestEnv();
await insertPullRequest(env, "owner/repo", 20, "octocat");
Expand Down