194 lines · 7.5 KB
import { first, run } from "../db";
import { repoDb } from "../scoped-db";
import { sha256Hex } from "../crypto";
import { enforceRateLimit } from "../limits";
import { HttpError } from "../errors";
import { loadRepoById } from "../repos";
import { isScratchRepo } from "../sites/config";
import { branchSha, listRefs } from "../git/refs";
import type { ExprValue } from "./expressions";
import { isJobTokenPush } from "./job-token";
import { scheduleWorkflows } from "./scheduler";
import type { Trigger, TriggerEvent } from "./triggers";
import { hasTriggerFor } from "./workflow-index";
import type { RepoRow } from "~/lib/types";

const MAX_WORKFLOW_RUN_DEPTH = 3;
const ZERO_SHA = "0000000000000000000000000000000000000000";

export function toExprValue(value: unknown): ExprValue {
  if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
    return value;
  }
  if (Array.isArray(value)) return value.map(toExprValue);
  if (typeof value === "object") {
    return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, toExprValue(item)]));
  }
  return null;
}

function record(value: ExprValue | undefined): Record<string, ExprValue> {
  return value && typeof value === "object" && !Array.isArray(value) ? value : {};
}

function text(value: ExprValue | undefined): string {
  return typeof value === "string" ? value : typeof value === "number" ? String(value) : "";
}

type Mapped = {
  event: TriggerEvent;
  action: string | null;