import re
import sys
from pathlib import Path
USES = re.compile(r"^\s*(?:-\s*)?uses:\s*(?P<value>[^#\s]+)")
SHA = re.compile(r"^[0-9a-f]{40}$")
DOCKER_DIGEST = re.compile(r"^docker://.+\@sha256:[0-9a-f]{64}$")
def check_line(value: str) -> str | None:
if value.startswith("./"):
return None if value.startswith("docker://"):
if DOCKER_DIGEST.match(value):
return None
return f"{value!r} is not pinned by digest (expected docker://image@sha256:<64 hex>)"
if "@" not in value:
return f"{value!r} has no ref at all; pin it to a full-length commit SHA"
repo, ref = value.rsplit("@", 1)
if SHA.match(ref):
return None
return (
f"{value!r} uses the mutable ref {ref!r}; pin it to the full-length "
f"commit SHA that {ref} resolves to, keeping {ref} in a trailing comment"
)
def files(root: Path, given: list[str]) -> list[Path]:
if given:
return [Path(p) for p in given]
found = sorted(root.glob(".github/workflows/*.y*ml"))
found += sorted(root.glob(".github/actions/*/action.y*ml"))
return found
def main(argv: list[str]) -> int:
root = Path(__file__).resolve().parent.parent
targets = files(root, argv)
if not targets:
print("actions: no workflow files found", file=sys.stderr)
return 0
failures = []
for path in targets:
for number, line in enumerate(path.read_text().splitlines(), 1):
m = USES.match(line)
if not m:
continue
error = check_line(m.group("value"))
if error:
failures.append((path, number, error))
for path, number, error in failures:
try:
shown = path.relative_to(root)
except ValueError:
shown = path
print(f"{shown}:{number}: {error}", file=sys.stderr)
if failures:
print(
f"\n{len(failures)} action reference(s) are not pinned to a commit SHA. "
"The cipansor org rejects unpinned actions at workflow start.",
file=sys.stderr,
)
return 1
print(f"actions: {len(targets)} file(s) checked, every action pinned to a commit SHA")
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))