Skip to content

Commit

Permalink
remove prompt for deleting branches on pr merge in interactive mode w…
Browse files Browse the repository at this point in the history
…hen -d flag is passed
  • Loading branch information
dpromanko committed Jan 15, 2021
1 parent 2c35eb0 commit 8289a55
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
11 changes: 7 additions & 4 deletions pkg/cmd/pr/merge/merge.go
Expand Up @@ -140,7 +140,7 @@ func mergeRun(opts *MergeOptions) error {
mergeMethod := opts.MergeMethod

if opts.InteractiveMode {
mergeMethod, deleteBranch, err = prInteractiveMerge(opts.DeleteLocalBranch, crossRepoPR)
mergeMethod, deleteBranch, err = prInteractiveMerge(opts.DeleteBranch, opts.DeleteLocalBranch, crossRepoPR)
if err != nil {
if errors.Is(err, cancelError) {
fmt.Fprintln(opts.IO.ErrOut, "Cancelled.")
Expand Down Expand Up @@ -238,7 +238,7 @@ func mergeRun(opts *MergeOptions) error {

var cancelError = errors.New("cancelError")

func prInteractiveMerge(deleteLocalBranch bool, crossRepoPR bool) (api.PullRequestMergeMethod, bool, error) {
func prInteractiveMerge(deleteBranch bool, deleteLocalBranch bool, crossRepoPR bool) (api.PullRequestMergeMethod, bool, error) {
mergeMethodQuestion := &survey.Question{
Name: "mergeMethod",
Prompt: &survey.Select{
Expand All @@ -250,7 +250,7 @@ func prInteractiveMerge(deleteLocalBranch bool, crossRepoPR bool) (api.PullReque

qs := []*survey.Question{mergeMethodQuestion}

if !crossRepoPR {
if !crossRepoPR && !deleteBranch {
var message string
if deleteLocalBranch {
message = "Delete the branch locally and on GitHub?"
Expand Down Expand Up @@ -300,6 +300,9 @@ func prInteractiveMerge(deleteLocalBranch bool, crossRepoPR bool) (api.PullReque
mergeMethod = api.PullRequestMergeMethodSquash
}

deleteBranch := answers.DeleteBranch
if !deleteBranch {
deleteBranch = answers.DeleteBranch
}

return mergeMethod, deleteBranch, nil
}
54 changes: 54 additions & 0 deletions pkg/cmd/pr/merge/merge_test.go
Expand Up @@ -606,6 +606,60 @@ func TestPRMerge_interactive(t *testing.T) {
test.ExpectLines(t, output.Stderr(), "Merged pull request #3")
}

func TestPRMerge_interactiveWithDeleteBranch(t *testing.T) {
http := initFakeHTTP()
defer http.Verify(t)
http.Register(
httpmock.GraphQL(`query PullRequestForBranch\b`),
httpmock.StringResponse(`
{ "data": { "repository": { "pullRequests": { "nodes": [{
"headRefName": "blueberries",
"headRepositoryOwner": {"login": "OWNER"},
"id": "THE-ID",
"number": 3
}] } } } }`))
http.Register(
httpmock.GraphQL(`mutation PullRequestMerge\b`),
httpmock.GraphQLMutation(`{}`, func(input map[string]interface{}) {
assert.Equal(t, "THE-ID", 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.StringResponse(`{}`))

cs, cmdTeardown := test.InitCmdStubber()
defer cmdTeardown()

cs.Stub("") // git config --get-regexp ^branch\.blueberries\.(remote|merge)$
cs.Stub("") // git symbolic-ref --quiet --short HEAD
cs.Stub("") // git checkout master
cs.Stub("") // git push origin --delete blueberries
cs.Stub("") // git branch -d

as, surveyTeardown := prompt.InitAskStubber()
defer surveyTeardown()

as.Stub([]*prompt.QuestionStub{
{
Name: "mergeMethod",
Value: 0,
},
{
Name: "isConfirmed",
Value: true,
},
})

output, err := runCommand(http, "blueberries", true, "-d")
if err != nil {
t.Fatalf("Got unexpected error running `pr merge` %s", err)
}

test.ExpectLines(t, output.Stderr(), "Merged pull request #3")
}

func TestPRMerge_interactiveCancelled(t *testing.T) {
http := initFakeHTTP()
defer http.Verify(t)
Expand Down

0 comments on commit 8289a55

Please sign in to comment.