import { matchesAny } from "../config/branch-pattern.ts"; export type RunSource = | { readonly kind: "push"; readonly pusher: "human" | "agent" | "system"; readonly branch: string } | { readonly kind: "pull_request"; readonly author: "human" | "agent" | "system"; readonly fromFork: boolean; readonly number: number; readonly base: string; readonly head: string } | { readonly kind: "manual"; readonly by: "human" | "agent"; readonly branch: string }; export type Trust = "trusted" | "untrusted"; /** * Which secrets a run's jobs receive. `repo` gives organization and repository secrets; a job in an environment also * gets that environment's secrets once a person approves it. `all` gives environment secrets without that approval. * Every environment's own gate (see `gateEnvironment`) still applies. */ export type SecretAccess = "all" | "repo" | "none"; export interface TrustDecision { readonly trust: Trust; readonly secrets: SecretAccess; readonly reason: string; } /** * Decides trust from who started the run, not from the branch alone. An agent can edit `.refs/ci.yml`, so a run it * starts gets no secrets until a human approves it. A push that refs found itself has no known pusher, so it is untrusted too. */ export function decideTrust(source: RunSource, protectedBranches: readonly string[]): TrustDecision { switch (source.kind) { case "push": if (source.pusher === "agent") return untrusted("An agent pushed this commit."); if (source.pusher === "system") return untrusted("refs found this push without a known pusher."); if (protectedBranches.length && matchesAny(protectedBranches, source.branch)) return { trust: "trusted", secrets: "all", reason: "A person pushed to a protected branch." }; return { trust: "trusted", secrets: "repo", reason: "A person pushed to a branch that is not protected." }; case "pull_request": if (source.fromFork) return untrusted("The pull request comes from a fork."); if (source.author === "agent") return untrusted("An agent opened the pull request or pushed to it."); if (source.author === "system") return untrusted("refs found this push without a known pusher."); return { trust: "trusted", secrets: "repo", reason: "A person opened the pull request from this repository." }; case "manual": if (source.by === "agent") return untrusted("An agent started the run."); if (protectedBranches.length && matchesAny(protectedBranches, source.branch)) return { trust: "trusted", secrets: "all", reason: "A person started the run on a protected branch." }; return { trust: "trusted", secrets: "repo", reason: "A person started the run on a branch that is not protected." }; } } /** The decision after a person with write access approves an untrusted run: repository secrets, and still each environment's own gate. */ export function approvedTrust(approver: string): TrustDecision { return { trust: "trusted", secrets: "repo", reason: `${approver} approved the run.` }; } function untrusted(reason: string): TrustDecision { return { trust: "untrusted", secrets: "none", reason }; }