|
| 1 | +"""The GitHub Actions examples stay pinned, current, and safe to require. |
| 2 | +
|
| 3 | +Dependabot updates the actions in `.github/workflows`, but not the copy-paste |
| 4 | +templates in `workflows/` or the snippets in the docs, which is how the templates |
| 5 | +fell four majors behind. These tests fail when an example uses an unpinned action |
| 6 | +or a different commit than the repository's own CI, so a Dependabot bump surfaces |
| 7 | +the examples that need the same update. |
| 8 | +""" |
| 9 | +import re |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +import pytest |
| 13 | + |
| 14 | +ROOT = Path(__file__).resolve().parents[2] |
| 15 | +TEMPLATES = sorted((ROOT / "workflows").glob("github-actions*.yml")) |
| 16 | +EXAMPLES = TEMPLATES + [ROOT / "docs" / "ci-cd.md"] |
| 17 | +USES = re.compile(r"uses:\s*(?P<action>[\w.-]+/[\w./-]+)@(?P<ref>\S+)(?P<comment>[^\n]*)") |
| 18 | +PINNED = re.compile(r"^[0-9a-f]{40}$") |
| 19 | + |
| 20 | + |
| 21 | +def _uses(path): |
| 22 | + return [m.groupdict() | {"path": path.relative_to(ROOT)} for m in USES.finditer(path.read_text())] |
| 23 | + |
| 24 | + |
| 25 | +def _ci_pins(): |
| 26 | + pins = {} |
| 27 | + files = list((ROOT / ".github" / "workflows").glob("*.yml")) |
| 28 | + files += list((ROOT / ".github" / "actions").glob("*/action.yml")) |
| 29 | + for path in files: |
| 30 | + for use in _uses(path): |
| 31 | + if PINNED.match(use["ref"]): |
| 32 | + pins.setdefault(use["action"], set()).add(use["ref"]) |
| 33 | + return pins |
| 34 | + |
| 35 | + |
| 36 | +def test_the_templates_exist(): |
| 37 | + assert {path.name for path in TEMPLATES} == { |
| 38 | + "github-actions.yml", |
| 39 | + "github-actions-push.yml", |
| 40 | + "github-actions-comments.yml", |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.parametrize("path", EXAMPLES, ids=lambda path: path.name) |
| 45 | +def test_examples_pin_actions_to_a_commit_with_its_version(path): |
| 46 | + unpinned = [ |
| 47 | + f"{use['path']}: {use['action']}@{use['ref']}" |
| 48 | + for use in _uses(path) |
| 49 | + if not PINNED.match(use["ref"]) or not re.search(r"#\s*v\d", use["comment"]) |
| 50 | + ] |
| 51 | + assert not unpinned, "pin these to a full commit SHA with a `# vX.Y.Z` comment: " + ", ".join(unpinned) |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.parametrize("path", EXAMPLES, ids=lambda path: path.name) |
| 55 | +def test_examples_use_the_same_commit_as_ci(path): |
| 56 | + pins = _ci_pins() |
| 57 | + stale = [ |
| 58 | + f"{use['path']}: {use['action']}@{use['ref'][:12]} (CI uses " |
| 59 | + f"{', '.join(sorted(ref[:12] for ref in pins[use['action']]))})" |
| 60 | + for use in _uses(path) |
| 61 | + if use["action"] in pins and use["ref"] not in pins[use["action"]] |
| 62 | + ] |
| 63 | + assert not stale, "update these to the commit .github/workflows uses: " + "; ".join(stale) |
| 64 | + |
| 65 | + |
| 66 | +def test_only_the_pull_request_workflow_reports_the_required_check(): |
| 67 | + """GitHub counts a skipped job as passing, so the required check needs its own workflow.""" |
| 68 | + pr_workflow = (ROOT / "workflows" / "github-actions.yml").read_text() |
| 69 | + on_block = re.search(r"^on:\n((?:[ \t]+.*\n|\n)*)", pr_workflow, re.M).group(1) |
| 70 | + triggers = re.findall(r"^ (\w+):", on_block, re.M) |
| 71 | + assert triggers == ["pull_request"] |
| 72 | + for path in TEMPLATES: |
| 73 | + names = re.findall(r"^ ([\w-]+):\n|^ name: ([\w-]+)$", path.read_text(), re.M) |
| 74 | + reported = {job or name for job, name in names} |
| 75 | + if path.name == "github-actions.yml": |
| 76 | + assert "socket-security" in reported |
| 77 | + else: |
| 78 | + assert "socket-security" not in reported, f"{path.name} reports the required check name" |
0 commit comments