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.

17.3 KB
Raw Blame
"""Tests for the twice-hourly auto-merge pull-request branch updater.

The production script is exercised with mocked ``gh`` responses. No test uses
GitHub credentials, calls the network, updates a branch, or merges a PR.
"""

from __future__ import annotations

import importlib.util
from pathlib import Path

import pytest
import yaml

REPO_ROOT = Path(__file__).resolve().parents[2]
SCRIPT = REPO_ROOT / "scripts" / "ci" / "update_oldest_auto_merge_pr.py"
WORKFLOW = (
    REPO_ROOT / ".github" / "workflows" / "update-oldest-auto-merge-pr.yml"
)
HEAD_A = "a" * 40
HEAD_B = "b" * 40

_SPEC = importlib.util.spec_from_file_location(
    "update_oldest_auto_merge_pr", SCRIPT
)
assert _SPEC is not None and _SPEC.loader is not None
updater = importlib.util.module_from_spec(_SPEC)
_SPEC.loader.exec_module(updater)


def pull_request(
    number: int = 100,
    created_at: str = "2026-01-02T03:04:05Z",
    **overrides,
):
    """Return a minimal PR that satisfies every update requirement."""
    result = {
        "id": f"PR_example_{number}",
        "number": number,
        "url": f"https://github.com/example/project/pull/{number}",
        "createdAt": created_at,
        "state": "OPEN",
        "baseRefName": "main",
        "headRefOid": HEAD_A,
        "isCrossRepository": False,
        "isDraft": False,
        "maintainerCanModify": False,
        "mergeable": "MERGEABLE",
        "mergeStateStatus": "BEHIND",
        "reviewDecision": "APPROVED",
        "autoMergeRequest": {"enabledAt": "2026-01-02T04:00:00Z"},
        "statusCheckRollup": [],
    }
    result.update(overrides)
    return result


@pytest.mark.parametrize(
    "change",
    [
        {"state": "CLOSED"},
        {"baseRefName": "release"},
        {"isDraft": True},
        {"reviewDecision": "REVIEW_REQUIRED"},
        {"autoMergeRequest": None},
        {"mergeable": "CONFLICTING"},
        {"mergeStateStatus": "CLEAN"},
        {"mergeStateStatus": "UNKNOWN"},
        {"isCrossRepository": True, "maintainerCanModify": False},
    ],
    ids=[
        "closed",
        "wrong-base",
        "draft",
        "not-approved",
        "auto-merge-disabled",
        "conflict",
        "already-current",
        "mergeability-unknown",
        "unmodifiable-fork",
    ],
)
def test_ineligible_pull_requests_are_rejected(change):
    assert not updater.is_eligible(pull_request(**change), base="main")


def test_modifiable_fork_is_eligible():
    candidate = pull_request(
        isCrossRepository=True,
        maintainerCanModify=True,
    )

    assert updater.is_eligible(candidate, base="main")


@pytest.mark.parametrize(
    "check",
    [
        {"name": "unit-tests", "conclusion": "FAILURE"},
        {"name": "lint", "conclusion": "TIMED_OUT"},
        {"name": "policy", "conclusion": "ACTION_REQUIRED"},
        {"name": "bootstrap", "conclusion": "STARTUP_FAILURE"},
        {"context": "legacy-ci", "state": "ERROR"},
    ],
    ids=["failure", "timeout", "action-required", "startup", "status-error"],
)
def test_red_check_is_skipped_even_when_optional(check):
    candidate = pull_request(statusCheckRollup=[check])

    assert not updater.is_eligible(candidate, base="main")


@pytest.mark.parametrize(
    "conclusion",
    ["SUCCESS", "SKIPPED", "NEUTRAL", "CANCELLED", "STALE", None],
)
def test_non_red_check_does_not_block_update(conclusion):
    candidate = pull_request(
        statusCheckRollup=[{"name": "optional-check", "conclusion": conclusion}]
    )

    assert updater.is_eligible(candidate, base="main")


def test_failed_candidate_becomes_eligible_after_checks_pass():
    candidate = pull_request(
        statusCheckRollup=[{"name": "tests", "conclusion": "FAILURE"}]
    )

    assert not updater.is_eligible(candidate, base="main")
    candidate["statusCheckRollup"][0]["conclusion"] = "SUCCESS"
    assert updater.is_eligible(candidate, base="main")


def test_candidates_are_sorted_oldest_first_then_by_number():
    candidates = [
        pull_request(30, "2026-03-01T00:00:00Z"),
        pull_request(20, "2026-01-01T00:00:00Z"),
        pull_request(10, "2026-01-01T00:00:00Z"),
    ]

    selected = updater.eligible_pull_requests(candidates, base="main")

    assert [item["number"] for item in selected] == [10, 20, 30]


