import { stmt } from "../db";
/**
* Integrations and legacy webhooks are configured by a repository's admins, so they do not survive a change
* of ownership: the new owner must knowingly re-enable them. Pull mirrors would otherwise keep writing refs as
* the old admin and push mirrors would keep exporting the new owner's pushes to the old admin's GitHub repo.
*/
export function repoOwnershipChangeStatements(repoId: string, actorId: string | null): D1PreparedStatement[] {
const now = Date.now();
return [
stmt(
`INSERT INTO integration_audit (id, integration_id, actor_id, action, detail, created_at)
SELECT lower(hex(randomblob(16))), id, ?, 'repo.transferred', ?, ? FROM integrations
WHERE scope_type = 'repo' AND scope_id = ? AND active = 1`,
actorId,
JSON.stringify({ reason: "Repository transferred; integration disabled until the new owner re-enables it" }),
now,
repoId,
),
stmt(
`UPDATE integrations SET active = 0, updated_at = ?,
config = json_remove(json_set(config, '$.githubMirror', 'none'), '$.githubRepo')
WHERE scope_type = 'repo' AND scope_id = ?`,
now,
repoId,
),
stmt("UPDATE webhooks SET active = 0 WHERE repo_id = ?", repoId),
];
}
/** Repo deletion removes everything the repo configured; cascades cover deliveries, jobs, audit and digest items. */
export function repoDeletionStatements(repoId: string): D1PreparedStatement[] {
return [
stmt("DELETE FROM integrations WHERE scope_type = 'repo' AND scope_id = ?", repoId),
stmt("DELETE FROM webhook_deliveries WHERE webhook_id IN (SELECT id FROM webhooks WHERE repo_id = ?)", repoId),
stmt("DELETE FROM webhooks WHERE repo_id = ?", repoId),
];
}