Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

15.9 KB
Raw Blame
#!/usr/bin/env python3
"""Select and update the oldest eligible auto-merge pull request.

The GitHub Actions workflow invokes this module in two separate steps:

* ``select`` uses the short-lived ``GITHUB_TOKEN`` to list and rank PRs.
* ``update`` uses the dedicated ``AUTO_MERGE_PAT_TOKEN`` (exposed as
  ``GH_TOKEN`` only to that step) to revalidate one PR and update its branch.

Keeping the state transition logic here makes it deterministic and unit
testable without exposing a token or changing a real pull request.
"""

from __future__ import annotations

import argparse
import json
import os
import subprocess
import sys
from collections.abc import Sequence
from pathlib import Path
from typing import Any

PR_LIST_FIELDS = (
    "id",
    "number",
    "url",
    "createdAt",
    "state",
    "baseRefName",
    "headRefOid",
    "isCrossRepository",
    "isDraft",
    "maintainerCanModify",
    "mergeable",
    "mergeStateStatus",
    "reviewDecision",
    "autoMergeRequest",
)

PR_FIELDS = (
    *PR_LIST_FIELDS,
    "statusCheckRollup",
)

FAILED_CHECK_RESULTS = frozenset(
    {
        "ACTION_REQUIRED",
        "ERROR",
        "FAILURE",
        "STARTUP_FAILURE",
        "TIMED_OUT",
    }
)

UPDATE_BRANCH_MUTATION = """\
mutation UpdatePullRequestBranch(
  $pullRequestId: ID!
  $expectedHeadOid: GitObjectID!
) {
  updatePullRequestBranch(
    input: {
      pullRequestId: $pullRequestId
      expectedHeadOid: $expectedHeadOid
      updateMethod: MERGE
    }
  ) {
    pullRequest {
      number
    }
  }
}
"""


class UpdaterError(RuntimeError):
    """A safe, user-facing workflow failure."""


def run_gh_json(args: Sequence[str]) -> Any:
    """Run a ``gh`` command and decode its JSON response."""
    command = ["gh", *args]
    try:
        result = subprocess.run(
            command,
            capture_output=True,
            check=False,
            text=True,
            timeout=120,
        )
    except FileNotFoundError as exc:
        raise UpdaterError("gh is not installed on this runner") from exc
    except subprocess.TimeoutExpired as exc:
        raise UpdaterError(
            f"gh command timed out: {' '.join(command)}"
        ) from exc

    if result.returncode != 0:
        detail = (
            result.stderr.strip() or result.stdout.strip() or "unknown error"
        )
        raise UpdaterError(
            f"gh command failed with exit {result.returncode}: {detail}"
        )

    try:
        return json.loads(result.stdout)
    except json.JSONDecodeError as exc:
        raise UpdaterError("gh returned invalid JSON") from exc


def failed_check_names(pull_request: dict[str, Any]) -> list[str]:
    """Return the names of current checks with a red result."""
    rollup = pull_request.get("statusCheckRollup")
    if not isinstance(rollup, list):
        return []

    failures = {
        str(check.get("name") or check.get("context") or "unnamed check")
        for check in rollup
        if isinstance(check, dict)
        and (check.get("conclusion") or check.get("state"))
        in FAILED_CHECK_RESULTS
    }
    return sorted(failures)


def matches_update_state(
    pull_request: dict[str, Any],
    *,
    base: str,
    expected_head_sha: str | None = None,
) -> bool:
    """Return whether a PR has the GitHub state required for an update."""
    number = pull_request.get("number")
    pull_request_id = pull_request.get("id")
    created_at = pull_request.get("createdAt")
    head_sha = pull_request.get("headRefOid")
    is_cross_repository = pull_request.get("isCrossRepository")

    has_valid_identity = (
        type(number) is int
        and number > 0
        and isinstance(pull_request_id, str)
        and bool(pull_request_id)
        and isinstance(created_at, str)
        and bool(created_at)
        and isinstance(head_sha, str)
        and bool(head_sha)
        and type(is_cross_repository) is bool
    )
    head_is_current = expected_head_sha is None or head_sha == expected_head_sha

    return bool(
        has_valid_identity
        and pull_request.get("state") == "OPEN"
        and pull_request.get("baseRefName") == base
        and pull_request.get("isDraft") is False
        and pull_request.get("reviewDecision") == "APPROVED"
        and pull_request.get("autoMergeRequest") is not None
        and pull_request.get("mergeable") == "MERGEABLE"
        and pull_request.get("mergeStateStatus") == "BEHIND"
        and head_is_current
    )


def candidate_skip_reason(pull_request: dict[str, Any]) -> str | None:
    """Explain an operational reason not to update an otherwise-ready PR."""
    if (
        pull_request.get("isCrossRepository") is True
        and pull_request.get("maintainerCanModify") is not True
    ):
        return "fork does not allow maintainer edits"

    rollup = pull_request.get("statusCheckRollup")
    if not isinstance(rollup, list) or not all(
        isinstance(check, dict) for check in rollup
    ):
        return "check status is unavailable"

    failed_checks = failed_check_names(pull_request)
    if failed_checks:
        return f"failed checks: {', '.join(failed_checks)}"
    return None


