83 lines · 3.1 KB
import { first } from "../db";
import { badRequest } from "../errors";
import { loadRepoById } from "../repos";
import { loadPull } from "../pulls";
import { toExprValue } from "./event-triggers";
import type { CompileInput } from "./compile";
import type { ExprValue } from "./expressions";
import type { WorkflowRunRow } from "./jobs";
import type { Trigger, TriggerEvent } from "./triggers";
import type { RepoRow, Viewer } from "~/lib/types";

const EVENTS: TriggerEvent[] = [
  "push",
  "pull_request",
  "release",
  "workflow_dispatch",
  "schedule",
  "issues",
  "issue_comment",
  "pull_request_review",
  "workflow_run",
  "create",
  "delete",
];

function isTriggerEvent(value: string): value is TriggerEvent {
  return (EVENTS as string[]).includes(value);
}

function storedEvent(runRow: WorkflowRunRow): Record<string, ExprValue> {
  if (!runRow.context) return {};
  const context: CompileInput = JSON.parse(runRow.context);
  const event = toExprValue(context.event.event);
  return event && typeof event === "object" && !Array.isArray(event) ? event : {};
}

const MERGE_QUEUE_REF_PREFIX = "refs/heads/gitfor-merge-queue/";

/** Merge queue runs must keep reading workflow definitions from the base commit, never the merge group. */
async function queueWorkflowSource(repo: RepoRow, runRow: WorkflowRunRow): Promise<Trigger["workflowSource"]> {