Skip to content

Commit 71a66cc

Browse files
committed
Fix merge
1 parent a912396 commit 71a66cc

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

pkg/cmd/pr/merge/merge.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,21 @@ func mergeRun(opts *MergeOptions) error {
130130

131131
deleteBranch := opts.DeleteBranch
132132
crossRepoPR := pr.HeadRepositoryOwner.Login != baseRepo.RepoOwner()
133+
isTerminal := opts.IO.IsStdoutTTY()
133134

134-
if opts.InteractiveMode {
135-
mergeMethod, deleteBranch, err = prInteractiveMerge(opts.DeleteLocalBranch, crossRepoPR, baseRepo, apiClient)
136-
if err != nil {
137-
return nil
135+
isPRAlreadyMerged := pr.State == "MERGED"
136+
if !isPRAlreadyMerged {
137+
mergeMethod := opts.MergeMethod
138+
139+
if opts.InteractiveMode {
140+
mergeMethod, deleteBranch, err = prInteractiveMerge(opts, crossRepoPR, baseRepo, apiClient)
141+
if err != nil {
142+
if errors.Is(err, cancelError) {
143+
fmt.Fprintln(opts.IO.ErrOut, "Cancelled.")
144+
return cmdutil.SilentError
145+
}
146+
return err
147+
}
138148
}
139149

140150
err = api.PullRequestMerge(apiClient, baseRepo, pr, mergeMethod)
@@ -219,7 +229,9 @@ func mergeRun(opts *MergeOptions) error {
219229
return nil
220230
}
221231

222-
func prInteractiveMerge(deleteLocalBranch bool, crossRepoPR bool, repo ghrepo.Interface, apiClient *api.Client) (api.PullRequestMergeMethod, bool, error) {
232+
var cancelError = errors.New("cancelError")
233+
234+
func prInteractiveMerge(opts *MergeOptions, crossRepoPR bool, repo ghrepo.Interface, apiClient *api.Client) (api.PullRequestMergeMethod, bool, error) {
223235
var baseRepo *api.Repository
224236

225237
if r, ok := repo.(*api.Repository); ok {
@@ -232,29 +244,29 @@ func prInteractiveMerge(deleteLocalBranch bool, crossRepoPR bool, repo ghrepo.In
232244
}
233245
}
234246

235-
var opts []string
247+
var mergeOpts []string
236248

237249
if baseRepo.MergeCommitAllowed {
238-
opts = append(opts, "Create a merge commit")
250+
mergeOpts = append(mergeOpts, "Create a merge commit")
239251
}
240252

241253
if baseRepo.RebaseMergeAllowed {
242-
opts = append(opts, "Rebase and merge")
254+
mergeOpts = append(mergeOpts, "Rebase and merge")
243255
}
244256

245257
if baseRepo.SquashMergeAllowed {
246-
opts = append(opts, "Squash and merge")
258+
mergeOpts = append(mergeOpts, "Squash and merge")
247259
}
248260

249-
if len(opts) == 0 {
261+
if len(mergeOpts) == 0 {
250262
return 0, false, fmt.Errorf("no merge options enabled, please enable at least one for your repo")
251263
}
252264

253265
mergeMethodQuestion := &survey.Question{
254266
Name: "mergeMethod",
255267
Prompt: &survey.Select{
256268
Message: "What merge method would you like to use?",
257-
Options: opts,
269+
Options: mergeOpts,
258270
Default: "Create a merge commit",
259271
},
260272
}

0 commit comments

Comments
 (0)