def test_pr_can_become_eligible_again_each_time_main_advances():
    candidate = pull_request(42, mergeStateStatus="CLEAN")

    # It was just updated and is current with main, so there is no work.
    assert updater.eligible_pull_requests([candidate], base="main") == []

    # Another PR merges without waiting for this PR's CI, advancing main.
    candidate["mergeStateStatus"] = "BEHIND"
    assert updater.eligible_pull_requests([candidate], base="main") == [
        candidate
    ]

    # Updating makes it current; a later main change makes it eligible again.
    candidate["mergeStateStatus"] = "CLEAN"
    assert updater.eligible_pull_requests([candidate], base="main") == []
    candidate["mergeStateStatus"] = "BEHIND"
    assert updater.eligible_pull_requests([candidate], base="main") == [
        candidate
    ]


def test_selection_queries_oldest_approved_prs_and_writes_outputs(
    monkeypatch, tmp_path
):
    calls = []
    oldest = pull_request(12, "2026-01-01T00:00:00Z")
    newer = pull_request(13, "2026-02-01T00:00:00Z")
    output = tmp_path / "output"
    summary = tmp_path / "summary"

    def fake_gh(args):
        calls.append(args)
        if args[:2] == ["pr", "list"]:
            return [newer, oldest]
        number = int(args[2])
        return {oldest["number"]: oldest, newer["number"]: newer}[number]

    monkeypatch.setattr(updater, "run_gh_json", fake_gh)

    selected = updater.select_oldest(
        "example/project",
        "main",
        github_output=output,
        step_summary=summary,
    )

    assert selected == oldest
    assert output.read_text() == f"number=12\nhead_sha={HEAD_A}\n"
    summary_text = summary.read_text()
    assert summary_text.index("#12") < summary_text.index("#13")
    assert "Selected oldest candidate: #12." in summary_text
    assert calls[0][:2] == ["pr", "list"]
    assert "review:approved sort:created-asc" in calls[0]
    assert calls[0][calls[0].index("--limit") + 1] == "1000"
    fields = calls[0][calls[0].index("--json") + 1]
    assert "id" in fields
    assert "maintainerCanModify" in fields
    assert "statusCheckRollup" not in fields
    assert [call[:2] for call in calls[1:]] == [
        ["pr", "view"],
        ["pr", "view"],
    ]
    detail_fields = calls[1][calls[1].index("--json") + 1]
    assert "statusCheckRollup" in detail_fields


def test_selection_skips_failed_oldest_candidate(monkeypatch, tmp_path):
    failed = pull_request(
        12,
        "2026-01-01T00:00:00Z",
        statusCheckRollup=[{"name": "optional-lint", "conclusion": "FAILURE"}],
    )
    healthy = pull_request(13, "2026-02-01T00:00:00Z")
    output = tmp_path / "output"
    summary = tmp_path / "summary"

    def fake_gh(args):
        if args[:2] == ["pr", "list"]:
            return [failed, healthy]
        number = int(args[2])
        return {failed["number"]: failed, healthy["number"]: healthy}[number]

    monkeypatch.setattr(updater, "run_gh_json", fake_gh)

    selected = updater.select_oldest(
        "example/project",
        "main",
        github_output=output,
        step_summary=summary,
    )

    assert selected == healthy
    assert output.read_text() == f"number=13\nhead_sha={HEAD_A}\n"
    assert "#12" in summary.read_text()
    assert "failed checks: optional-lint" in summary.read_text()


def test_all_failed_candidates_are_a_successful_no_op(monkeypatch, tmp_path):
    failed = pull_request(
        statusCheckRollup=[{"name": "tests", "conclusion": "FAILURE"}]
    )
    output = tmp_path / "output"
    summary = tmp_path / "summary"

    def fake_gh(args):
        return [failed] if args[:2] == ["pr", "list"] else failed

    monkeypatch.setattr(updater, "run_gh_json", fake_gh)

    selected = updater.select_oldest(
        "example/project",
        "main",
        github_output=output,
        step_summary=summary,
    )

    assert selected is None
    assert output.read_text() == "number=\nhead_sha=\n"
    assert "failed checks: tests" in summary.read_text()


def test_empty_selection_is_a_successful_no_op(monkeypatch, tmp_path):
    output = tmp_path / "output"
    summary = tmp_path / "summary"
    monkeypatch.setattr(updater, "run_gh_json", lambda _args: [])

    selected = updater.select_oldest(
        "example/project",
        "main",
        github_output=output,
        step_summary=summary,
    )

    assert selected is None
    assert output.read_text() == "number=\nhead_sha=\n"
    assert "No approved, auto-merge-enabled PR" in summary.read_text()


