#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Classify a pull request for the documentation-only CI shortcut.""" import os from pathlib import Path import subprocess import sys ROOT = Path(__file__).resolve().parents[1] DOC_FILES = {"README.md", "CONTRIBUTING.md", "CHANGELOG.md"} def documentation_only(paths: list[str]) -> bool: return bool(paths) and all( path in DOC_FILES or (path.startswith("docs/") and Path(path).suffix in {".md", ".txt"}) for path in paths ) def changed_paths() -> list[str]: if os.environ.get("GITHUB_EVENT_NAME") != "pull_request": raise ValueError("not a pull request") if os.environ.get("GITHUB_BASE_REF") != "main": raise ValueError("unknown base branch") merge_base = subprocess.check_output( ["git", "-C", str(ROOT), "merge-base", "HEAD", "refs/remotes/origin/main"], stderr=subprocess.DEVNULL, text=True, ).strip() if len(merge_base) != 40: raise ValueError("missing merge base") raw = subprocess.check_output( ["git", "-C", str(ROOT), "diff", "--name-only", "-z", merge_base, "HEAD"], stderr=subprocess.DEVNULL, ) return [path.decode("utf-8", "surrogateescape") for path in raw.split(b"\0") if path] def main() -> None: try: paths = changed_paths() docs = documentation_only(paths) except (OSError, subprocess.CalledProcessError, ValueError): paths = [] docs = False print(f"surface={'documentation' if docs else 'full'}") print(f"Changed paths: {len(paths)}", file=sys.stderr) if __name__ == "__main__": main()