Skip to content

Commit bd16026

Browse files
committed
Fix tests and polish
1 parent 290355a commit bd16026

File tree

3 files changed

+68
-206
lines changed

3 files changed

+68
-206
lines changed

api/queries_pr.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,9 @@ type PullRequest struct {
116116
Milestone struct {
117117
Title string
118118
}
119-
Comments Comments
120-
ReactionGroups ReactionGroups
121-
Reviews PullRequestReviews
122-
ViewerMergeBodyText string
123-
ViewerMergeHeadlineText string
119+
Comments Comments
120+
ReactionGroups ReactionGroups
121+
Reviews PullRequestReviews
124122
}
125123

126124
type NotFoundError struct {
@@ -598,8 +596,6 @@ func PullRequestByNumber(client *Client, repo ghrepo.Interface, number int) (*Pu
598596
milestone{
599597
title
600598
}
601-
viewerMergeBodyText
602-
viewerMergeHeadlineText
603599
` + commentsFragment() + `
604600
` + reactionGroupsFragment() + `
605601
}

pkg/cmd/pr/merge/merge.go

Lines changed: 39 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ type MergeOptions struct {
3232
DeleteBranch bool
3333
MergeMethod api.PullRequestMergeMethod
3434

35-
Body *string
35+
Body string
36+
BodyProvided bool
3637

3738
IsDeleteBranchIndicated bool
3839
CanDeleteLocalBranch bool
@@ -99,8 +100,7 @@ func NewCmdMerge(f *cmdutil.Factory, runF func(*MergeOptions) error) *cobra.Comm
99100
opts.CanDeleteLocalBranch = !cmd.Flags().Changed("repo")
100101

101102
if cmd.Flags().Changed("body") {
102-
bodyStr, _ := cmd.Flags().GetString("body")
103-
opts.Body = &bodyStr
103+
opts.BodyProvided = true
104104
}
105105

106106
if runF != nil {
@@ -111,7 +111,7 @@ func NewCmdMerge(f *cmdutil.Factory, runF func(*MergeOptions) error) *cobra.Comm
111111
}
112112

113113
cmd.Flags().BoolVarP(&opts.DeleteBranch, "delete-branch", "d", false, "Delete the local and remote branch after merge")
114-
cmd.Flags().StringP("body", "b", "", "Body for merge commit")
114+
cmd.Flags().StringVarP(&opts.Body, "body", "b", "", "Body for merge commit")
115115
cmd.Flags().BoolVarP(&flagMerge, "merge", "m", false, "Merge the commits with the base branch")
116116
cmd.Flags().BoolVarP(&flagRebase, "rebase", "r", false, "Rebase the commits onto the base branch")
117117
cmd.Flags().BoolVarP(&flagSquash, "squash", "s", false, "Squash the commits into one commit and merge it into the base branch")
@@ -155,8 +155,6 @@ func mergeRun(opts *MergeOptions) error {
155155
if !isPRAlreadyMerged {
156156
mergeMethod := opts.MergeMethod
157157

158-
action := shared.SubmitAction
159-
160158
if opts.InteractiveMode {
161159
r, err := api.GitHubRepo(apiClient, baseRepo)
162160
if err != nil {
@@ -171,9 +169,9 @@ func mergeRun(opts *MergeOptions) error {
171169
return err
172170
}
173171

174-
allowEditMsg := mergeMethod != api.PullRequestMergeMethodRebase
172+
allowEditMsg := (mergeMethod != api.PullRequestMergeMethodRebase) && !opts.BodyProvided
175173

176-
action, err = confirmSubmission(allowEditMsg)
174+
action, err := confirmSurvey(allowEditMsg)
177175
if err != nil {
178176
return fmt.Errorf("unable to confirm: %w", err)
179177
}
@@ -185,37 +183,32 @@ func mergeRun(opts *MergeOptions) error {
185183
return err
186184
}
187185

188-
if opts.Body == nil {
189-
var body string
190-
191-
if mergeMethod == api.PullRequestMergeMethodSquash {
192-
body = fmt.Sprintf("%s\n\n%s", pr.ViewerMergeHeadlineText, pr.ViewerMergeBodyText)
193-
}
194-
195-
opts.Body = &body
196-
}
197-
198-
err := commitMsgSurvey(opts.Body, editorCommand)
186+
msg, err := commitMsgSurvey(editorCommand)
199187
if err != nil {
200188
return err
201189
}
190+
opts.Body = msg
191+
opts.BodyProvided = true
202192

203-
action, err = confirmSubmission(false)
193+
action, err = confirmSurvey(false)
204194
if err != nil {
205195
return fmt.Errorf("unable to confirm: %w", err)
206196
}
207197
}
208-
209198
if action == shared.CancelAction {
210199
fmt.Fprintln(opts.IO.ErrOut, "Cancelled.")
211200
return cmdutil.SilentError
212201
}
213202
}
214-
if action == shared.SubmitAction {
215-
err = api.PullRequestMerge(apiClient, baseRepo, pr, mergeMethod, opts.Body)
216-
if err != nil {
217-
return err
218-
}
203+
204+
var body *string
205+
if opts.BodyProvided {
206+
body = &opts.Body
207+
}
208+
209+
err = api.PullRequestMerge(apiClient, baseRepo, pr, mergeMethod, body)
210+
if err != nil {
211+
return err
219212
}
220213

221214
if isTerminal {
@@ -352,69 +345,48 @@ func deleteBranchSurvey(opts *MergeOptions, crossRepoPR bool) (bool, error) {
352345
return opts.DeleteBranch, nil
353346
}
354347

355-
func confirmSubmission(allowEditMsg bool) (shared.Action, error) {
348+
func confirmSurvey(allowEditMsg bool) (shared.Action, error) {
356349
const (
357350
submitLabel = "Submit"
358-
editCommitMsgLabel = "Edit Commit Message"
351+
editCommitMsgLabel = "Edit commit message"
359352
cancelLabel = "Cancel"
360353
)
354+
361355
options := []string{submitLabel}
362356
if allowEditMsg {
363357
options = append(options, editCommitMsgLabel)
364358
}
365359
options = append(options, cancelLabel)
366360

367-
confirmAnswers := struct {
368-
Confirmation int
369-
}{}
370-
371-
confirmQs := []*survey.Question{
372-
{
373-
Name: "confirmation",
374-
Prompt: &survey.Select{
375-
Message: "What's next?",
376-
Options: options,
377-
},
378-
},
361+
var result string
362+
submit := &survey.Select{
363+
Message: "What's next?",
364+
Options: options,
379365
}
380-
381-
err := prompt.SurveyAsk(confirmQs, &confirmAnswers)
366+
err := prompt.SurveyAskOne(submit, &result)
382367
if err != nil {
383-
return -1, fmt.Errorf("could not prompt: %w", err)
368+
return shared.CancelAction, fmt.Errorf("could not prompt: %w", err)
384369
}
385370

386-
switch options[confirmAnswers.Confirmation] {
371+
switch result {
387372
case submitLabel:
388373
return shared.SubmitAction, nil
389374
case editCommitMsgLabel:
390375
return shared.EditCommitMessageAction, nil
391-
case cancelLabel:
392-
return shared.CancelAction, nil
393376
default:
394-
return -1, fmt.Errorf("invalid index: %d", confirmAnswers.Confirmation)
377+
return shared.CancelAction, nil
395378
}
396379
}
397380

398-
func commitMsgSurvey(body *string, editorCommand string) error {
399-
qs := []*survey.Question{
400-
{
401-
Name: "body",
402-
Prompt: &surveyext.GhEditor{
403-
BlankAllowed: true,
404-
EditorCommand: editorCommand,
405-
Editor: &survey.Editor{
406-
Message: "Body",
407-
AppendDefault: true,
408-
Default: *body,
409-
FileName: "*.md",
410-
},
411-
},
381+
func commitMsgSurvey(editorCommand string) (string, error) {
382+
var result string
383+
q := &surveyext.GhEditor{
384+
EditorCommand: editorCommand,
385+
Editor: &survey.Editor{
386+
Message: "Body",
387+
FileName: "*.md",
412388
},
413389
}
414-
err := prompt.SurveyAsk(qs, body)
415-
if err != nil {
416-
return err
417-
}
418-
419-
return nil
390+
err := prompt.SurveyAskOne(q, &result)
391+
return result, err
420392
}

0 commit comments

Comments
 (0)