def test_update_revalidates_and_uses_expected_head_sha(monkeypatch, tmp_path):
    calls = []
    summary = tmp_path / "summary"
    # ``gh api -F`` only coerces a value to a JSON number when it parses
    # within int64 (max ~1.8e19); an all-``1`` 40-char SHA overflows that
    # range and would already be sent as a string. Use a SHA whose digits
    # parse within int64 so this test exercises the coercion ``-f`` avoids.
    int64_coercible_head_sha = "0" * 37 + "123"

    def fake_gh(args):
        calls.append(args)
        if args[:2] == ["pr", "view"]:
            return pull_request(55, headRefOid=int64_coercible_head_sha)
        return {
            "data": {"updatePullRequestBranch": {"pullRequest": {"number": 55}}}
        }

    monkeypatch.setattr(updater, "run_gh_json", fake_gh)

    updated = updater.update_pull_request(
        "example/project",
        "main",
        number=55,
        expected_head_sha=int64_coercible_head_sha,
        step_summary=summary,
    )

    assert updated is True
    assert len(calls) == 2
    assert calls[0][:3] == ["pr", "view", "55"]
    update_call = calls[1]
    assert update_call[:2] == ["api", "graphql"]
    query_argument = update_call[update_call.index("-f") + 1]
    assert "updatePullRequestBranch" in query_argument
    assert "mergePullRequest" not in query_argument
    pull_request_id_index = update_call.index("pullRequestId=PR_example_55")
    expected_head_index = update_call.index(
        f"expectedHeadOid={int64_coercible_head_sha}"
    )
    assert update_call[pull_request_id_index - 1] == "-f"
    assert update_call[expected_head_index - 1] == "-f"
    assert "Branch update accepted." in summary.read_text()


@pytest.mark.parametrize(
    "api_error",
    [
        "GraphQL: user doesn't have permission to update head repository",
        "GraphQL: refusing to update a workflow without `workflow` scope",
        "GraphQL: refusing to update a workflow without `workflows` permission",
    ],
)
def test_fork_authorization_error_explains_classic_pat_requirement(
    monkeypatch, tmp_path, api_error
):
    calls = []

    def fake_gh(args):
        calls.append(args)
        if args[:2] == ["pr", "view"]:
            return pull_request(
                55,
                isCrossRepository=True,
                maintainerCanModify=True,
            )
        raise updater.UpdaterError(api_error)

    monkeypatch.setattr(updater, "run_gh_json", fake_gh)

    with pytest.raises(
        updater.UpdaterError,
        match=r"classic PAT with public_repo and workflow scopes",
    ):
        updater.update_pull_request(
            "example/project",
            "main",
            number=55,
            expected_head_sha=HEAD_A,
            step_summary=tmp_path / "summary",
        )

    assert len(calls) == 2


def test_fork_authorization_hint_uses_captured_gh_stderr(monkeypatch, tmp_path):
    candidate = pull_request(
        55,
        isCrossRepository=True,
        maintainerCanModify=True,
    )
    monkeypatch.setattr(updater, "fetch_pull_request", lambda *_args: candidate)

    def failed_gh_run(*_args, **_kwargs):
        # The real ``subprocess.run(command, ...)`` call passes the full
        # ``gh`` argv positionally as the first argument; assert on it so
        # this test also confirms the mutation is sent via ``-f`` (not
        # ``-F``), the actual fix under test.
        assert _args[0][:4] == ["gh", "api", "graphql", "-f"]
        return updater.subprocess.CompletedProcess(
            args=["gh", "api", "graphql"],
            returncode=1,
            stdout="",
            stderr=(
                "GraphQL: user doesn't have permission to update head "
                "repository\n"
            ),
        )

    monkeypatch.setattr(updater.subprocess, "run", failed_gh_run)

    with pytest.raises(
        updater.UpdaterError,
        match=r"classic PAT with public_repo and workflow scopes",
    ):
        updater.update_pull_request(
            "example/project",
            "main",
            number=55,
            expected_head_sha=HEAD_A,
            step_summary=tmp_path / "summary",
        )


