|
| 1 | +import { readFileSync } from "node:fs"; |
| 2 | + |
| 3 | +import type { IMetaRule, IViolation } from "../../types"; |
| 4 | + |
| 5 | +const JOB_KEY_REGEX = /^ {2}([\w-]+):\s*(?:#.*)?$/u; |
| 6 | +const TOP_LEVEL_KEY_REGEX = /^\S/u; |
| 7 | + |
| 8 | +interface IJobBlock { |
| 9 | + readonly name: string; |
| 10 | + readonly lines: readonly string[]; |
| 11 | +} |
| 12 | + |
| 13 | +/* |
| 14 | + * Line-based scan (same pragmatic idiom as github-actions-permissions): |
| 15 | + * collect each `jobs:` child block, then require a job-level |
| 16 | + * `timeout-minutes:` unless the job is a reusable-workflow call |
| 17 | + * (job-level `uses:` — those cannot set timeout-minutes). |
| 18 | + */ |
| 19 | +function collectJobBlocks(text: string): IJobBlock[] { |
| 20 | + const lines = text.split("\n"); |
| 21 | + const blocks: IJobBlock[] = []; |
| 22 | + let inJobs = false; |
| 23 | + let current: { name: string; lines: string[] } | null = null; |
| 24 | + |
| 25 | + for (const line of lines) { |
| 26 | + if (/^jobs:\s*(?:#.*)?$/u.test(line)) { |
| 27 | + inJobs = true; |
| 28 | + continue; |
| 29 | + } |
| 30 | + |
| 31 | + if (!inJobs) { |
| 32 | + continue; |
| 33 | + } |
| 34 | + |
| 35 | + if (TOP_LEVEL_KEY_REGEX.test(line)) { |
| 36 | + inJobs = false; |
| 37 | + |
| 38 | + if (current !== null) { |
| 39 | + blocks.push(current); |
| 40 | + current = null; |
| 41 | + } |
| 42 | + |
| 43 | + continue; |
| 44 | + } |
| 45 | + |
| 46 | + const jobMatch = JOB_KEY_REGEX.exec(line); |
| 47 | + |
| 48 | + if (jobMatch?.[1] !== undefined) { |
| 49 | + if (current !== null) { |
| 50 | + blocks.push(current); |
| 51 | + } |
| 52 | + |
| 53 | + current = { name: jobMatch[1], lines: [] }; |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + if (current !== null) { |
| 58 | + current.lines.push(line); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + if (current !== null) { |
| 63 | + blocks.push(current); |
| 64 | + } |
| 65 | + |
| 66 | + return blocks; |
| 67 | +} |
| 68 | + |
| 69 | +export function checkWorkflowTimeouts(file: string): IViolation[] { |
| 70 | + const violations: IViolation[] = []; |
| 71 | + const text = readFileSync(file, "utf8"); |
| 72 | + |
| 73 | + for (const job of collectJobBlocks(text)) { |
| 74 | + const isReusableCall = job.lines.some((line) => |
| 75 | + /^ {4}uses:\s*\S/u.test(line) |
| 76 | + ); |
| 77 | + |
| 78 | + if (isReusableCall) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + const hasTimeout = job.lines.some((line) => |
| 83 | + /^ {4}timeout-minutes:\s*[1-9]\d*\s*(?:#.*)?$/u.test(line) |
| 84 | + ); |
| 85 | + |
| 86 | + if (!hasTimeout) { |
| 87 | + violations.push({ |
| 88 | + file, |
| 89 | + rule: "github-actions-timeout-required", |
| 90 | + message: `Job "${job.name}" has no job-level \`timeout-minutes:\` — a hung step runs for GitHub's 6h default and blocks the PR check.`, |
| 91 | + }); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return violations; |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Every runnable workflow job must declare an explicit `timeout-minutes:` so |
| 100 | + * a hang fails fast instead of occupying a runner for GitHub's 6h default. |
| 101 | + */ |
| 102 | +export const githubActionsTimeoutRequiredRule: IMetaRule = { |
| 103 | + id: "github-actions-timeout-required", |
| 104 | + category: "ci", |
| 105 | + description: |
| 106 | + "GitHub Actions jobs require an explicit timeout-minutes (reusable-workflow calls exempt).", |
| 107 | + run({ workflowFiles }) { |
| 108 | + return workflowFiles.flatMap(checkWorkflowTimeouts); |
| 109 | + }, |
| 110 | +}; |
0 commit comments