def is_eligible(
    pull_request: dict[str, Any],
    *,
    base: str,
    expected_head_sha: str | None = None,
) -> bool:
    """Return whether a PR is safe and useful to update right now."""
    return (
        matches_update_state(
            pull_request,
            base=base,
            expected_head_sha=expected_head_sha,
        )
        and candidate_skip_reason(pull_request) is None
    )


def eligible_pull_requests(
    pull_requests: Sequence[dict[str, Any]], *, base: str
) -> list[dict[str, Any]]:
    """Return eligible PRs sorted oldest-first, then by PR number."""
    eligible = [
        pull_request
        for pull_request in pull_requests
        if is_eligible(pull_request, base=base)
    ]
    return sorted(
        eligible,
        key=lambda pull_request: (
            pull_request["createdAt"],
            pull_request["number"],
        ),
    )


def fetch_pull_requests(repository: str, base: str) -> list[dict[str, Any]]:
    """Fetch open, approved PRs, asking GitHub for the oldest page first."""
    response = run_gh_json(
        [
            "pr",
            "list",
            "--repo",
            repository,
            "--state",
            "open",
            "--base",
            base,
            "--search",
            "review:approved sort:created-asc",
            "--limit",
            "1000",
            "--json",
            ",".join(PR_LIST_FIELDS),
        ]
    )
    if not isinstance(response, list) or not all(
        isinstance(item, dict) for item in response
    ):
        raise UpdaterError("gh pr list returned an unexpected response")
    return response


def fetch_pull_request(repository: str, number: int) -> dict[str, Any]:
    """Fetch the current state of one PR for last-moment revalidation."""
    response = run_gh_json(
        [
            "pr",
            "view",
            str(number),
            "--repo",
            repository,
            "--json",
            ",".join(PR_FIELDS),
        ]
    )
    if not isinstance(response, dict):
        raise UpdaterError("gh pr view returned an unexpected response")
    return response


def append_summary(path: Path | None, lines: Sequence[str]) -> None:
    """Append lines to the GitHub step summary when one is available."""
    if path is None:
        return
    with path.open("a", encoding="utf-8") as summary:
        summary.write("\n".join(lines))
        summary.write("\n")


def write_outputs(path: Path | None, values: dict[str, str]) -> None:
    """Write single-line values using the GitHub Actions output protocol."""
    if path is None:
        return
    with path.open("a", encoding="utf-8") as output:
        for name, value in values.items():
            if "\n" in name or "\n" in value:
                raise UpdaterError(
                    "workflow output names and values must be single-line"
                )
            output.write(f"{name}={value}\n")


def select_oldest(
    repository: str,
    base: str,
    *,
    github_output: Path | None,
    step_summary: Path | None,
) -> dict[str, Any] | None:
    """Discover candidates, report all of them, and output the oldest."""
    pull_requests = fetch_pull_requests(repository, base)
    update_state_candidates = sorted(
        (
            pull_request
            for pull_request in pull_requests
            if matches_update_state(pull_request, base=base)
        ),
        key=lambda pull_request: (
            pull_request["createdAt"],
            pull_request["number"],
        ),
    )
    candidates = []
    skipped_candidates = []
    for listed_pull_request in update_state_candidates:
        pull_request = fetch_pull_request(
            repository, listed_pull_request["number"]
        )
        if not matches_update_state(pull_request, base=base):
            continue
        reason = candidate_skip_reason(pull_request)
        if reason is None:
            candidates.append(pull_request)
        else:
            skipped_candidates.append((pull_request, reason))
    candidates.sort(
        key=lambda pull_request: (
            pull_request["createdAt"],
            pull_request["number"],
        ),
    )
    skipped_candidates.sort(
        key=lambda item: (item[0]["createdAt"], item[0]["number"]),
    )
    print(f"Found {len(candidates)} eligible pull request(s).")

    summary_lines = ["## Auto-merge branch updater", ""]
    if not candidates:
        summary_lines.append(
            "No approved, auto-merge-enabled PR with an editable head "
            f"and no failed checks is currently behind `{base}`."
        )
        write_outputs(github_output, {"number": "", "head_sha": ""})
        selected = None
    else:
        summary_lines.extend(["Eligible PRs, oldest first:", ""])
        summary_lines.extend(
            f"- [#{pull_request['number']}]({pull_request['url']}) — "
            f"opened `{pull_request['createdAt']}`"
            for pull_request in candidates
        )

        selected = candidates[0]
        summary_lines.extend(
            ["", f"Selected oldest candidate: #{selected['number']}."]
        )
        write_outputs(
            github_output,
            {
                "number": str(selected["number"]),
                "head_sha": selected["headRefOid"],
            },
        )

    if skipped_candidates:
        summary_lines.extend(["", "Skipped candidates:", ""])
        summary_lines.extend(
            f"- [#{pull_request['number']}]({pull_request['url']}) — {reason}"
            for pull_request, reason in skipped_candidates
        )

    append_summary(step_summary, summary_lines)
    return selected


