import { repoDb } from "../scoped-db";
import { loadRepoById } from "../repos";
import { notifyQuietly, quiet } from "./deliver";
import { accountAdmins } from "./recipients";
/** Tells whoever started a failed import or fork; falls back to the owning account's admins. */
async function notifyProvisioningFailedUnsafe(repoId: string, reason: string): Promise<void> {
const repo = await loadRepoById(repoId);
if (!repo) return;
const started = await repoDb(repoId).first<{ actor_id: string | null; kind: string }>(
`SELECT actor_id, kind FROM activity WHERE repo_id = ? AND kind IN ('repo_imported', 'repo_forked')
ORDER BY created_at DESC LIMIT 1`,
repoId,
);
const recipients = started?.actor_id ? [started.actor_id] : await accountAdmins(repo.owner_id);
const what = started?.kind === "repo_forked" ? "Fork" : "Import";
await notifyQuietly({
category: "import",
repoId,
accountId: null,
access: "read",
recipients,
title: `${what} of ${repo.owner_name}/${repo.name} failed`,
details: [`Reason: ${reason}.`, "You can delete the repository and try again."],
url: `/${repo.owner_name}/${repo.name}`,
});
}
export const notifyProvisioningFailed = quiet("notifyProvisioningFailed", notifyProvisioningFailedUnsafe);