|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import subprocess |
| 5 | +from pathlib import Path, PurePosixPath |
| 6 | + |
| 7 | +FULL_SUITE = "all" |
| 8 | + |
| 9 | + |
| 10 | +def _changed_files(base_sha: str, head_sha: str) -> list[str]: |
| 11 | + result = subprocess.run( |
| 12 | + ["git", "diff", "--name-only", "--no-renames", f"{base_sha}...{head_sha}"], |
| 13 | + check=True, |
| 14 | + stdout=subprocess.PIPE, |
| 15 | + text=True, |
| 16 | + ) |
| 17 | + return [line for line in result.stdout.splitlines() if line] |
| 18 | + |
| 19 | + |
| 20 | +def _task_from_path(path: str, tasks_root: Path) -> str | None: |
| 21 | + parts = PurePosixPath(path).parts |
| 22 | + if len(parts) < 3 or parts[0] != "tasks" or parts[1] == "common": |
| 23 | + return None |
| 24 | + |
| 25 | + task_id = parts[1] |
| 26 | + if not (tasks_root / task_id).is_dir(): |
| 27 | + return None |
| 28 | + |
| 29 | + return task_id |
| 30 | + |
| 31 | + |
| 32 | +def detect_scope(changed_files: list[str], tasks_root: Path) -> tuple[str, bool]: |
| 33 | + task_ids = set() |
| 34 | + |
| 35 | + for changed_file in changed_files: |
| 36 | + task_id = _task_from_path(changed_file, tasks_root) |
| 37 | + if task_id is None: |
| 38 | + return FULL_SUITE, False |
| 39 | + task_ids.add(task_id) |
| 40 | + |
| 41 | + if not task_ids: |
| 42 | + return FULL_SUITE, False |
| 43 | + |
| 44 | + return ";".join(sorted(task_ids)), True |
| 45 | + |
| 46 | + |
| 47 | +def _write_github_output(github_output: Path, scope: str, task_scoped: bool) -> None: |
| 48 | + with github_output.open("a", encoding="utf-8") as output: |
| 49 | + output.write(f"ppc_tasks={scope}\n") |
| 50 | + output.write(f"task_scoped={str(task_scoped).lower()}\n") |
| 51 | + |
| 52 | + |
| 53 | +def _parse_args() -> argparse.Namespace: |
| 54 | + parser = argparse.ArgumentParser( |
| 55 | + description="Detect the PPC_TASKS value for CI from changed PR paths.", |
| 56 | + ) |
| 57 | + parser.add_argument( |
| 58 | + "--base-sha", required=True, help="Base commit for the PR diff." |
| 59 | + ) |
| 60 | + parser.add_argument( |
| 61 | + "--head-sha", required=True, help="Head commit for the PR diff." |
| 62 | + ) |
| 63 | + parser.add_argument( |
| 64 | + "--tasks-root", default="tasks", type=Path, help="Path to the tasks directory." |
| 65 | + ) |
| 66 | + parser.add_argument( |
| 67 | + "--github-output", |
| 68 | + type=Path, |
| 69 | + help="Optional GITHUB_OUTPUT file to append workflow outputs.", |
| 70 | + ) |
| 71 | + return parser.parse_args() |
| 72 | + |
| 73 | + |
| 74 | +def main() -> None: |
| 75 | + args = _parse_args() |
| 76 | + scope, task_scoped = detect_scope( |
| 77 | + _changed_files(args.base_sha, args.head_sha), args.tasks_root |
| 78 | + ) |
| 79 | + |
| 80 | + print(f"PPC_TASKS={scope}") |
| 81 | + if args.github_output is not None: |
| 82 | + _write_github_output(args.github_output, scope, task_scoped) |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + main() |
0 commit comments