97 lines · 4.1 KB
import { repoDb } from "../scoped-db";
import { withStore } from "../git/store";
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 INVALID_PREFIX = "Invalid workflow: ";

/** A run that failed to compile stored its error as its title; a re-run gets the commit's subject back. */
async function rerunTitle(repo: RepoRow, runRow: WorkflowRunRow): Promise<string> {
  if (!runRow.title.startsWith(INVALID_PREFIX)) return runRow.title;
  const commit = await withStore(repo.artifacts_name, (store) => store.commit(runRow.sha)).catch(() => null);
  return commit?.subject ?? `${runRow.workflow_path} re-run`;
}

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> {