|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Detect which packages have changed in a PR or push to main. |
| 3 | +
|
| 4 | +Includes dependency-aware propagation: when a package changes, all |
| 5 | +downstream dependents are also included in the test list. |
| 6 | +""" |
| 7 | + |
| 8 | +import json |
| 9 | +import os |
| 10 | +import subprocess |
| 11 | +import sys |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +# Internal dependency graph: package -> packages that depend on it. |
| 15 | +# When a package changes, its dependents' tests also run. |
| 16 | +# Add new entries here as packages are added to the monorepo. |
| 17 | +# External dependents (uipath-langchain, uipath-runtime, etc.) are |
| 18 | +# handled separately via labeler.yml auto-labels. |
| 19 | +DEPENDENTS: dict[str, list[str]] = { |
| 20 | + "uipath-core": ["uipath-platform", "uipath"], |
| 21 | + "uipath-platform": ["uipath"], |
| 22 | +} |
| 23 | + |
| 24 | + |
| 25 | +def expand_with_dependents(changed: list[str], all_packages: list[str]) -> list[str]: |
| 26 | + """Expand changed package list to include downstream dependents.""" |
| 27 | + expanded = set(changed) |
| 28 | + for pkg in changed: |
| 29 | + for dep in DEPENDENTS.get(pkg, []): |
| 30 | + if dep in all_packages: |
| 31 | + expanded.add(dep) |
| 32 | + return sorted(expanded) |
| 33 | + |
| 34 | + |
| 35 | +def get_all_packages() -> list[str]: |
| 36 | + """Get all packages in the monorepo.""" |
| 37 | + packages_dir = Path("packages") |
| 38 | + packages = [] |
| 39 | + |
| 40 | + for item in packages_dir.iterdir(): |
| 41 | + if item.is_dir() and (item / "pyproject.toml").exists(): |
| 42 | + packages.append(item.name) |
| 43 | + |
| 44 | + return sorted(packages) |
| 45 | + |
| 46 | + |
| 47 | +def get_changed_packages(base_sha: str, head_sha: str) -> list[str]: |
| 48 | + """Get packages that have changed between two commits.""" |
| 49 | + try: |
| 50 | + # Get changed files |
| 51 | + result = subprocess.run( |
| 52 | + ["git", "diff", "--name-only", f"{base_sha}...{head_sha}"], |
| 53 | + capture_output=True, |
| 54 | + text=True, |
| 55 | + check=True, |
| 56 | + ) |
| 57 | + |
| 58 | + changed_files = result.stdout.strip().split("\n") |
| 59 | + |
| 60 | + # Extract package names from paths like "packages/uipath-llamaindex/..." |
| 61 | + changed_packages = set() |
| 62 | + for file_path in changed_files: |
| 63 | + if file_path.startswith("packages/"): |
| 64 | + parts = file_path.split("/") |
| 65 | + if len(parts) >= 2: |
| 66 | + package_name = parts[1] |
| 67 | + # Verify it's a real package |
| 68 | + if (Path("packages") / package_name / "pyproject.toml").exists(): |
| 69 | + changed_packages.add(package_name) |
| 70 | + |
| 71 | + return sorted(changed_packages) |
| 72 | + |
| 73 | + except subprocess.CalledProcessError as e: |
| 74 | + print(f"Error running git diff: {e}", file=sys.stderr) |
| 75 | + return [] |
| 76 | + |
| 77 | + |
| 78 | +def get_changed_packages_auto() -> list[str]: |
| 79 | + """Auto-detect changed packages using git.""" |
| 80 | + try: |
| 81 | + # Try to detect changes against origin/main |
| 82 | + result = subprocess.run( |
| 83 | + ["git", "diff", "--name-only", "origin/main...HEAD"], |
| 84 | + capture_output=True, |
| 85 | + text=True, |
| 86 | + check=True, |
| 87 | + ) |
| 88 | + |
| 89 | + changed_files = result.stdout.strip().split("\n") |
| 90 | + |
| 91 | + # Extract package names |
| 92 | + changed_packages = set() |
| 93 | + for file_path in changed_files: |
| 94 | + if file_path.startswith("packages/"): |
| 95 | + parts = file_path.split("/") |
| 96 | + if len(parts) >= 2: |
| 97 | + package_name = parts[1] |
| 98 | + if (Path("packages") / package_name / "pyproject.toml").exists(): |
| 99 | + changed_packages.add(package_name) |
| 100 | + |
| 101 | + return sorted(changed_packages) |
| 102 | + |
| 103 | + except (subprocess.CalledProcessError, Exception) as e: |
| 104 | + print(f"Warning: Could not auto-detect changes: {e}", file=sys.stderr) |
| 105 | + return [] |
| 106 | + |
| 107 | + |
| 108 | +def main(): |
| 109 | + """Main entry point.""" |
| 110 | + event_name = os.getenv("GITHUB_EVENT_NAME", "") |
| 111 | + base_sha = os.getenv("BASE_SHA", "") |
| 112 | + head_sha = os.getenv("HEAD_SHA", "") |
| 113 | + |
| 114 | + all_packages = get_all_packages() |
| 115 | + |
| 116 | + # If we have explicit SHAs (from PR or push), detect changed packages |
| 117 | + if base_sha and head_sha: |
| 118 | + packages = get_changed_packages(base_sha, head_sha) |
| 119 | + event_type = "pull request" if event_name == "pull_request" else "push" |
| 120 | + print(f"{event_type.capitalize()} - detected {len(packages)} directly changed package(s):") |
| 121 | + for pkg in packages: |
| 122 | + print(f" - {pkg}") |
| 123 | + |
| 124 | + # workflow_call or missing context - try auto-detection |
| 125 | + else: |
| 126 | + print(f"Event: {event_name or 'workflow_call'} - attempting auto-detection") |
| 127 | + packages = get_changed_packages_auto() |
| 128 | + |
| 129 | + if packages: |
| 130 | + print(f"Auto-detected {len(packages)} directly changed package(s):") |
| 131 | + for pkg in packages: |
| 132 | + print(f" - {pkg}") |
| 133 | + else: |
| 134 | + # Fallback: test all packages |
| 135 | + print("Could not detect changes - testing all packages") |
| 136 | + packages = all_packages |
| 137 | + for pkg in packages: |
| 138 | + print(f" - {pkg}") |
| 139 | + |
| 140 | + # Expand with downstream dependents |
| 141 | + expanded = expand_with_dependents(packages, all_packages) |
| 142 | + added = sorted(set(expanded) - set(packages)) |
| 143 | + if added: |
| 144 | + print(f"\nAdded {len(added)} dependent package(s):") |
| 145 | + for pkg in added: |
| 146 | + print(f" - {pkg}") |
| 147 | + packages = expanded |
| 148 | + |
| 149 | + # Output as JSON for GitHub Actions |
| 150 | + packages_json = json.dumps(packages) |
| 151 | + print(f"\nPackages JSON: {packages_json}") |
| 152 | + |
| 153 | + # Write to GitHub output |
| 154 | + github_output = os.getenv("GITHUB_OUTPUT") |
| 155 | + if github_output: |
| 156 | + with open(github_output, "a") as f: |
| 157 | + f.write(f"packages={packages_json}\n") |
| 158 | + f.write(f"count={len(packages)}\n") |
| 159 | + |
| 160 | + |
| 161 | +if __name__ == "__main__": |
| 162 | + main() |
0 commit comments