285 lines · 11.2 KB
import { env } from "cloudflare:workers";
import { claimHost, deploymentDb, hostHolder, loadSiteRow, siteDb, siteDbById } from "./store";
import { registerLocator } from "../scoped-db/locators";
import { scopedId } from "../scoped-db/ids";
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 loadSiteRow(id);
}

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