@pytest.mark.parametrize(
    "change",
    [
        {"state": "CLOSED"},
        {"headRefOid": HEAD_B},
        {"reviewDecision": "CHANGES_REQUESTED"},
        {"autoMergeRequest": None},
        {"mergeStateStatus": "CLEAN"},
        {
            "statusCheckRollup": [
                {"name": "late-failure", "conclusion": "FAILURE"}
            ]
        },
    ],
    ids=[
        "merged-or-closed",
        "head-changed",
        "approval-revoked",
        "auto-merge-disabled",
        "already-updated",
        "check-failed",
    ],
)
def test_changed_pr_is_not_updated(monkeypatch, tmp_path, change):
    calls = []
    current = pull_request(55, **change)

    def fake_gh(args):
        calls.append(args)
        return current

    monkeypatch.setattr(updater, "run_gh_json", fake_gh)

    updated = updater.update_pull_request(
        "example/project",
        "main",
        number=55,
        expected_head_sha=HEAD_A,
        step_summary=tmp_path / "summary",
    )

    assert updated is False
    assert len(calls) == 1
    assert calls[0][:2] == ["pr", "view"]


def test_cli_fails_before_api_access_when_token_is_missing(monkeypatch, capsys):
    monkeypatch.delenv("GH_TOKEN", raising=False)
    api_called = False

    def fake_gh(_args):
        nonlocal api_called
        api_called = True
        return []

    monkeypatch.setattr(updater, "run_gh_json", fake_gh)

    exit_code = updater.main(
        [
            "update",
            "--repo",
            "example/project",
            "--pr-number",
            "55",
            "--expected-head-sha",
            HEAD_A,
        ]
    )

    assert exit_code == 1
    assert api_called is False
    assert "provide AUTO_MERGE_PAT_TOKEN" in capsys.readouterr().err


@pytest.fixture(scope="module")
def workflow():
    return yaml.safe_load(WORKFLOW.read_text())


def test_workflow_runs_twice_hourly_and_serializes_updates(workflow):
    # PyYAML 1.1 parses the unquoted key ``on`` as boolean True.
    triggers = workflow.get("on", workflow.get(True))

    assert triggers == {
        "schedule": [{"cron": "17,47 * * * *"}],
        "workflow_dispatch": None,
    }
    assert workflow["concurrency"] == {
        "group": "update-oldest-auto-merge-pr",
        "cancel-in-progress": False,
    }


def test_workflow_is_a_thin_pinned_wrapper(workflow):
    assert workflow["permissions"] == {}
    job = workflow["jobs"]["update-branch"]
    assert job["permissions"] == {
        "contents": "read",
        "pull-requests": "read",
    }

    checkout = next(
        step
        for step in job["steps"]
        if str(step.get("uses", "")).startswith("actions/checkout@")
    )
    checkout_ref = checkout["uses"].split("@", 1)[1]
    assert len(checkout_ref) == 40
    assert all(character in "0123456789abcdef" for character in checkout_ref)
    assert checkout["with"] == {"persist-credentials": False, "ref": "main"}

    run_scripts = "\n".join(step.get("run", "") for step in job["steps"])
    assert run_scripts.count("scripts/ci/update_oldest_auto_merge_pr.py") == 2
    assert "gh pr" not in run_scripts
    assert "gh api" not in run_scripts

    select_step = next(
        step for step in job["steps"] if step.get("id") == "select"
    )
    update_step = next(
        step
        for step in job["steps"]
        if step.get("name") == "Update the selected PR branch"
    )
    assert " select " in select_step["run"]
    assert update_step["if"] == "${{ steps.select.outputs.number != '' }}"
    assert update_step["env"]["PR_NUMBER"] == (
        "${{ steps.select.outputs.number }}"
    )
    assert update_step["env"]["EXPECTED_HEAD_SHA"] == (
        "${{ steps.select.outputs.head_sha }}"
    )
    assert " update " in update_step["run"]
    assert '--pr-number "${PR_NUMBER}"' in update_step["run"]
    assert '--expected-head-sha "${EXPECTED_HEAD_SHA}"' in update_step["run"]


def test_workflow_keeps_pat_in_update_step_only(workflow):
    steps = workflow["jobs"]["update-branch"]["steps"]
    select_step = next(step for step in steps if step.get("id") == "select")
    update_step = next(
        step
        for step in steps
        if step.get("name") == "Update the selected PR branch"
    )

    assert select_step["env"] == {"GH_TOKEN": "${{ github.token }}"}
    assert (
        update_step["env"]["GH_TOKEN"] == "${{ secrets.AUTO_MERGE_PAT_TOKEN }}"
    )
    for step in steps:
        if step is not update_step:
            assert "PAT_TOKEN" not in str(step)


def test_production_script_only_contains_branch_update_mutation():
    script = SCRIPT.read_text()

    assert "updatePullRequestBranch" in script
    assert "expectedHeadOid" in script
    assert "mergePullRequest" not in script
    assert "/merge" not in script