import { run } from "./db"; import { repoDb } from "./scoped-db"; /** 1 when the comment bound as the parameter exists and is not hidden, else 0: what deleting it takes off a count. */ export const VISIBLE_COMMENT = "(SELECT COUNT(*) FROM comments WHERE id = ? AND hidden_at IS NULL)"; /** * Moves `repos.open_issues_count` / `open_pulls_count` by `delta` for one issue, unless the issue is hidden: hiding an * open issue already took it out of the counter, so later state changes must not move it. The issue lives in the * repository's object and the counter in the D1 directory, so this is a read there and a write here. */ export async function adjustOpenCounter( issue: { id: string; repo_id: string; is_pull: number }, delta: number, ): Promise { const hidden = await repoDb(issue.repo_id).first<{ ok: number }>( "SELECT 1 AS ok FROM issues WHERE id = ? AND hidden_at IS NOT NULL", issue.id, ); if (hidden) return; const counter = issue.is_pull === 1 ? "open_pulls_count" : "open_issues_count"; await run(`UPDATE repos SET ${counter} = MAX(${counter} + ?, 0) WHERE id = ?`, delta, issue.repo_id); }