Repository navigation
Recover native merge after a lost GitHub reply #1436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1423,7 +1423,7 @@ def mark_ready(repo: str, number: int) -> None: | |
|
|
||
| _QUEUE_QUERY = ( | ||
| "query($owner:String!,$name:String!,$number:Int!){repository(owner:$owner,name:$name)" | ||
| "{pullRequest(number:$number){isInMergeQueue autoMergeRequest{enabledAt}}}}" | ||
| "{pullRequest(number:$number){headRefOid isInMergeQueue autoMergeRequest{enabledAt}}}}" | ||
| ) | ||
|
|
||
|
|
||
|
|
@@ -1454,7 +1454,7 @@ def merged_sha( | |
| raise error(argv, "PR closed without a verified merge or invalid merge evidence") | ||
|
|
||
|
|
||
| def merge_pending(repo: str, number: int) -> bool: | ||
| def merge_pending(repo: str, number: int, head_sha: str) -> bool: | ||
| """True when GitHub holds the PR for merging: queued, or accepted as an auto-merge | ||
| request that it moves into the queue shortly after (observed ~1 minute, #180).""" | ||
| args = _pull_query(repo, number, _QUEUE_QUERY) | ||
|
|
@@ -1469,6 +1469,8 @@ def merge_pending(repo: str, number: int) -> bool: | |
| raise MergeFailed(("gh", "api", "graphql"), | ||
| f"GitHub queue lookup for PR #{number}: queue status could not be verified. " | ||
| f"Check the PR's queue or merge state before retrying.\n{str(exc)[-300:]}") from exc | ||
| if pr.get("headRefOid") != head_sha: | ||
| raise MergeFailed(("gh", *args), "PR head changed or unavailable during queue verification") | ||
| return queued or bool(auto) | ||
|
|
||
|
|
||
|
|
@@ -1485,16 +1487,19 @@ def merge(repo: str, number: int, strategy: str, head_sha: str) -> str | None: | |
| argv = ( | ||
| "pr", "merge", str(number), "--repo", repo, *method, "--match-head-commit", head_sha, | ||
| ) | ||
| if strategy == "queue" and merge_pending(repo, number, head_sha): | ||
| return None | ||
| write_error = None | ||
| try: | ||
| _gh(*argv, timeout=300) | ||
| except GhError as exc: | ||
| raise MergeFailed(exc.command, exc.tail) from exc | ||
| except (GhError, ProcTimeout, OSError) as exc: | ||
| write_error = exc | ||
| sha = merged_sha(repo, number, expected_head=head_sha) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: The write error is lost when the follow-up read raises
|
||
| if sha: | ||
| return sha | ||
| queue_error = None | ||
| try: | ||
| if merge_pending(repo, number): | ||
| if merge_pending(repo, number, head_sha): | ||
| return None | ||
| except MergeFailed as exc: | ||
| queue_error = exc | ||
|
|
@@ -1504,7 +1509,8 @@ def merge(repo: str, number: int, strategy: str, head_sha: str) -> str | None: | |
| return sha | ||
| if queue_error: | ||
| raise queue_error | ||
| raise MergeFailed(("gh", *argv), f"PR #{number} neither merged nor queued") | ||
| detail = f": {write_error}" if write_error else "" | ||
| raise MergeFailed(("gh", *argv), f"PR #{number} neither merged nor queued{detail}") | ||
|
|
||
|
|
||
| def _remote_branch_head(endpoint: str) -> str | None: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1114,6 +1114,8 @@ def test_merge_success_verifies_rest_landing(gh: FakeGh) -> None: | |
|
|
||
| def test_merge_command_failure(gh: FakeGh) -> None: | ||
| gh.fail("pr merge", "head commit mismatch") | ||
| gh.respond(f"api repos/{REPO}/pulls/7", merge_json()) | ||
| gh.respond("api graphql", _queue_json(False)) | ||
| with pytest.raises(github.MergeFailed) as info: | ||
| github.merge(REPO, 7, "rebase", HEAD) | ||
| assert "--rebase" in info.value.command and "mismatch" in info.value.tail | ||
|
|
@@ -1147,7 +1149,7 @@ def test_delete_remote_branch_scopes_operator_tokens(gh: FakeGh, fake_tool, monk | |
|
|
||
|
|
||
| def _queue_json(queued: bool) -> dict: | ||
| return {"data": {"repository": {"pullRequest": {"isInMergeQueue": queued}}}} | ||
| return {"data": {"repository": {"pullRequest": {"isInMergeQueue": queued, "headRefOid": HEAD}}}} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: No test for a missing or changed The new head check in |
||
|
|
||
|
|
||
| def arm_merge(gh: FakeGh, queue: dict, *details: dict) -> None: | ||
|
|
@@ -1178,10 +1180,15 @@ def test_merge_reports_queue_admission_or_landing(gh, strategy, queue_state): | |
| if landed: | ||
| snapshots.append(merge_json(state="MERGED", sha="d" * 40)) | ||
| arm_merge(gh, accepted, *snapshots) | ||
| if strategy == "queue": | ||
| gh.respond("api graphql", _queue_json(False), accepted) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: The queue branch silently replaces the GraphQL responses that arm_merge sets
|
||
| assert github.merge(REPO, 7, strategy, HEAD) == ("d" * 40 if landed else None) | ||
| if not landed: | ||
| assert ("--squash" in gh.argv(0)) == (strategy == "squash") | ||
| assert "--queue" not in gh.argv(0) | ||
| if strategy == "queue": | ||
| assert github.merge(REPO, 7, strategy, HEAD) is None | ||
| writes = [c["argv"] for c in gh.calls if c["argv"][:2] == ["pr", "merge"]] | ||
| assert len(writes) == 1 | ||
| assert ("--squash" in writes[0]) == (strategy == "squash") | ||
| assert "--queue" not in writes[0] | ||
|
|
||
|
|
||
| def test_merged_sha_only_for_merged_state(gh: FakeGh) -> None: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: The admission check before writing only runs for
strategy == "queue"merge_pendingalso returns True for an accepted auto-merge request. The squash, merge and rebase strategies reach that state on a queue-protected base, andtest_merge_queued_returns_nonecovers that case. A retry after a lost reply under those strategies still sends a secondgh pr merge. That second write is probably harmless, because gh rejects it or treats it as a no-op and the queue check afterwards returns None. Still, it falls short of the stated goal that a retry observes admission before writing. Consider either running the check for every strategy, or documenting that onlyqueueis protected this way.