X Tutup
Skip to content

Commit a90997e

Browse files
committed
pr merge: avoid prompting to enter editor after editing phase is chosen
When user chooses "Edit commit message", open the editor immediately instead of showing an additional prompt to open the editor.
1 parent 70d4786 commit a90997e

File tree

2 files changed

+57
-41
lines changed

2 files changed

+57
-41
lines changed

pkg/cmd/pr/merge/merge.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ import (
2020
"github.com/spf13/cobra"
2121
)
2222

23+
type editor interface {
24+
Edit(string, string) (string, error)
25+
}
26+
2327
type MergeOptions struct {
2428
HttpClient func() (*http.Client, error)
2529
Config func() (config.Config, error)
@@ -37,6 +41,7 @@ type MergeOptions struct {
3741

3842
Body string
3943
BodySet bool
44+
Editor editor
4045

4146
IsDeleteBranchIndicated bool
4247
CanDeleteLocalBranch bool
@@ -103,6 +108,11 @@ func NewCmdMerge(f *cmdutil.Factory, runF func(*MergeOptions) error) *cobra.Comm
103108
opts.CanDeleteLocalBranch = !cmd.Flags().Changed("repo")
104109
opts.BodySet = cmd.Flags().Changed("body")
105110

111+
opts.Editor = &userEditor{
112+
io: opts.IO,
113+
config: opts.Config,
114+
}
115+
106116
if runF != nil {
107117
return runF(opts)
108118
}
@@ -198,20 +208,14 @@ func mergeRun(opts *MergeOptions) error {
198208
}
199209

200210
if action == shared.EditCommitMessageAction {
201-
var editorCommand string
202-
editorCommand, err = cmdutil.DetermineEditor(opts.Config)
203-
if err != nil {
204-
return err
205-
}
206-
207211
if !payload.setCommitBody {
208212
payload.commitBody, err = getMergeText(httpClient, baseRepo, pr.ID, payload.method)
209213
if err != nil {
210214
return err
211215
}
212216
}
213217

214-
payload.commitBody, err = commitMsgSurvey(payload.commitBody, editorCommand)
218+
payload.commitBody, err = opts.Editor.Edit("*.md", payload.commitBody)
215219
if err != nil {
216220
return err
217221
}
@@ -410,17 +414,16 @@ func confirmSurvey(allowEditMsg bool) (shared.Action, error) {
410414
}
411415
}
412416

413-
func commitMsgSurvey(msg string, editorCommand string) (string, error) {
414-
var result string
415-
q := &surveyext.GhEditor{
416-
EditorCommand: editorCommand,
417-
Editor: &survey.Editor{
418-
Message: "Body",
419-
AppendDefault: true,
420-
Default: msg,
421-
FileName: "*.md",
422-
},
417+
type userEditor struct {
418+
io *iostreams.IOStreams
419+
config func() (config.Config, error)
420+
}
421+
422+
func (e *userEditor) Edit(filename, startingText string) (string, error) {
423+
editorCommand, err := cmdutil.DetermineEditor(e.config)
424+
if err != nil {
425+
return "", err
423426
}
424-
err := prompt.SurveyAskOne(q, &result)
425-
return result, err
427+
428+
return surveyext.Edit(editorCommand, filename, startingText, e.io.In, e.io.Out, e.io.ErrOut, nil)
426429
}

pkg/cmd/pr/merge/merge_test.go

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -689,61 +689,68 @@ func TestPRMerge_interactiveWithDeleteBranch(t *testing.T) {
689689
}
690690

691691
func TestPRMerge_interactiveSquashEditCommitMsg(t *testing.T) {
692-
http := initFakeHTTP()
693-
defer http.Verify(t)
694-
http.Register(
695-
httpmock.GraphQL(`query PullRequestForBranch\b`),
692+
io, _, stdout, stderr := iostreams.Test()
693+
io.SetStdoutTTY(true)
694+
io.SetStderrTTY(true)
695+
696+
tr := initFakeHTTP()
697+
defer tr.Verify(t)
698+
699+
tr.Register(
700+
httpmock.GraphQL(`query PullRequestByNumber\b`),
696701
httpmock.StringResponse(`
697-
{ "data": { "repository": { "pullRequests": { "nodes": [{
698-
"headRefName": "blueberries",
702+
{ "data": { "repository": { "pullRequest": {
699703
"headRepositoryOwner": {"login": "OWNER"},
700704
"id": "THE-ID",
701705
"number": 3,
702706
"title": "title"
703-
}] } } } }`))
704-
http.Register(
707+
} } } }`))
708+
tr.Register(
705709
httpmock.GraphQL(`query RepositoryInfo\b`),
706710
httpmock.StringResponse(`
707711
{ "data": { "repository": {
708712
"mergeCommitAllowed": true,
709713
"rebaseMergeAllowed": true,
710714
"squashMergeAllowed": true
711715
} } }`))
712-
http.Register(
716+
tr.Register(
713717
httpmock.GraphQL(`query PullRequestMergeText\b`),
714718
httpmock.StringResponse(`
715719
{ "data": { "node": {
716-
"viewerMergeBodyText": ""
720+
"viewerMergeBodyText": "default body text"
717721
} } }`))
718-
http.Register(
722+
tr.Register(
719723
httpmock.GraphQL(`mutation PullRequestMerge\b`),
720724
httpmock.GraphQLMutation(`{}`, func(input map[string]interface{}) {
721725
assert.Equal(t, "THE-ID", input["pullRequestId"].(string))
722726
assert.Equal(t, "SQUASH", input["mergeMethod"].(string))
723-
assert.Equal(t, "cool story", input["commitBody"].(string))
727+
assert.Equal(t, "DEFAULT BODY TEXT", input["commitBody"].(string))
724728
}))
725729

726-
cs, cmdTeardown := run.Stub()
730+
_, cmdTeardown := run.Stub()
727731
defer cmdTeardown(t)
728732

729-
cs.Register("git -c log.ShowSignature=false log --pretty=format:%H,%s -1", 0, "")
730-
cs.Register(`git config --get-regexp.+branch\\\.blueberries\\\.`, 0, "")
731-
732733
as, surveyTeardown := prompt.InitAskStubber()
733734
defer surveyTeardown()
734735

735736
as.StubOne(2) // Merge method survey
736737
as.StubOne(false) // Delete branch survey
737738
as.StubOne("Edit commit message") // Confirm submit survey
738-
as.StubOne("cool story") // Edit commit message survey
739739
as.StubOne("Submit") // Confirm submit survey
740740

741-
output, err := runCommand(http, "blueberries", true, "")
742-
if err != nil {
743-
t.Fatalf("Got unexpected error running `pr merge` %s", err)
744-
}
741+
err := mergeRun(&MergeOptions{
742+
IO: io,
743+
Editor: testEditor{},
744+
HttpClient: func() (*http.Client, error) {
745+
return &http.Client{Transport: tr}, nil
746+
},
747+
SelectorArg: "https://github.com/OWNER/REPO/pull/123",
748+
InteractiveMode: true,
749+
})
750+
assert.NoError(t, err)
745751

746-
assert.Equal(t, "✓ Squashed and merged pull request #3 (title)\n", output.Stderr())
752+
assert.Equal(t, "", stdout.String())
753+
assert.Equal(t, "✓ Squashed and merged pull request #3 (title)\n", stderr.String())
747754
}
748755

749756
func TestPRMerge_interactiveCancelled(t *testing.T) {
@@ -885,3 +892,9 @@ func TestMergeRun_disableAutoMerge(t *testing.T) {
885892
assert.Equal(t, "", stdout.String())
886893
assert.Equal(t, "✓ Auto-merge disabled for pull request #123\n", stderr.String())
887894
}
895+
896+
type testEditor struct{}
897+
898+
func (e testEditor) Edit(filename, text string) (string, error) {
899+
return strings.ToUpper(text), nil
900+
}

0 commit comments

Comments
 (0)
X Tutup