78 lines · 2.9 KB
import { matchesFilters } from "~/lib/glob";
import type { ExprValue } from "./expressions";
import type { RefFilters, WorkflowSpec } from "./spec";
import type { RepoRow } from "~/lib/types";

const DEFAULT_PR_TYPES = ["opened", "synchronize", "reopened"];
const DEFAULT_RELEASE_TYPES = ["published"];

export type Trigger = {
  event: "push" | "pull_request" | "release" | "workflow_dispatch";
  action: string | null;
  ref: string;
  sha: string;
  actorId: string | null;
  actorName: string;
  title: string;
  prNumber: number | null;
  headRef: string | null;
  baseRef: string | null;
  changedPaths: string[] | null;
  inputs: Record<string, ExprValue>;
  workflowPath: string | null;
  /** Repository whose objects are checked out (differs from `repo` for fork pull requests). */
  sourceRepo: RepoRow;
};

function pathsAllowed(filters: RefFilters, changed: string[] | null): boolean {
  if (changed === null) return true;
  if (filters.paths && !changed.some((path) => matchesFilters(filters.paths ?? [], path))) return false;
  if (filters["paths-ignore"] && changed.every((path) => matchesFilters(filters["paths-ignore"] ?? [], path))) {
    return false;
  }
  return true;
}

function refAllowed(include: string[] | undefined, exclude: string[] | undefined, name: string): boolean {
  if (include && !matchesFilters(include, name)) return false;
  if (exclude && matchesFilters(exclude, name)) return false;
  return true;
}