231 lines · 12.0 KBRaw
| 1 | import { parseDocument } from "yaml"; |
| 2 | import { JobGraph } from "./job-graph.ts"; |
| 3 | import { |
| 4 | AGENT_TIMEOUT_MINUTES, |
| 5 | BUILT_IN_IMAGES, |
| 6 | DEFAULT_SIZE, |
| 7 | DEFAULT_TIMEOUT_MINUTES, |
| 8 | INSTANCE_SIZES, |
| 9 | MAX_TIMEOUT_MINUTES, |
| 10 | type BranchFilter, |
| 11 | type BuiltInImage, |
| 12 | type CacheSpec, |
| 13 | type ConfigProblem, |
| 14 | type ImageRef, |
| 15 | type InstanceSize, |
| 16 | type Job, |
| 17 | type ParseResult, |
| 18 | type Step, |
| 19 | type Triggers, |
| 20 | } from "./pipeline.ts"; |
| 21 | |
| 22 | export const CONFIG_PATH = ".refs/ci.yml"; |
| 23 | export const MAX_CONFIG_BYTES = 64 * 1024; |
| 24 | export const MAX_JOBS = 64; |
| 25 | export const MAX_STEPS = 100; |
| 26 | |
| 27 | const NAME = /^[a-z0-9][a-z0-9_-]{0,63}$/i; |
| 28 | const IMAGE = /^(?:refs\/)?([a-z0-9][a-z0-9-]{0,63})(?:@(\d{4}-\d{2}))?$/; |
| 29 | const STEP_NAME_LENGTH = 80; |
| 30 | export const SECRET_NAME = /^[A-Z_][A-Z0-9_]{0,127}$/; |
| 31 | |
| 32 | const ROOT_KEYS = new Set(["on", "jobs"]); |
| 33 | const TRIGGER_KEYS = new Set(["push", "pull_request", "manual"]); |
| 34 | const FILTER_KEYS = new Set(["branches"]); |
| 35 | const JOB_KEYS = new Set(["image", "size", "needs", "steps", "environment", "timeout_minutes", "cache", "artifacts", "secrets", "agent", "prompt"]); |
| 36 | const AGENT_JOB_KEYS = new Set(["agent", "prompt", "needs", "timeout_minutes"]); |
| 37 | const AGENT_HANDLE = /^[a-z][a-z0-9-]{1,31}$/; |
| 38 | const MAX_PROMPT = 4000; |
| 39 | const STEP_KEYS = new Set(["name", "run"]); |
| 40 | const CACHE_KEYS = new Set(["key", "paths"]); |
| 41 | |
| 42 | const DEFAULT_TRIGGERS: Triggers = { push: { branches: [] }, pullRequest: { branches: [] }, manual: true }; |
| 43 | |
| 44 | /** Reads `.refs/ci.yml`. Returns every problem it finds, with the path of the field, instead of stopping at the first. */ |
| 45 | export function parsePipeline(text: string): ParseResult { |
| 46 | if (new TextEncoder().encode(text).length > MAX_CONFIG_BYTES) return invalid([{ path: "", message: `The file is larger than ${MAX_CONFIG_BYTES / 1024} KB.` }]); |
| 47 | const document = parseDocument(text, { uniqueKeys: true }); |
| 48 | if (document.errors.length) return invalid(document.errors.map((error) => ({ path: "", message: error.message }))); |
| 49 | const reader = new Reader(); |
| 50 | const root: unknown = document.toJS({ maxAliasCount: 16 }); |
| 51 | if (!isRecord(root)) return invalid([{ path: "", message: "The file must be a mapping with a jobs key." }]); |
| 52 | reader.keys(root, "", ROOT_KEYS); |
| 53 | const triggers = root.on === undefined ? DEFAULT_TRIGGERS : reader.triggers(root.on, "on"); |
| 54 | const jobs = reader.jobs(root.jobs, "jobs"); |
| 55 | if (reader.problems.length || !triggers) return invalid(reader.problems); |
| 56 | const checked = JobGraph.check(jobs); |
| 57 | if ("problems" in checked) return invalid(checked.problems); |
| 58 | return { kind: "valid", pipeline: { triggers, jobs } }; |
| 59 | } |
| 60 | |
| 61 | function invalid(problems: readonly ConfigProblem[]): ParseResult { |
| 62 | return { kind: "invalid", problems }; |
| 63 | } |
| 64 | |
| 65 | function isRecord(value: unknown): value is Record<string, unknown> { |
| 66 | return typeof value === "object" && value !== null && !Array.isArray(value); |
| 67 | } |
| 68 | |
| 69 | function isBuiltIn(name: string): name is BuiltInImage { |
| 70 | return BUILT_IN_IMAGES.some((image) => image === name); |
| 71 | } |
| 72 | |
| 73 | function isSize(value: unknown): value is InstanceSize { |
| 74 | return INSTANCE_SIZES.some((size) => size === value); |
| 75 | } |
| 76 | |
| 77 | class Reader { |
| 78 | readonly problems: ConfigProblem[] = []; |
| 79 | |
| 80 | fail(path: string, message: string): null { |
| 81 | this.problems.push({ path, message }); |
| 82 | return null; |
| 83 | } |
| 84 | |
| 85 | keys(value: Record<string, unknown>, path: string, allowed: ReadonlySet<string>): void { |
| 86 | for (const key of Object.keys(value)) if (!allowed.has(key)) this.fail(path ? `${path}.${key}` : key, `Unknown key "${key}".`); |
| 87 | } |
| 88 | |
| 89 | string(value: unknown, path: string): string | null { |
| 90 | if (typeof value === "string" && value.trim()) return value; |
| 91 | return this.fail(path, "Must be a non-empty string."); |
| 92 | } |
| 93 | |
| 94 | strings(value: unknown, path: string): string[] | null { |
| 95 | if (typeof value === "string") return this.string(value, path) === null ? null : [value]; |
| 96 | if (!Array.isArray(value)) return this.fail(path, "Must be a string or a list of strings."); |
| 97 | const read = value.map((item, index) => this.string(item, `${path}[${index}]`)); |
| 98 | return read.every((item) => item !== null) ? read : null; |
| 99 | } |
| 100 | |
| 101 | triggers(value: unknown, path: string): Triggers | null { |
| 102 | if (!isRecord(value)) return this.fail(path, "Must be a mapping of push, pull_request and manual."); |
| 103 | this.keys(value, path, TRIGGER_KEYS); |
| 104 | const manual = value.manual; |
| 105 | if (manual !== undefined && manual !== null && manual !== true && !(isRecord(manual) && !Object.keys(manual).length)) this.fail(`${path}.manual`, "Must be empty or true."); |
| 106 | return { |
| 107 | push: "push" in value ? this.filter(value.push, `${path}.push`) : null, |
| 108 | pullRequest: "pull_request" in value ? this.filter(value.pull_request, `${path}.pull_request`) : null, |
| 109 | manual: "manual" in value, |
| 110 | }; |
| 111 | } |
| 112 | |
| 113 | filter(value: unknown, path: string): BranchFilter | null { |
| 114 | if (value === null || value === undefined) return { branches: [] }; |
| 115 | if (!isRecord(value)) return this.fail(path, "Must be empty or a mapping with branches."); |
| 116 | this.keys(value, path, FILTER_KEYS); |
| 117 | if (value.branches === undefined) return { branches: [] }; |
| 118 | const branches = this.strings(value.branches, `${path}.branches`); |
| 119 | return branches && { branches }; |
| 120 | } |
| 121 | |
| 122 | jobs(value: unknown, path: string): Map<string, Job> { |
| 123 | const jobs = new Map<string, Job>(); |
| 124 | if (!isRecord(value) || !Object.keys(value).length) { |
| 125 | this.fail(path, "Must be a mapping with at least one job."); |
| 126 | return jobs; |
| 127 | } |
| 128 | const names = Object.keys(value); |
| 129 | if (names.length > MAX_JOBS) this.fail(path, `A pipeline can have at most ${MAX_JOBS} jobs.`); |
| 130 | for (const name of names.slice(0, MAX_JOBS)) { |
| 131 | if (!NAME.test(name)) { |
| 132 | this.fail(`${path}.${name}`, "A job name uses letters, digits, - and _, and is at most 64 characters."); |
| 133 | continue; |
| 134 | } |
| 135 | const job = this.job(name, value[name], `${path}.${name}`); |
| 136 | if (job) jobs.set(name, job); |
| 137 | } |
| 138 | return jobs; |
| 139 | } |
| 140 | |
| 141 | job(name: string, value: unknown, path: string): Job | null { |
| 142 | if (!isRecord(value)) return this.fail(path, "Must be a mapping with steps."); |
| 143 | const before = this.problems.length; |
| 144 | if (value.agent !== undefined) return this.agentJob(name, value, path); |
| 145 | if (value.prompt !== undefined) this.fail(`${path}.prompt`, "Only an agent job takes a prompt. Add agent, or remove prompt."); |
| 146 | this.keys(value, path, JOB_KEYS); |
| 147 | const image = value.image === undefined ? { kind: "built-in" as const, name: "base" as const, version: null } : this.image(value.image, `${path}.image`); |
| 148 | const size = value.size === undefined ? DEFAULT_SIZE : isSize(value.size) ? value.size : this.fail(`${path}.size`, `Must be one of ${INSTANCE_SIZES.join(", ")}.`); |
| 149 | const needs = value.needs === undefined ? [] : this.strings(value.needs, `${path}.needs`); |
| 150 | const steps = this.steps(value.steps, `${path}.steps`); |
| 151 | const environment = value.environment === undefined ? null : this.environment(value.environment, `${path}.environment`); |
| 152 | const timeoutMinutes = value.timeout_minutes === undefined ? DEFAULT_TIMEOUT_MINUTES : this.timeout(value.timeout_minutes, `${path}.timeout_minutes`); |
| 153 | const cache = value.cache === undefined ? null : this.cache(value.cache, `${path}.cache`); |
| 154 | const artifacts = value.artifacts === undefined ? [] : this.strings(value.artifacts, `${path}.artifacts`); |
| 155 | const secrets = value.secrets === undefined ? [] : this.secretNames(value.secrets, `${path}.secrets`); |
| 156 | if (this.problems.length > before || !image || !size || !needs || !steps || timeoutMinutes === null || !artifacts || !secrets) return null; |
| 157 | return { name, agent: null, image, size, needs, steps, environment, timeoutMinutes, cache, artifacts, secrets }; |
| 158 | } |
| 159 | |
| 160 | /** A job that runs a hosted agent. It has no container, so it takes no image, size, steps, environment or secrets. */ |
| 161 | agentJob(name: string, value: Record<string, unknown>, path: string): Job | null { |
| 162 | const before = this.problems.length; |
| 163 | for (const key of Object.keys(value)) if (!AGENT_JOB_KEYS.has(key)) this.fail(`${path}.${key}`, "An agent job takes only agent, prompt, needs and timeout_minutes."); |
| 164 | const handle = typeof value.agent === "string" ? value.agent.replace(/^@/, "") : ""; |
| 165 | if (!AGENT_HANDLE.test(handle)) this.fail(`${path}.agent`, "Must be the handle of a hosted agent, such as reviewer."); |
| 166 | const prompt = value.prompt === undefined ? "" : this.string(value.prompt, `${path}.prompt`); |
| 167 | if (prompt && prompt.length > MAX_PROMPT) this.fail(`${path}.prompt`, `A prompt is at most ${MAX_PROMPT} characters.`); |
| 168 | const needs = value.needs === undefined ? [] : this.strings(value.needs, `${path}.needs`); |
| 169 | const timeoutMinutes = value.timeout_minutes === undefined ? AGENT_TIMEOUT_MINUTES : this.timeout(value.timeout_minutes, `${path}.timeout_minutes`); |
| 170 | if (this.problems.length > before || prompt === null || !needs || timeoutMinutes === null) return null; |
| 171 | const step = { name: `@${handle}`, run: prompt || `@${handle} follows its own instructions.` }; |
| 172 | return { name, agent: { handle, prompt }, image: { kind: "built-in", name: "base", version: null }, size: DEFAULT_SIZE, needs, steps: [step], environment: null, timeoutMinutes, cache: null, artifacts: [], secrets: [] }; |
| 173 | } |
| 174 | |
| 175 | image(value: unknown, path: string): ImageRef | null { |
| 176 | const match = typeof value === "string" ? IMAGE.exec(value) : null; |
| 177 | if (!match) return this.fail(path, "Must be an image name such as node or rust@2026-12."); |
| 178 | const [, name, version] = match; |
| 179 | if (isBuiltIn(name)) return { kind: "built-in", name, version: version ?? null }; |
| 180 | if (version) return this.fail(path, "Only built-in images have dated versions."); |
| 181 | if (typeof value === "string" && value.startsWith("refs/")) return this.fail(path, `There is no built-in image named "${name}".`); |
| 182 | return this.fail(path, `There is no image named "${name}". Use one of ${BUILT_IN_IMAGES.join(", ")}.`); |
| 183 | } |
| 184 | |
| 185 | secretNames(value: unknown, path: string): string[] | null { |
| 186 | const names = this.strings(value, path); |
| 187 | if (!names) return null; |
| 188 | const bad = names.find((name) => !SECRET_NAME.test(name)); |
| 189 | return bad === undefined ? names : this.fail(path, `"${bad}" is not a secret name. Use capital letters, digits and _, and do not start with a digit.`); |
| 190 | } |
| 191 | |
| 192 | steps(value: unknown, path: string): Step[] | null { |
| 193 | if (!Array.isArray(value) || !value.length) return this.fail(path, "Must be a list with at least one step."); |
| 194 | if (value.length > MAX_STEPS) return this.fail(path, `A job can have at most ${MAX_STEPS} steps.`); |
| 195 | const steps = value.map((item, index) => this.step(item, `${path}[${index}]`)); |
| 196 | return steps.every((step) => step !== null) ? steps : null; |
| 197 | } |
| 198 | |
| 199 | step(value: unknown, path: string): Step | null { |
| 200 | if (typeof value === "string") return value.trim() ? { name: stepName(value), run: value } : this.fail(path, "A step cannot be empty."); |
| 201 | if (!isRecord(value)) return this.fail(path, "Must be a command or a mapping with run."); |
| 202 | this.keys(value, path, STEP_KEYS); |
| 203 | const run = this.string(value.run, `${path}.run`); |
| 204 | const name = value.name === undefined ? null : this.string(value.name, `${path}.name`); |
| 205 | if (!run || (value.name !== undefined && !name)) return null; |
| 206 | return { name: name ?? stepName(run), run }; |
| 207 | } |
| 208 | |
| 209 | environment(value: unknown, path: string): string | null { |
| 210 | if (typeof value === "string" && NAME.test(value)) return value; |
| 211 | return this.fail(path, "An environment name uses letters, digits, - and _."); |
| 212 | } |
| 213 | |
| 214 | timeout(value: unknown, path: string): number | null { |
| 215 | if (Number.isInteger(value) && typeof value === "number" && value >= 1 && value <= MAX_TIMEOUT_MINUTES) return value; |
| 216 | return this.fail(path, `Must be a whole number of minutes from 1 to ${MAX_TIMEOUT_MINUTES}.`); |
| 217 | } |
| 218 | |
| 219 | cache(value: unknown, path: string): CacheSpec | null { |
| 220 | if (!isRecord(value)) return this.fail(path, "Must be a mapping with key and paths."); |
| 221 | this.keys(value, path, CACHE_KEYS); |
| 222 | const key = this.string(value.key, `${path}.key`); |
| 223 | const paths = this.strings(value.paths, `${path}.paths`); |
| 224 | return key && paths ? { key, paths } : null; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | function stepName(run: string): string { |
| 229 | const line = run.trim().split("\n")[0].trim(); |
| 230 | return line.length > STEP_NAME_LENGTH ? `${line.slice(0, STEP_NAME_LENGTH - 1)}…` : line; |
| 231 | } |