|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Detect which packages have changed in a PR or push to main.""" |
| 3 | + |
| 4 | +import json |
| 5 | +import os |
| 6 | +import subprocess |
| 7 | +import sys |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | + |
| 11 | +def get_all_packages() -> list[str]: |
| 12 | + """Get all packages in the monorepo.""" |
| 13 | + packages_dir = Path("packages") |
| 14 | + packages = [] |
| 15 | + |
| 16 | + for item in packages_dir.iterdir(): |
| 17 | + if item.is_dir() and (item / "pyproject.toml").exists(): |
| 18 | + packages.append(item.name) |
| 19 | + |
| 20 | + return sorted(packages) |
| 21 | + |
| 22 | + |
| 23 | +def get_changed_packages(base_sha: str, head_sha: str) -> list[str]: |
| 24 | + """Get packages that have changed between two commits.""" |
| 25 | + try: |
| 26 | + # Get changed files |
| 27 | + result = subprocess.run( |
| 28 | + ["git", "diff", "--name-only", f"{base_sha}...{head_sha}"], |
| 29 | + capture_output=True, |
| 30 | + text=True, |
| 31 | + check=True, |
| 32 | + ) |
| 33 | + |
| 34 | + changed_files = result.stdout.strip().split("\n") |
| 35 | + |
| 36 | + # Extract package names from paths like "packages/uipath-llamaindex/..." |
| 37 | + changed_packages = set() |
| 38 | + for file_path in changed_files: |
| 39 | + if file_path.startswith("packages/"): |
| 40 | + parts = file_path.split("/") |
| 41 | + if len(parts) >= 2: |
| 42 | + package_name = parts[1] |
| 43 | + # Verify it's a real package |
| 44 | + if (Path("packages") / package_name / "pyproject.toml").exists(): |
| 45 | + changed_packages.add(package_name) |
| 46 | + |
| 47 | + return sorted(changed_packages) |
| 48 | + |
| 49 | + except subprocess.CalledProcessError as e: |
| 50 | + print(f"Error running git diff: {e}", file=sys.stderr) |
| 51 | + return [] |
| 52 | + |
| 53 | + |
| 54 | +def main(): |
| 55 | + """Main entry point.""" |
| 56 | + event_name = os.getenv("GITHUB_EVENT_NAME", "") |
| 57 | + base_sha = os.getenv("BASE_SHA", "") |
| 58 | + head_sha = os.getenv("HEAD_SHA", "") |
| 59 | + |
| 60 | + # On push to main, test all packages |
| 61 | + if event_name == "push": |
| 62 | + packages = get_all_packages() |
| 63 | + print(f"Push to main - testing all {len(packages)} packages:") |
| 64 | + for pkg in packages: |
| 65 | + print(f" - {pkg}") |
| 66 | + |
| 67 | + # On PR, detect changed packages |
| 68 | + elif event_name == "pull_request": |
| 69 | + if not base_sha or not head_sha: |
| 70 | + print( |
| 71 | + "Error: BASE_SHA and HEAD_SHA must be set for pull requests", |
| 72 | + file=sys.stderr, |
| 73 | + ) |
| 74 | + sys.exit(1) |
| 75 | + |
| 76 | + packages = get_changed_packages(base_sha, head_sha) |
| 77 | + print(f"Pull request - detected {len(packages)} changed package(s):") |
| 78 | + for pkg in packages: |
| 79 | + print(f" - {pkg}") |
| 80 | + |
| 81 | + else: |
| 82 | + print(f"Unknown event: {event_name}", file=sys.stderr) |
| 83 | + sys.exit(1) |
| 84 | + |
| 85 | + # Output as JSON for GitHub Actions |
| 86 | + packages_json = json.dumps(packages) |
| 87 | + print(f"\nPackages JSON: {packages_json}") |
| 88 | + |
| 89 | + # Write to GitHub output |
| 90 | + github_output = os.getenv("GITHUB_OUTPUT") |
| 91 | + if github_output: |
| 92 | + with open(github_output, "a") as f: |
| 93 | + f.write(f"packages={packages_json}\n") |
| 94 | + f.write(f"count={len(packages)}\n") |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + main() |
0 commit comments