|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Three invariants about what lives in .github/workflows/ and what its names mean. |
| 3 | +
|
| 4 | +`.github/workflows/` is a directory GitHub reads, not a place to keep things. Every |
| 5 | +file at its top level is parsed as a workflow, so a script or a data file parked there |
| 6 | +is either an invalid workflow or an orphan nobody can find. A subdirectory is not read |
| 7 | +at all, so helper files may live in one. GitHub accepts both `.yml` and `.yaml`, and |
| 8 | +this repo spells them `.yml`, which is a naming rule rather than a validity one and is |
| 9 | +reported separately. And the `_` prefix is the repo's only signal that a workflow is a |
| 10 | +reusable building block rather than something that runs on its own, which is worth |
| 11 | +nothing unless it is true both ways. |
| 12 | +
|
| 13 | + WF001 a top-level file in .github/workflows/ that is not a workflow at all |
| 14 | + WF002 a workflow whose only trigger is `workflow_call` but is not `_`-prefixed |
| 15 | + WF003 a `_`-prefixed workflow that no other workflow can call |
| 16 | + WF004 a real workflow spelled `.yaml` where this directory spells them `.yml` |
| 17 | +
|
| 18 | +A workflow with `workflow_call` alongside a human trigger is deliberately dual-mode |
| 19 | +and belongs under its plain name, so only the call-only ones are held to WF002. |
| 20 | +
|
| 21 | +Usage |
| 22 | +----- |
| 23 | + python assert_workflow_dir_hygiene.py |
| 24 | +
|
| 25 | +Exit code 1 if any violation is found. |
| 26 | +""" |
| 27 | + |
| 28 | +from __future__ import annotations |
| 29 | + |
| 30 | +import pathlib |
| 31 | +import sys |
| 32 | +from dataclasses import dataclass |
| 33 | +from typing import Final |
| 34 | + |
| 35 | +import yaml |
| 36 | + |
| 37 | +REPO_ROOT: Final = pathlib.Path(__file__).resolve().parents[2] |
| 38 | +WORKFLOW_DIR: Final = REPO_ROOT / ".github" / "workflows" |
| 39 | +SCRIPT_HOME: Final = ".github/scripts/" |
| 40 | +REUSABLE_PREFIX: Final = "_" |
| 41 | +CALL_TRIGGER: Final = "workflow_call" |
| 42 | +CANONICAL_SUFFIX: Final = ".yml" |
| 43 | +WORKFLOW_SUFFIXES: Final = frozenset((CANONICAL_SUFFIX, ".yaml")) |
| 44 | + |
| 45 | + |
| 46 | +@dataclass(frozen=True, slots=True) |
| 47 | +class Finding: |
| 48 | + subject: str |
| 49 | + code: str |
| 50 | + detail: str |
| 51 | + |
| 52 | + def render(self) -> str: |
| 53 | + return f" - {self.subject}: {self.code} {self.detail}" |
| 54 | + |
| 55 | + |
| 56 | +def _triggers(document: object) -> frozenset[str]: |
| 57 | + if not isinstance(document, dict): |
| 58 | + return frozenset() |
| 59 | + raw: Final = document.get("on", document.get(True)) |
| 60 | + if isinstance(raw, str): |
| 61 | + return frozenset({raw}) |
| 62 | + if isinstance(raw, dict): |
| 63 | + return frozenset(str(key) for key in raw) |
| 64 | + if isinstance(raw, list): |
| 65 | + return frozenset(str(item) for item in raw) |
| 66 | + return frozenset() |
| 67 | + |
| 68 | + |
| 69 | +def _workflows(directory: pathlib.Path) -> tuple[pathlib.Path, ...]: |
| 70 | + return tuple( |
| 71 | + path |
| 72 | + for path in sorted(directory.iterdir()) |
| 73 | + if path.is_file() and path.suffix in WORKFLOW_SUFFIXES |
| 74 | + ) |
| 75 | + |
| 76 | + |
| 77 | +def _strays(directory: pathlib.Path) -> tuple[Finding, ...]: |
| 78 | + return tuple( |
| 79 | + Finding( |
| 80 | + path.name, |
| 81 | + "WF001", |
| 82 | + f"is not a workflow, and GitHub parses every top-level file here as one; " |
| 83 | + f"move it to {SCRIPT_HOME} or into a subdirectory, which GitHub does not read", |
| 84 | + ) |
| 85 | + for path in sorted(directory.iterdir()) |
| 86 | + if path.is_file() and path.suffix not in WORKFLOW_SUFFIXES |
| 87 | + ) |
| 88 | + |
| 89 | + |
| 90 | +def _misspelled(directory: pathlib.Path) -> tuple[Finding, ...]: |
| 91 | + return tuple( |
| 92 | + Finding( |
| 93 | + path.name, |
| 94 | + "WF004", |
| 95 | + f"is a real workflow and GitHub reads it, but this directory spells them " |
| 96 | + f"{CANONICAL_SUFFIX}; rename it to {path.stem}{CANONICAL_SUFFIX}", |
| 97 | + ) |
| 98 | + for path in _workflows(directory) |
| 99 | + if path.suffix != CANONICAL_SUFFIX |
| 100 | + ) |
| 101 | + |
| 102 | + |
| 103 | +def _misnamed(directory: pathlib.Path) -> tuple[Finding, ...]: |
| 104 | + return tuple( |
| 105 | + finding |
| 106 | + for path in _workflows(directory) |
| 107 | + for finding in _naming_findings(path, _triggers(yaml.safe_load(path.read_text(encoding="utf-8")))) |
| 108 | + ) |
| 109 | + |
| 110 | + |
| 111 | +def _naming_findings(path: pathlib.Path, triggers: frozenset[str]) -> tuple[Finding, ...]: |
| 112 | + underscored: Final = path.name.startswith(REUSABLE_PREFIX) |
| 113 | + if triggers == frozenset({CALL_TRIGGER}) and not underscored: |
| 114 | + return ( |
| 115 | + Finding( |
| 116 | + path.name, |
| 117 | + "WF002", |
| 118 | + f"is only callable by another workflow, so name it {REUSABLE_PREFIX}{path.name}", |
| 119 | + ), |
| 120 | + ) |
| 121 | + if underscored and CALL_TRIGGER not in triggers: |
| 122 | + return ( |
| 123 | + Finding( |
| 124 | + path.name, |
| 125 | + "WF003", |
| 126 | + f"is named as a reusable workflow but has no {CALL_TRIGGER} trigger; " |
| 127 | + "add one or drop the prefix", |
| 128 | + ), |
| 129 | + ) |
| 130 | + return () |
| 131 | + |
| 132 | + |
| 133 | +def main() -> int: |
| 134 | + findings: Final = _strays(WORKFLOW_DIR) + _misspelled(WORKFLOW_DIR) + _misnamed(WORKFLOW_DIR) |
| 135 | + if not findings: |
| 136 | + total: Final = len(_workflows(WORKFLOW_DIR)) |
| 137 | + sys.stdout.write( |
| 138 | + f"OK: {total} workflows, every file in .github/workflows/ is one, and the " |
| 139 | + f"{REUSABLE_PREFIX} prefix means callable in both directions.\n" |
| 140 | + ) |
| 141 | + return 0 |
| 142 | + sys.stdout.write("ERROR: .github/workflows/ holds files that break its own conventions\n") |
| 143 | + for finding in findings: |
| 144 | + sys.stdout.write(f"{finding.render()}\n") |
| 145 | + return 1 |
| 146 | + |
| 147 | + |
| 148 | +if __name__ == "__main__": |
| 149 | + sys.exit(main()) |
0 commit comments