def update_pull_request(
    repository: str,
    base: str,
    *,
    number: int,
    expected_head_sha: str,
    step_summary: Path | None,
) -> bool:
    """Revalidate and request a branch update; return whether one was sent."""
    current = fetch_pull_request(repository, number)
    if not is_eligible(
        current,
        base=base,
        expected_head_sha=expected_head_sha,
    ):
        message = f"PR #{number} changed after selection and was not updated."
        print(message)
        append_summary(step_summary, ["", message])
        return False

    # Use the same mutation as ``gh pr update-branch``. Passing the PR node ID
    # and revalidated head SHA directly preserves the atomic stale-head guard.
    try:
        response = run_gh_json(
            [
                "api",
                "graphql",
                "-f",
                f"query={UPDATE_BRANCH_MUTATION}",
                "-f",
                f"pullRequestId={current['id']}",
                "-f",
                f"expectedHeadOid={expected_head_sha}",
            ]
        )
    except UpdaterError as exc:
        detail = str(exc)
        fork_auth_errors = (
            "permission to update head repository",
            "without `workflow` scope",
            "without `workflows` permission",
        )
        if current["isCrossRepository"] and any(
            marker in detail for marker in fork_auth_errors
        ):
            raise UpdaterError(
                "AUTO_MERGE_PAT_TOKEN cannot update this editable fork; use "
                "a classic PAT with public_repo and workflow scopes"
            ) from exc
        raise
    try:
        updated_number = response["data"]["updatePullRequestBranch"][
            "pullRequest"
        ]["number"]
    except (KeyError, TypeError) as exc:
        raise UpdaterError(
            "GitHub returned an unexpected branch-update response"
        ) from exc
    if updated_number != number:
        raise UpdaterError(
            "GitHub returned an unexpected pull request after branch update"
        )

    message = "Branch update accepted."
    print(message)
    append_summary(
        step_summary,
        ["", f"Update requested for PR #{number}: {message}"],
    )
    return True


def build_parser() -> argparse.ArgumentParser:
    """Build the command-line parser."""
    parser = argparse.ArgumentParser(description=__doc__)
    subparsers = parser.add_subparsers(dest="command", required=True)

    def add_common_arguments(command: argparse.ArgumentParser) -> None:
        command.add_argument(
            "--repo",
            default=os.environ.get("GITHUB_REPOSITORY"),
            help="GitHub repository in OWNER/REPO form",
        )
        command.add_argument("--base", default="main")
        command.add_argument(
            "--step-summary",
            type=Path,
            default=(
                Path(os.environ["GITHUB_STEP_SUMMARY"])
                if os.environ.get("GITHUB_STEP_SUMMARY")
                else None
            ),
        )

    select_parser = subparsers.add_parser("select")
    add_common_arguments(select_parser)
    select_parser.add_argument(
        "--github-output",
        type=Path,
        default=(
            Path(os.environ["GITHUB_OUTPUT"])
            if os.environ.get("GITHUB_OUTPUT")
            else None
        ),
    )

    update_parser = subparsers.add_parser("update")
    add_common_arguments(update_parser)
    update_parser.add_argument("--pr-number", type=int, required=True)
    update_parser.add_argument("--expected-head-sha", required=True)
    return parser


def validate_repository(repository: str | None) -> str:
    """Validate the OWNER/REPO value before placing it in an API path."""
    if repository is None:
        raise UpdaterError(
            "repository is required; pass --repo or set GITHUB_REPOSITORY"
        )
    owner, separator, name = repository.partition("/")
    if separator != "/" or not owner or not name or "/" in name:
        raise UpdaterError("repository must use OWNER/REPO form")
    return repository


def require_token(command: str) -> None:
    """Fail clearly before invoking ``gh`` when its token is unavailable."""
    if os.environ.get("GH_TOKEN"):
        return
    token_name = (
        "AUTO_MERGE_PAT_TOKEN" if command == "update" else "GITHUB_TOKEN"
    )
    raise UpdaterError(
        f"GH_TOKEN is empty; the workflow must provide {token_name}"
    )


def main(argv: Sequence[str] | None = None) -> int:
    """CLI entry point."""
    parser = build_parser()
    args = parser.parse_args(argv)

    try:
        repository = validate_repository(args.repo)
        require_token(args.command)

        if args.command == "select":
            select_oldest(
                repository,
                args.base,
                github_output=args.github_output,
                step_summary=args.step_summary,
            )
        else:
            update_pull_request(
                repository,
                args.base,
                number=args.pr_number,
                expected_head_sha=args.expected_head_sha,
                step_summary=args.step_summary,
            )
    except UpdaterError as exc:
        print(f"::error::{exc}", file=sys.stderr)
        return 1
    return 0


if __name__ == "__main__":
    raise SystemExit(main())