CI always starts and reports one CI passed check a ruleset can require; a prose-only change skips the jobs inside the run instead of skipping the run
A path filter on the trigger meant a docs-only pull request never got the checks a ruleset would require, so it could never merge. scripts/ci-changes.py decides from the diff whether the jobs have work, and CI passed sums them up, since a skipped matrix job reports under no name a ruleset can hold. The old filter also skipped crates/luxe-cli/guide.md, which the CLI compiles in, and docs/guide/, which the suite checks against the code; both now run the jobs.
This commit is contained in:
56
scripts/ci-changes.py
Normal file
56
scripts/ci-changes.py
Normal file
@@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env python3
|
||||
"""ci-changes — whether a pull request touched anything CI has to build.
|
||||
|
||||
Pull-request CI always runs so it can be a required check; a required check that never starts
|
||||
blocks the merge for good. This decides whether the jobs have work: prose cannot change what the
|
||||
compiler, the suite or the site says, so a prose-only change skips them and reports a pass.
|
||||
|
||||
Prose is only what is provably not an input: Markdown, `docs/`, `.pi/`, the licenses and
|
||||
Dependabot's schedule, except that nothing under `crates/` counts (`luxe-cli` compiles its bundled
|
||||
`guide.md` in) and neither does `docs/guide/`, which the suite checks against the code and the site
|
||||
renders.
|
||||
|
||||
Usage: scripts/ci-changes.py [base-sha] prints `code=true|false` for $GITHUB_OUTPUT.
|
||||
|
||||
When there is nothing to diff against, the jobs run: no base (a run by hand), the all-zero base a
|
||||
push of a new branch reports, or a base the history no longer holds after a force-push. Skipping
|
||||
has to be proven; running never needs to be.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
PROSE = re.compile(r"\.md$|^docs/|^\.pi/|^LICENSE-|^\.github/dependabot\.yml$")
|
||||
INPUTS_AMONG_PROSE = re.compile(r"^crates/|^docs/guide/")
|
||||
|
||||
|
||||
def is_input(path: str) -> bool:
|
||||
"""Whether a change to `path` can change what CI reports."""
|
||||
return bool(INPUTS_AMONG_PROSE.search(path)) or not PROSE.search(path)
|
||||
|
||||
|
||||
def changed_since(base: str) -> list[str] | None:
|
||||
"""The paths changed since HEAD's merge base with `base`, or `None` when that cannot be said."""
|
||||
if re.fullmatch(r"0*", base):
|
||||
return None
|
||||
diff = ["git", "diff", "--name-only", f"{base}...HEAD"]
|
||||
result = subprocess.run(diff, capture_output=True, text=True)
|
||||
if result.returncode != 0:
|
||||
print(f"ci-changes: {' '.join(diff)} failed, so everything runs: {result.stderr.strip()}",
|
||||
file=sys.stderr)
|
||||
return None
|
||||
return result.stdout.split()
|
||||
|
||||
|
||||
def main() -> int:
|
||||
changed = changed_since(sys.argv[1] if len(sys.argv) > 1 else "")
|
||||
code = changed is None or any(is_input(path) for path in changed)
|
||||
print(f"code={str(code).lower()}")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
52
scripts/ci_changes_test.py
Normal file
52
scripts/ci_changes_test.py
Normal file
@@ -0,0 +1,52 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Tests for ci-changes.py. Run: python3 scripts/ci_changes_test.py"""
|
||||
|
||||
import importlib.util
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
# The script's name has a hyphen, like its neighbors, so it is loaded by path rather than imported
|
||||
SPEC = importlib.util.spec_from_file_location("ci_changes", Path(__file__).with_name("ci-changes.py"))
|
||||
assert SPEC and SPEC.loader, "ci-changes.py sits beside this file"
|
||||
cc: Any = importlib.util.module_from_spec(SPEC)
|
||||
SPEC.loader.exec_module(cc)
|
||||
|
||||
|
||||
class IsInput(unittest.TestCase):
|
||||
def test_prose_is_not(self):
|
||||
for path in ["README.md", "CHANGELOG.md", "docs/brand/voice.md", ".pi/x.txt", "LICENSE-MIT",
|
||||
".github/dependabot.yml"]:
|
||||
with self.subTest(path):
|
||||
self.assertFalse(cc.is_input(path))
|
||||
|
||||
def test_markdown_the_build_or_suite_reads_is(self):
|
||||
# luxe-cli compiles its guide bundle in; the suite checks docs/guide against the code
|
||||
for path in ["crates/luxe-cli/guide.md", "crates/luxe-core/README.md", "docs/guide/index.md"]:
|
||||
with self.subTest(path):
|
||||
self.assertTrue(cc.is_input(path))
|
||||
|
||||
def test_everything_else_is(self):
|
||||
for path in ["crates/luxe-core/src/lib.rs", "Cargo.lock", "web/src/pages/index.astro",
|
||||
".github/workflows/ci.yml", "scripts/ci-changes.py", "docs.rs", "mdbook.toml"]:
|
||||
with self.subTest(path):
|
||||
self.assertTrue(cc.is_input(path))
|
||||
|
||||
|
||||
class NothingToDiff(unittest.TestCase):
|
||||
"""Skipping has to be proven: without a base to diff against, everything runs."""
|
||||
|
||||
def test_no_base_or_a_new_branch_cannot_be_diffed(self):
|
||||
for base in ["", "0000000000000000000000000000000000000000"]:
|
||||
with self.subTest(base):
|
||||
self.assertIsNone(cc.changed_since(base))
|
||||
|
||||
def test_a_base_the_history_lacks_cannot_be_diffed(self):
|
||||
self.assertIsNone(cc.changed_since("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"))
|
||||
|
||||
def test_a_real_base_lists_what_changed(self):
|
||||
self.assertIsInstance(cc.changed_since("HEAD"), list)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user