|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# /// script |
| 3 | +# dependencies = [ |
| 4 | +# "pygithub", |
| 5 | +# "pyyaml", |
| 6 | +# ] |
| 7 | +# /// |
| 8 | +import os |
| 9 | +import sys |
| 10 | +from triage.github_api import GitHubAPIClient |
| 11 | +from triage.rules_engine import RulesEngine |
| 12 | +from triage.rules import StalePRRule |
| 13 | + |
| 14 | +def main(): |
| 15 | + token = os.environ.get("GH_TOKEN") or os.environ.get("GITHUB_TOKEN") |
| 16 | + if not token: |
| 17 | + print("[ERROR] GH_TOKEN or GITHUB_TOKEN is not set.") |
| 18 | + sys.exit(1) |
| 19 | + |
| 20 | + # Extract active repository name dynamically from the git config of the local clone. |
| 21 | + try: |
| 22 | + import subprocess |
| 23 | + script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 24 | + origin_url = subprocess.check_output(["git", "-C", script_dir, "config", "--get", "remote.origin.url"]).decode("utf-8").strip() |
| 25 | + clean_url = origin_url.replace(".git", "").replace(":", "/") |
| 26 | + parts = clean_url.split("/") |
| 27 | + repo_name = f"{parts[-2]}/{parts[-1]}" |
| 28 | + print(f"[INFO] Target Repository resolved: {repo_name}") |
| 29 | + except Exception as e: |
| 30 | + print(f"[ERROR] Failed to dynamically determine current Git repository name: {e}") |
| 31 | + sys.exit(1) |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + # Configure thresholds (stale after 30 days, abandon candidate after 37 days) |
| 37 | + STALE_THRESHOLD_DAYS = 30 |
| 38 | + ABANDON_THRESHOLD_DAYS = 37 |
| 39 | + |
| 40 | + print(f"[START] Inactivity Scan: Scanning open PRs in '{repo_name}'...") |
| 41 | + |
| 42 | + try: |
| 43 | + # Initialize client and engine |
| 44 | + client = GitHubAPIClient(token, repo_name) |
| 45 | + engine = RulesEngine(client) |
| 46 | + |
| 47 | + # Register only stale/abandon inactivity rules |
| 48 | + engine.add_rule(StalePRRule( |
| 49 | + stale_threshold_days=STALE_THRESHOLD_DAYS, |
| 50 | + abandon_threshold_days=ABANDON_THRESHOLD_DAYS |
| 51 | + )) |
| 52 | + |
| 53 | + # Fetch all currently open pull requests |
| 54 | + pulls = client.repo.get_pulls(state="open") |
| 55 | + total_scanned = 0 |
| 56 | + |
| 57 | + for pygithub_pr in pulls: |
| 58 | + if pygithub_pr.draft: |
| 59 | + continue |
| 60 | + |
| 61 | + total_scanned += 1 |
| 62 | + print(f" - [SCANNING] PR #{pygithub_pr.number}: '{pygithub_pr.title}' (Updated at: {pygithub_pr.updated_at})") |
| 63 | + |
| 64 | + try: |
| 65 | + # Wrap the PR inside our shared context model and run engine |
| 66 | + context = client.get_pr_context(pygithub_pr.number, event_name="schedule") |
| 67 | + engine.run(context) |
| 68 | + except Exception as pe: |
| 69 | + print(f" [ERROR] Failed to run stale evaluation on PR #{pygithub_pr.number}: {pe}", file=sys.stderr) |
| 70 | + |
| 71 | + print(f"[SUCCESS] Inactivity Scan complete. Scanned {total_scanned} open non-draft pull requests.") |
| 72 | + except Exception as e: |
| 73 | + print(f"[ERROR] Inactivity scan runner failed: {e}", file=sys.stderr) |
| 74 | + sys.exit(1) |
| 75 | + |
| 76 | +if __name__ == "__main__": |
| 77 | + main() |
0 commit comments