lib/checks/detect-changes.py
53 lines · 1489 bytes
1"""
2Detects which files changed between the PR base and HEAD.
3Sets GitHub Actions output: yaml_changed.
4"""
5
6import argparse
7import logging
8import os
9import subprocess
10import sys
11
12sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
13import utils
14
15utils.setup_logging()
16logger = logging.getLogger(__name__)
17
18PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
19
20YAML_FILE = "awesome-privacy.yml"
21
22
23def write_github_output(name, value):
24 output_file = os.environ.get("GITHUB_OUTPUT")
25 if output_file:
26 with open(output_file, "a") as f:
27 f.write(f"{name}={value}\n")
28
29
30def main():
31 parser = argparse.ArgumentParser(description="Detect changed files in a PR")
32 parser.add_argument("--base-ref", required=True, help="Base git ref to diff against")
33 args = parser.parse_args()
34
35 result = subprocess.run(
36 ["git", "diff", "--name-only", f"{args.base_ref}..HEAD"],
37 capture_output=True, text=True, check=True,
38 cwd=PROJECT_ROOT,
39 )
40
41 changed_files = [f for f in result.stdout.strip().splitlines() if f]
42
43 logger.info("Detected %d changed file(s) since %s", len(changed_files), args.base_ref)
44 for f in changed_files:
45 logger.debug(" %s", f)
46
47 yaml_changed = YAML_FILE in changed_files
48 write_github_output("yaml_changed", str(yaml_changed).lower())
49 logger.info("%s changed: %s", YAML_FILE, yaml_changed)
50
51
52if __name__ == "__main__":
53 main()