8000 Handle `HTTP 404` when deleting remote branch in `pr merge` by babakks · Pull Request #11234 · cli/cli · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Handle HTTP 404 when deleting remote branch in pr merge #11234

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

Merged
merged 3 commits into from
Jul 8, 2025

Conversation

babakks
Copy link
Member
@babakks babakks commented Jul 5, 2025

Fixes #11187

This PR improves the error handling when deleting a remote branch. Prior to this, only an HTTP 422 response was considered as an indication of an already deleted branch. However, as explained in the issue, the API can also return HTTP 404, which is now handled in this PR.

Since we don't have a simple way to reproduce this, there's no A/C check result. However, necessary test cases are added and verify the expected behaviour.

@Copilot Copilot AI review requested due to automatic review settings July 5, 2025 13:48
@babakks babakks requested a review from a team as a code owner July 5, 2025 13:48
@babakks babakks requested a review from BagToad July 5, 2025 13:48
@babakks babakks linked an issue Jul 5, 2025 that may be closed by this pull request
@babakks babakks temporarily deployed to cli-automation July 5, 2025 13:48 — with GitHub Actions Inactive
Copilot

This comment was marked as outdated.

@@ -460,10 +460,22 @@ func (m *mergeContext) deleteRemoteBranch() error {
if !m.merged {
Copy link
Member Author
@babakks babakks Jul 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to improve the code around this if-statement, because it's confusing, but at the end gave up due to tests breaking. The coupling among the methods on mergeContext, and their behaviour against the state fields make it very hard to understand/improve the code.

babakks added 2 commits July 5, 2025 14:54
Signed-off-by: Babak K. Shandiz <babakks@github.com>
…s error

Signed-off-by: Babak K. Shandiz <babakks@github.com>
@babakks babakks force-pushed the babakks/pr-merge-handle-404 branch from 2abe161 to b1e1c8d Compare July 5, 2025 13:54
@babakks babakks requested a review from Copilot July 5, 2025 13:55
Copy link
Contributor
@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR updates the remote-branch deletion logic to treat GitHub’s 404 “Reference does not exist” as non-fatal (in addition to 422) and adds tests for these cases.

  • Treat HTTP 404 errors from the delete-ref endpoint as “already deleted”
  • Retain existing behavior for HTTP 422 and surface other HTTP errors
  • Add TestPrMerge_deleteBranch_apiError covering 422, 404, and 500 scenarios

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
pkg/cmd/pr/merge/merge.go Extend deleteRemoteBranch to ignore both 422 and 404 responses
pkg/cmd/pr/merge/merge_test.go Add tests for handling 422, 404, and unexpected API errors
Comments suppressed due to low confidence (1)

pkg/cmd/pr/merge/merge_test.go:700

  • In the unknown-API-error test you define wantStderr but never assert on output.Stderr() when wantErr is set. Add an assertion to verify that no "Deleted remote branch" message is printed on failure.
			wantStderr: heredoc.Doc(`

Signed-off-by: Babak K. Shandiz <babakks@github.com>
Copy link
Member
@BagToad BagToad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. We reviewed this in sync and discussed the problem and confidence around tests. We are both confident with these changes because the tests back up the behavior around which API call (the delete ref API) returns a 404 and what the expected error is in that case. In other words, there are no changes to behavior here if other API calls were to return a 404.

@BagToad BagToad merged commit c93136b into trunk Jul 8, 2025
17 checks passed
@BagToad BagToad deleted the babakks/pr-merge-handle-404 branch July 8, 2025 13:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

--delete-branch fails, if branch is deleted already (race condition)
2 participants
0