Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 8.9k
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -460,10 +460,22 @@ func (m *mergeContext) deleteRemoteBranch() error { | ||
| if !m.merged { | ||
| apiClient := api.NewClientFromHTTP(m.httpClient) | ||
| err := api.BranchDeleteRemote(apiClient, m.baseRepo, m.pr.HeadRefName) | ||
| var httpErr api.HTTPError | ||
| // The ref might have already been deleted by GitHub | ||
| if err != nil && (!errors.As(err, &httpErr) || httpErr.StatusCode != 422) { | ||
| return fmt.Errorf("failed to delete remote branch %s: %w", m.cs.Cyan(m.pr.HeadRefName), err) | ||
| if err != nil { | ||
| // Normally, the API returns 422, with the message "Reference does not exist" | ||
| // when the branch has already been deleted. It also returns 404 with the same | ||
| // message, but that rarely happens. In both cases, we should not return an | ||
| // error because the goal is already achieved. | ||
| var isAlreadyDeletedError bool | ||
| if httpErr := (api.HTTPError{}); errors.As(err, &httpErr) { | ||
babakks marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| // TODO: since the API returns 422 for a couple of other reasons, for more accuracy | ||
| // we might want to check the error message against "Reference does not exist". | ||
| isAlreadyDeletedError = httpErr.StatusCode == http.StatusUnprocessableEntity || httpErr.StatusCode == http.StatusNotFound | ||
| } | ||
| if !isAlreadyDeletedError { | ||
| return fmt.Errorf("failed to delete remote branch %s: %w", m.cs.Cyan(m.pr.HeadRefName), err) | ||
babakks marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -24,6 +24,7 @@ import ( | ||
| "github.com/cli/cli/v2/pkg/httpmock" | ||
| "github.com/cli/cli/v2/pkg/iostreams" | ||
| "github.com/cli/cli/v2/test" | ||
| ghapi "github.com/cli/go-gh/v2/pkg/api" | ||
| "github.com/google/shlex" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| @@ -659,6 +660,103 @@ func TestPrMerge_deleteBranch(t *testing.T) { | ||
| `), output.Stderr()) | ||
| } | ||
| func TestPrMerge_deleteBranch_apiError(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| apiError ghapi.HTTPError | ||
| wantErr string | ||
| wantStderr string | ||
| }{ | ||
| { | ||
| name: "branch already deleted (422: Reference does not exist)", | ||
| apiError: ghapi.HTTPError{ | ||
| Message: "Reference does not exist", | ||
| StatusCode: http.StatusUnprocessableEntity, // 422 | ||
| }, | ||
| wantStderr: heredoc.Doc(` | ||
| ✓ Merged pull request OWNER/REPO#10 (Blueberries are a good fruit) | ||
| ✓ Deleted local branch blueberries and switched to branch main | ||
| ✓ Deleted remote branch blueberries | ||
| `), | ||
| }, | ||
| { | ||
| name: "branch already deleted (404: Reference does not exist) (#11187)", | ||
| apiError: ghapi.HTTPError{ | ||
| Message: "Reference does not exist", | ||
| StatusCode: http.StatusNotFound, // 404 | ||
| }, | ||
| wantStderr: heredoc.Doc(` | ||
| ✓ Merged pull request OWNER/REPO#10 (Blueberries are a good fruit) | ||
| ✓ Deleted local branch blueberries and switched to branch main | ||
| ✓ Deleted remote branch blueberries | ||
| `), | ||
| }, | ||
| { | ||
| name: "unknown API error", | ||
| apiError: ghapi.HTTPError{ | ||
| Message: "blah blah", | ||
| StatusCode: http.StatusInternalServerError, // 500 | ||
| }, | ||
| wantStderr: heredoc.Doc(` | ||
| ✓ Merged pull request OWNER/REPO#10 (Blueberries are a good fruit) | ||
| ✓ Deleted local branch blueberries and switched to branch main | ||
| `), | ||
babakks marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| wantErr: "failed to delete remote branch blueberries: HTTP 500: blah blah (https://api.github.com/repos/OWNER/REPO/git/refs/heads/blueberries)", | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| http := initFakeHTTP() | ||
| defer http.Verify(t) | ||
| shared.StubFinderForRunCommandStyleTests(t, | ||
| "", | ||
| &api.PullRequest{ | ||
| ID: "PR_10", | ||
| Number: 10, | ||
| State: "OPEN", | ||
| Title: "Blueberries are a good fruit", | ||
| HeadRefName: "blueberries", | ||
| BaseRefName: "main", | ||
| MergeStateStatus: "CLEAN", | ||
| }, | ||
| baseRepo("OWNER", "REPO", "main"), | ||
| ) | ||
| http.Register( | ||
| httpmock.GraphQL(`mutation PullRequestMerge\b`), | ||
| httpmock.GraphQLMutation(`{}`, func(input map[string]interface{}) { | ||
| assert.Equal(t, "PR_10", input["pullRequestId"].(string)) | ||
| assert.Equal(t, "MERGE", input["mergeMethod"].(string)) | ||
| assert.NotContains(t, input, "commitHeadline") | ||
| })) | ||
| http.Register( | ||
| httpmock.REST("DELETE", "repos/OWNER/REPO/git/refs/heads/blueberries"), | ||
| httpmock.JSONErrorResponse(tt.apiError.StatusCode, tt.apiError)) | ||
| cs, cmdTeardown := run.Stub() | ||
| defer cmdTeardown(t) | ||
| cs.Register(`git rev-parse --verify refs/heads/main`, 0, "") | ||
| cs.Register(`git checkout main`, 0, "") | ||
| cs.Register(`git rev-parse --verify refs/heads/blueberries`, 0, "") | ||
| cs.Register(`git branch -D blueberries`, 0, "") | ||
| cs.Register(`git pull --ff-only`, 0, "") | ||
| output, err := runCommand(http, nil, "blueberries", true, `pr merge --merge --delete-branch`) | ||
| assert.Equal(t, "", output.String()) | ||
| assert.Equal(t, tt.wantStderr, output.Stderr()) | ||
| if tt.wantErr != "" { | ||
| assert.EqualError(t, err, tt.wantErr) | ||
| return | ||
| } | ||
| assert.NoError(t, err) | ||
| }) | ||
| } | ||
| } | ||
| func TestPrMerge_deleteBranch_mergeQueue(t *testing.T) { | ||
| http := initFakeHTTP() | ||
| defer http.Verify(t) | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
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 onmergeContext, and their behaviour against the state fields make it very hard to understand/improve the code.