258 lines · 10.1 KB
import { env } from "cloudflare:workers";
import { all, first, run } from "../db";
import { randomId } from "../crypto";
import { HttpError, badRequest } from "../errors";
import { manifestKey, publishMeta, SITE_LIMITS, slotKey } from "./config";
import { isValidLabel, splitWorker } from "./naming";
import { isOrphanSite } from "./sync";
import { dropAliasesForPreview, dropSiteAliases, syncPullAliases } from "./pr-aliases";
import { loadRepoById, repoPermission } from "../repos";
import { loadUserById, toViewer } from "../auth";
import type { SiteDeploymentRow, SiteEnvironment, SiteManifest, SiteRow, SlotPointer } from "./config";
import { notifySiteDeployFailed } from "../notify/deploy";
import type { CollectedFiles } from "./collect";

const HOUR = 60 * 60 * 1000;
const HTTP_TOO_MANY = 429;

export type SiteDeployRequest = {
  site: SiteRow;
  environment: SiteEnvironment;
  label: string;
  branch: string | null;
  ref: string;
  sha: string;
  artifactId: string | null;
  source: SiteDeploymentRow["source"];
  creatorId: string | null;
  /** Whose admin role unlocks preview variables; defaults to the creator. Runs pass the SHA's pusher (CI-P2). */
  trustedId?: string | null;
};

export async function loadSiteById(id: string): Promise<SiteRow | null> {
  return first<SiteRow>("SELECT * FROM sites WHERE id = ?", id);
}

export async function loadSiteForRepo(repoId: string): Promise<SiteRow | null> {
  return first<SiteRow>("SELECT * FROM sites WHERE repo_id = ?", repoId);
}

export async function loadSiteDeployment(id: string): Promise<SiteDeploymentRow | null> {