|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Choose resume versus base refresh from trusted selection and live PR evidence.""" |
| 3 | + |
| 4 | +import argparse |
| 5 | +import json |
| 6 | +from pathlib import Path |
| 7 | +import re |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | + |
| 11 | + |
| 12 | +def positive_integer(value): |
| 13 | + return isinstance(value, int) and not isinstance(value, bool) and value > 0 |
| 14 | + |
| 15 | + |
| 16 | +def selected_pr(selection, branch): |
| 17 | + if not isinstance(selection, dict) or selection.get("error"): |
| 18 | + raise ValueError("Branch preparation requires successful scheduler evidence") |
| 19 | + selected = selection.get("selected") |
| 20 | + if isinstance(selected, dict): |
| 21 | + selected_branch, record = selected.get("branch"), selected |
| 22 | + elif isinstance(selected, str) and selected: |
| 23 | + selected_branch, record = selection.get("head_branch"), selection |
| 24 | + else: |
| 25 | + raise ValueError("Branch preparation requires selected work") |
| 26 | + if selected_branch != branch or "existing_pr" not in record: |
| 27 | + raise ValueError("Selection must identify this exact branch and its PR state") |
| 28 | + number = record["existing_pr"] |
| 29 | + if number is not None and not positive_integer(number): |
| 30 | + raise ValueError("Selected PR must be a positive integer or explicit null") |
| 31 | + return number |
| 32 | + |
| 33 | + |
| 34 | +def github_open_prs(repository, branch): |
| 35 | + result = subprocess.run( |
| 36 | + ["gh", "api", "--method", "GET", f"repos/{repository}/pulls", |
| 37 | + "-f", "state=open", "-f", f"head={repository.split('/')[0]}:{branch}", |
| 38 | + "-F", "per_page=100"], |
| 39 | + check=True, capture_output=True, text=True, timeout=30, |
| 40 | + ) |
| 41 | + return json.loads(result.stdout) |
| 42 | + |
| 43 | + |
| 44 | +def branch_mode(selection, branch, base, repository, fetch_open_prs=github_open_prs): |
| 45 | + number = selected_pr(selection, branch) |
| 46 | + if not re.fullmatch(r"[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+", repository): |
| 47 | + raise ValueError("A valid repository identity is required") |
| 48 | + pull_requests = fetch_open_prs(repository, branch) |
| 49 | + if not isinstance(pull_requests, list) or len(pull_requests) > 1: |
| 50 | + raise ValueError("Expected an unambiguous open-PR list for this branch") |
| 51 | + for pull_request in pull_requests: |
| 52 | + if not isinstance(pull_request, dict): |
| 53 | + raise ValueError("Invalid open-PR evidence") |
| 54 | + head = pull_request.get("head") |
| 55 | + target = pull_request.get("base") |
| 56 | + if (not positive_integer(pull_request.get("number")) or |
| 57 | + pull_request.get("state") != "open" or not isinstance(head, dict) or |
| 58 | + head.get("ref") != branch or not isinstance(head.get("repo"), dict) or |
| 59 | + head["repo"].get("full_name") != repository or not isinstance(target, dict) or |
| 60 | + target.get("ref") != base or not isinstance(target.get("repo"), dict) or |
| 61 | + target["repo"].get("full_name") != repository): |
| 62 | + raise ValueError("Open-PR evidence does not match the canonical repository branch") |
| 63 | + live_number = pull_requests[0]["number"] if pull_requests else None |
| 64 | + if live_number != number: |
| 65 | + raise ValueError("Scheduler PR selection is stale; stop and select again on a later run") |
| 66 | + return "resume" if live_number is not None else "refresh-base" |
| 67 | + |
| 68 | + |
| 69 | +def main(argv=None): |
| 70 | + parser = argparse.ArgumentParser(description=__doc__) |
| 71 | + parser.add_argument("--selection", type=Path, required=True) |
| 72 | + parser.add_argument("--branch", required=True) |
| 73 | + parser.add_argument("--base", required=True) |
| 74 | + parser.add_argument("--repo", required=True) |
| 75 | + args = parser.parse_args(argv) |
| 76 | + try: |
| 77 | + selection = json.loads(args.selection.read_text()) |
| 78 | + print(branch_mode(selection, args.branch, args.base, args.repo)) |
| 79 | + return 0 |
| 80 | + except (OSError, ValueError, subprocess.SubprocessError) as error: |
| 81 | + print(f"Branch preparation blocked: {error}", file=sys.stderr) |
| 82 | + return 1 |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + sys.exit(main()) |
0 commit comments