import { repoDb } from "../scoped-db";
import { loadRepoById } from "../repos";
import { notifyQuietly, quiet } from "./deliver";
const TERMINAL = new Set(["applied", "suggested", "failed"]);
const COPY: Record<string, string> = {
applied: "resolved the conflicts and pushed a merge commit",
suggested: "has suggestions ready for review",
failed: "could not fix the conflicts",
};
/** Tells the user who started a merge fix how it ended. */
async function notifyMergeFixResultUnsafe(repoId: string, runId: string, status: string | undefined): Promise<void> {
if (!status || !TERMINAL.has(status)) return;
const row = await repoDb(repoId).first<{
repo_id: string;
actor_id: string;
error: string | null;
number: number;
title: string;
}>(
`SELECT m.repo_id, m.actor_id, m.error, i.number, i.title FROM mergefix_runs m JOIN issues i ON i.id = m.pull_issue_id
WHERE m.id = ?`,
runId,
);
const repo = row ? await loadRepoById(row.repo_id) : null;
if (!row || !repo) return;
await notifyQuietly({
category: "mergefix",
repoId: repo.id,
accountId: null,
access: "read",
recipients: [row.actor_id],
title: `Merge fix ${status} on ${repo.owner_name}/${repo.name}#${row.number}`,
details: [`The AI merge fix ${COPY[status]} for "${row.title}".`, ...(row.error ? [row.error] : [])],
url: `/${repo.owner_name}/${repo.name}/pull/${row.number}`,
});
}
export const notifyMergeFixResult = quiet("notifyMergeFixResult", notifyMergeFixResultUnsafe);