lib/checks/detect-changes.py

46 lines · 1232 bytes

 1"""
 2Detects which files changed between the PR base and HEAD.
 3Sets GitHub Actions output: yaml_changed.
 4"""
 5
 6import argparse
 7import os
 8import subprocess
 9import sys
10
11PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
12
13YAML_FILE = "awesome-privacy.yml"
14
15
16def write_github_output(name, value):
17    output_file = os.environ.get("GITHUB_OUTPUT")
18    if output_file:
19        with open(output_file, "a") as f:
20            f.write(f"{name}={value}\n")
21
22
23def main():
24    parser = argparse.ArgumentParser(description="Detect changed files in a PR")
25    parser.add_argument("--base-ref", required=True, help="Base git ref to diff against")
26    args = parser.parse_args()
27
28    result = subprocess.run(
29        ["git", "diff", "--name-only", f"{args.base_ref}..HEAD"],
30        capture_output=True, text=True, check=True,
31        cwd=PROJECT_ROOT,
32    )
33
34    changed_files = [f for f in result.stdout.strip().splitlines() if f]
35
36    print("Changed files:")
37    for f in changed_files:
38        print(f"  {f}")
39
40    yaml_changed = YAML_FILE in changed_files
41    write_github_output("yaml_changed", str(yaml_changed).lower())
42    print(f"yaml_changed={yaml_changed}")
43
44
45if __name__ == "__main__":
46    main()