import { all, batch, first, run, stmt } from "./db"; import { loadIssue, setIssueState } from "./issues"; import type { IssueRow } from "./issues"; import { emitWebhook, publicUrl } from "./events/emit"; import { assertBelowCap } from "./limits"; import type { RepoRow } from "~/lib/types"; const CLOSING_KEYWORDS = /\b(?:close[sd]?|fix(?:e[sd])?|resolve[sd]?)\s+#(\d+)\b/gi; const MAX_KEYWORD_LINKS = 10; export const MAX_LINKED_ISSUES = 20; export type LinkedIssue = { number: number; title: string; state: "open" | "closed"; source: "manual" | "keyword" }; /** Re-derives keyword links ("fixes #12") from a pull request body; manual links are left untouched. */ export async function syncKeywordLinks(pullIssueId: string, repoId: string, body: string): Promise { const numbers = [...new Set([...body.matchAll(CLOSING_KEYWORDS)].map((match) => Number(match[1])))].slice( 0, MAX_KEYWORD_LINKS, ); const issues = (await Promise.all(numbers.map((number) => loadIssue(repoId, number)))).filter( (issue): issue is IssueRow => issue !== null && issue.is_pull === 0, ); const now = Date.now(); await batch([ stmt("DELETE FROM pull_issue_links WHERE pull_issue_id = ? AND source = 'keyword'", pullIssueId), ...issues.map((issue) => stmt( `INSERT OR IGNORE INTO pull_issue_links (pull_issue_id, issue_id, source, created_by, created_at) VALUES (?, ?, 'keyword', NULL, ?)`, pullIssueId, issue.id, now, ), ), ]); } export async function linkIssue(pullIssueId: string, issueId: string, actorId: string): Promise { const count = await first<{ count: number }>( "SELECT COUNT(*) AS count FROM pull_issue_links WHERE pull_issue_id = ?", pullIssueId, ); assertBelowCap(count?.count ?? 0, MAX_LINKED_ISSUES, "linked issues per pull request"); await run( `INSERT INTO pull_issue_links (pull_issue_id, issue_id, source, created_by, created_at) VALUES (?, ?, 'manual', ?, ?) ON CONFLICT (pull_issue_id, issue_id) DO UPDATE SET source = 'manual', created_by = excluded.created_by`, pullIssueId, issueId, actorId, Date.now(), ); } export async function unlinkIssue(pullIssueId: string, issueId: string): Promise { await run("DELETE FROM pull_issue_links WHERE pull_issue_id = ? AND issue_id = ?", pullIssueId, issueId); } export async function listLinkedIssues(pullIssueId: string): Promise { return all( `SELECT i.number, i.title, i.state, l.source FROM pull_issue_links l JOIN issues i ON i.id = l.issue_id WHERE l.pull_issue_id = ? ORDER BY i.number LIMIT ?`, pullIssueId, MAX_LINKED_ISSUES, ); } /** Closes linked issues once a pull request lands on the default branch, mirroring GitHub. */ export async function closeLinkedIssues(repo: RepoRow, pullIssueId: string, actorId: string | null, sha: string) { const rows = await all<{ number: number }>( `SELECT i.number FROM pull_issue_links l JOIN issues i ON i.id = l.issue_id WHERE l.pull_issue_id = ? AND i.state = 'open' AND i.repo_id = ? LIMIT ?`, pullIssueId, repo.id, MAX_LINKED_ISSUES, ); for (const row of rows) { const issue = await loadIssue(repo.id, row.number); if (!issue || issue.state !== "open") continue; await setIssueState(issue, "closed", actorId, { commit: sha }); await emitWebhook(repo.id, "issues", { action: "closed", issue: { number: issue.number, title: issue.title, html_url: publicUrl(`/${repo.owner_name}/${repo.name}/issues/${issue.number}`), }, }); } }