@@ -19,8 +19,6 @@ import (
1919 "github.com/spf13/cobra"
2020)
2121
22- var cancelError = errors .New ("cancelError" )
23-
2422type MergeOptions struct {
2523 HttpClient func () (* http.Client , error )
2624 Config func () (config.Config , error )
@@ -143,15 +141,22 @@ func mergeRun(opts *MergeOptions) error {
143141 if err != nil {
144142 return err
145143 }
146-
147- mergeMethod , deleteBranch , err = prInteractiveMerge (opts , r , crossRepoPR )
144+ mergeMethod , err = mergeMethodSurvey (r )
145+ if err != nil {
146+ return err
147+ }
148+ deleteBranch , err = deleteBranchSurvey (opts , crossRepoPR )
149+ if err != nil {
150+ return err
151+ }
152+ confirm , err := confirmSurvey ()
148153 if err != nil {
149- if errors .Is (err , cancelError ) {
150- fmt .Fprintln (opts .IO .ErrOut , "Cancelled." )
151- return cmdutil .SilentError
152- }
153154 return err
154155 }
156+ if ! confirm {
157+ fmt .Fprintln (opts .IO .ErrOut , "Cancelled." )
158+ return cmdutil .SilentError
159+ }
155160 }
156161
157162 err = api .PullRequestMerge (apiClient , baseRepo , pr , mergeMethod )
@@ -236,32 +241,46 @@ func mergeRun(opts *MergeOptions) error {
236241 return nil
237242}
238243
239- func prInteractiveMerge (opts * MergeOptions , baseRepo * api.Repository , crossRepoPR bool ) (api.PullRequestMergeMethod , bool , error ) {
240- var mergeOpts []string
244+ func mergeMethodSurvey (baseRepo * api.Repository ) (api.PullRequestMergeMethod , error ) {
245+ type mergeOption struct {
246+ title string
247+ method api.PullRequestMergeMethod
248+ }
249+
250+ var mergeOpts []mergeOption
241251 if baseRepo .MergeCommitAllowed {
242- mergeOpts = append (mergeOpts , "Create a merge commit" )
252+ opt := mergeOption {title : "Create a merge commit" , method : api .PullRequestMergeMethodMerge }
253+ mergeOpts = append (mergeOpts , opt )
243254 }
244255 if baseRepo .RebaseMergeAllowed {
245- mergeOpts = append (mergeOpts , "Rebase and merge" )
256+ opt := mergeOption {title : "Rebase and merge" , method : api .PullRequestMergeMethodRebase }
257+ mergeOpts = append (mergeOpts , opt )
246258 }
247259 if baseRepo .SquashMergeAllowed {
248- mergeOpts = append (mergeOpts , "Squash and merge" )
260+ opt := mergeOption {title : "Squash and merge" , method : api .PullRequestMergeMethodSquash }
261+ mergeOpts = append (mergeOpts , opt )
249262 }
250263 if len (mergeOpts ) == 0 {
251- return 0 , false , fmt .Errorf ("no merge options enabled, please enable at least one for your repo" )
264+ return 0 , fmt .Errorf ("The repo %s has no merge options enabled" , ghrepo . FullName ( baseRepo ) )
252265 }
253266
254- mergeMethodQuestion := & survey.Question {
255- Name : "mergeMethod" ,
256- Prompt : & survey.Select {
257- Message : "What merge method would you like to use?" ,
258- Options : mergeOpts ,
259- Default : "Create a merge commit" ,
260- },
267+ var surveyOpts []string
268+ for _ , v := range mergeOpts {
269+ surveyOpts = append (surveyOpts , v .title )
261270 }
262271
263- qs := []* survey.Question {mergeMethodQuestion }
272+ prompt := & survey.Select {
273+ Message : "What merge method would you like to use?" ,
274+ Options : surveyOpts ,
275+ Default : "Create a merge commit" ,
276+ }
264277
278+ var result int
279+ err := survey .AskOne (prompt , & result )
280+ return mergeOpts [result ].method , err
281+ }
282+
283+ func deleteBranchSurvey (opts * MergeOptions , crossRepoPR bool ) (bool , error ) {
265284 if ! crossRepoPR && ! opts .IsDeleteBranchIndicated {
266285 var message string
267286 if opts .CanDeleteLocalBranch {
@@ -270,49 +289,23 @@ func prInteractiveMerge(opts *MergeOptions, baseRepo *api.Repository, crossRepoP
270289 message = "Delete the branch on GitHub?"
271290 }
272291
273- deleteBranchQuestion := & survey.Question {
274- Name : "deleteBranch" ,
275- Prompt : & survey.Confirm {
276- Message : message ,
277- Default : false ,
278- },
279- }
280- qs = append (qs , deleteBranchQuestion )
281- }
282-
283- qs = append (qs , & survey.Question {
284- Name : "isConfirmed" ,
285- Prompt : & survey.Confirm {
286- Message : "Submit?" ,
292+ var result bool
293+ submit := & survey.Confirm {
294+ Message : message ,
287295 Default : false ,
288- },
289- })
290-
291- answers := struct {
292- MergeMethod int
293- DeleteBranch bool
294- IsConfirmed bool
295- }{
296- DeleteBranch : opts .DeleteBranch ,
297- }
298-
299- err := prompt .SurveyAsk (qs , & answers )
300- if err != nil {
301- return 0 , false , fmt .Errorf ("could not prompt: %w" , err )
302- }
303- if ! answers .IsConfirmed {
304- return 0 , false , cancelError
296+ }
297+ err := survey .AskOne (submit , & result )
298+ return result , err
305299 }
300+ return false , nil
301+ }
306302
307- var mergeMethod api.PullRequestMergeMethod
308- switch answers .MergeMethod {
309- case 0 :
310- mergeMethod = api .PullRequestMergeMethodMerge
311- case 1 :
312- mergeMethod = api .PullRequestMergeMethodRebase
313- case 2 :
314- mergeMethod = api .PullRequestMergeMethodSquash
303+ func confirmSurvey () (bool , error ) {
304+ var confirm bool
305+ submit := & survey.Confirm {
306+ Message : "Submit?" ,
307+ Default : true ,
315308 }
316-
317- return mergeMethod , answers . DeleteBranch , nil
309+ err := survey . AskOne ( submit , & confirm )
310+ return confirm , err
318311}
0 commit comments