diff --git a/tests/test_the_test/test_github_nightly.py b/tests/test_the_test/test_github_nightly.py index 505ead2268d..0c1c723e15e 100644 --- a/tests/test_the_test/test_github_nightly.py +++ b/tests/test_the_test/test_github_nightly.py @@ -344,6 +344,38 @@ def test_human_activity_guard_skips_branch_updates(self, tmp_path: Path) -> None assert "comments(first: 1)" in " ".join(thread_query_command.args) assert '.comments.nodes[0].author.__typename == "Bot"' in thread_query_command.args[-1] + @pytest.mark.parametrize(("pr_state", "skipped"), [("MERGED", True), ("CLOSED", True), ("OPEN", False)]) + def test_push_failure_on_existing_pr(self, tmp_path: Path, pr_state: str, *, skipped: bool) -> None: + """The PR may be merged by auto-merge between its lookup and the push of its update""" + runner = FakeRunner( + [ + *EXISTING_PR_UPDATE_RESULTS[:-1], + result(returncode=1, stderr="update ref: PATCH ...: 422 Reference cannot be updated []"), + result(f"{pr_state}\n"), # gh pr view --json state + ] + ) + + if skipped: + process_activation_branch("easy-win/asm-libraries/python", "python", options(tmp_path), runner) + else: + with pytest.raises(RuntimeError, match="422 Reference cannot be updated"): + process_activation_branch("easy-win/asm-libraries/python", "python", options(tmp_path), runner) + + assert runner.commands[-1].args == ["gh", "pr", "view", "123", "--json", "state", "--jq", ".state"] + + def test_push_failure_on_new_pr_is_raised(self, tmp_path: Path) -> None: + runner = FakeRunner( + [ + *NEW_PR_RESULTS[:5], + result(returncode=1, stderr="update ref: PATCH ...: 422 Reference cannot be updated []"), + ] + ) + + with pytest.raises(RuntimeError, match="422 Reference cannot be updated"): + process_activation_branch("easy-win/asm-libraries/python", "python", options(tmp_path), runner) + + assert not any(command[:3] == ["gh", "pr", "view"] for command in commands(runner)) + def test_auto_merge_runs_for_non_opted_out_pair(self, tmp_path: Path) -> None: runner = FakeRunner( [ diff --git a/utils/ci/github/nightly.py b/utils/ci/github/nightly.py index f4bf9334d02..aec7aec07dc 100644 --- a/utils/ci/github/nightly.py +++ b/utils/ci/github/nightly.py @@ -242,7 +242,15 @@ def process_activation_branch( return print(f"PR #{pr_number} exists but has no human activity, updating") # noqa: T201 - _push_signed_branch(branch_name, options, runner) + try: + _push_signed_branch(branch_name, options, runner) + except RuntimeError: + # The PR may have been merged (auto-merge) or closed between the lookup and the push: + # GitHub then refuses to update its branch. The next nightly recreates it from an up-to-date main. + if pr_number is None or _get_pr_state(pr_number, runner) == "OPEN": + raise + print(f"PR #{pr_number} for {branch_name} was merged or closed during the update, skipping") # noqa: T201 + return if pr_number is None: _create_pr(branch_name, owner, library, options.github, runner) @@ -297,6 +305,11 @@ def _find_pr_number(branch_name: str, runner: CommandRunner) -> str | None: return value +def _get_pr_state(pr_number: str, runner: CommandRunner) -> str: + """Returns OPEN, CLOSED or MERGED""" + return _run_checked(["gh", "pr", "view", pr_number, "--json", "state", "--jq", ".state"], runner).stdout.strip() + + def _get_pr_activity(repository: str, pr_number: str, runner: CommandRunner) -> PullRequestActivity: comments = _api_count( [