import { all, first, run } from "../db"; import { randomId } from "../crypto"; import { HttpError } from "../errors"; import { loadRepoById } from "../repos"; import { ZERO_SHA } from "../git/store"; import { labelUrl, SITE_LIMITS } from "./config"; import { createSiteDeployment, loadSiteForRepo, removePreview } from "./deploy"; import { previewLabel } from "./naming"; import type { SiteDeployRequest } from "./deploy"; import type { SiteRow } from "./config"; async function tryDeploy(input: SiteDeployRequest): Promise { try { await createSiteDeployment(input); return true; } catch (error) { if (error instanceof HttpError) { console.warn("Site deploy skipped", input.site.id, error.message); return false; } throw error; } } /** Comments the preview link once per pull request; the pusher is the author so no bot account is needed. */ async function commentPreview(site: SiteRow, branch: string, label: string, authorId: string | null): Promise { const url = labelUrl(label); if (!url || !authorId || !site.repo_id) return; const pulls = await all<{ number: number; issue_id: string }>( `SELECT i.number, i.id AS issue_id FROM pull_requests p JOIN issues i ON i.id = p.issue_id WHERE p.repo_id = ? AND p.head_repo_id = ? AND p.head_ref = ? AND i.state = 'open' LIMIT ?`, site.repo_id, site.repo_id, branch, SITE_LIMITS.previewsPerPush, ); for (const pull of pulls) { const now = Date.now(); const inserted = await run( "INSERT OR IGNORE INTO site_preview_comments (site_id, pr_number, created_at) VALUES (?, ?, ?)", site.id, pull.number, now, ); if (!inserted.meta.changes) continue; await run( "INSERT INTO comments (id, issue_id, author_id, body, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)", randomId(), pull.issue_id, authorId, `Preview deployment for \`${branch}\`: ${url}\n\nIt updates on every push to this branch.`, now, now, ); } } function canPublish(site: SiteRow | null, visibility: string | undefined): site is SiteRow { if (!site || site.disabled_reason || site.scratch === 1) return false; // Private repositories may only publish behind an access gate. return visibility === "public" || site.access !== "public"; } /** Push hook: the site branch deploys to production; any other branch of the same repo deploys a preview. */ export async function deploySitesForPush( repoId: string, branch: string | null, sha: string, pusherId: string | null, ): Promise { if (!branch) return 0; if (sha === ZERO_SHA) { const deleted = await loadSiteForRepo(repoId); if (deleted && branch !== deleted.branch) await removePreview(deleted, previewLabel(deleted.label, branch)); return 0; } const [repo, site] = await Promise.all([loadRepoById(repoId), loadSiteForRepo(repoId)]); if (!canPublish(site, repo?.visibility) || site.source !== "branch") return 0; const base = { site, branch, ref: `refs/heads/${branch}`, sha, artifactId: null, creatorId: pusherId } as const; if (branch === site.branch) { return (await tryDeploy({ ...base, source: "branch", environment: "production", label: site.label })) ? 1 : 0; } if (site.previews !== 1) return 0; const label = previewLabel(site.label, branch); if (!(await tryDeploy({ ...base, source: "branch", environment: "preview", label }))) return 0; await commentPreview(site, branch, label, pusherId); return 1; } type RunRow = { repo_id: string; source_repo_id: string; untrusted: number; ref: string; sha: string; actor_id: string | null; conclusion: string | null; }; /** CI hook: a successful trusted run's artifact deploys to production or to its branch preview. */ export async function deploySitesForRun(runId: string): Promise { const runRow = await first( "SELECT repo_id, source_repo_id, untrusted, ref, sha, actor_id, conclusion FROM workflow_runs WHERE id = ?", runId, ); if (!runRow || runRow.conclusion !== "success") return false; // Fork code never deploys, even to an isolated preview origin (PAGES-1). if (runRow.source_repo_id !== runRow.repo_id || runRow.untrusted !== 0) return false; if (!runRow.ref.startsWith("refs/heads/")) return false; const [repo, site] = await Promise.all([loadRepoById(runRow.repo_id), loadSiteForRepo(runRow.repo_id)]); if (!canPublish(site, repo?.visibility) || site.source !== "workflow") return false; const branch = runRow.ref.slice("refs/heads/".length); const production = branch === site.branch; if (!production && site.previews !== 1) return false; const artifact = await first<{ id: string }>( "SELECT id FROM job_artifacts WHERE run_id = ? AND name = ? ORDER BY created_at DESC LIMIT 1", runId, site.artifact_name, ); if (!artifact) return false; const label = production ? site.label : previewLabel(site.label, branch); const deployed = await tryDeploy({ site, environment: production ? "production" : "preview", label, branch, ref: runRow.ref, sha: runRow.sha, artifactId: artifact.id, source: "artifact", creatorId: runRow.actor_id, }); if (deployed && !production) await commentPreview(site, branch, label, runRow.actor_id); return deployed; }