Skip to content
refs
History
53 lines · 3.1 KBRaw
1import { matchesAny } from "../config/branch-pattern.ts";
2
3export type RunSource =
4 | { readonly kind: "push"; readonly pusher: "human" | "agent" | "system"; readonly branch: string }
5 | { readonly kind: "pull_request"; readonly author: "human" | "agent" | "system"; readonly fromFork: boolean; readonly number: number; readonly base: string; readonly head: string }
6 | { readonly kind: "manual"; readonly by: "human" | "agent"; readonly branch: string };
7
8export type Trust = "trusted" | "untrusted";
9
10/**
11 * Which secrets a run's jobs receive. `repo` gives organization and repository secrets; a job in an environment also
12 * gets that environment's secrets once a person approves it. `all` gives environment secrets without that approval.
13 * Every environment's own gate (see `gateEnvironment`) still applies.
14 */
15export type SecretAccess = "all" | "repo" | "none";
16
17export interface TrustDecision {
18 readonly trust: Trust;
19 readonly secrets: SecretAccess;
20 readonly reason: string;
21}
22
23/**
24 * Decides trust from who started the run, not from the branch alone. An agent can edit `.refs/ci.yml`, so a run it
25 * starts gets no secrets until a human approves it. A push that refs found itself has no known pusher, so it is untrusted too.
26 */
27export function decideTrust(source: RunSource, protectedBranches: readonly string[]): TrustDecision {
28 switch (source.kind) {
29 case "push":
30 if (source.pusher === "agent") return untrusted("An agent pushed this commit.");
31 if (source.pusher === "system") return untrusted("refs found this push without a known pusher.");
32 if (protectedBranches.length && matchesAny(protectedBranches, source.branch)) return { trust: "trusted", secrets: "all", reason: "A person pushed to a protected branch." };
33 return { trust: "trusted", secrets: "repo", reason: "A person pushed to a branch that is not protected." };
34 case "pull_request":
35 if (source.fromFork) return untrusted("The pull request comes from a fork.");
36 if (source.author === "agent") return untrusted("An agent opened the pull request or pushed to it.");
37 if (source.author === "system") return untrusted("refs found this push without a known pusher.");
38 return { trust: "trusted", secrets: "repo", reason: "A person opened the pull request from this repository." };
39 case "manual":
40 if (source.by === "agent") return untrusted("An agent started the run.");
41 if (protectedBranches.length && matchesAny(protectedBranches, source.branch)) return { trust: "trusted", secrets: "all", reason: "A person started the run on a protected branch." };
42 return { trust: "trusted", secrets: "repo", reason: "A person started the run on a branch that is not protected." };
43 }
44}
45
46/** The decision after a person with write access approves an untrusted run: repository secrets, and still each environment's own gate. */
47export function approvedTrust(approver: string): TrustDecision {
48 return { trust: "trusted", secrets: "repo", reason: `${approver} approved the run.` };
49}
50
51function untrusted(reason: string): TrustDecision {
52 return { trust: "untrusted", secrets: "none", reason };
53}