Repository: cli/cli
File: pkg/cmd/pr/merge/merge.go (Go)

Code before:

		var action string
		if mergeMethod == api.PullRequestMergeMethodRebase {
			action = "Rebased and merged"
			err = api.PullRequestMerge(apiClient, baseRepo, pr, api.PullRequestMergeMethodRebase)
		} else if mergeMethod == api.PullRequestMergeMethodSquash {
			action = "Squashed and merged"
			err = api.PullRequestMerge(apiClient, baseRepo, pr, api.PullRequestMergeMethodSquash)
		} else if mergeMethod == api.PullRequestMergeMethodMerge {
			action = "Merged"
			err = api.PullRequestMerge(apiClient, baseRepo, pr, api.PullRequestMergeMethodMerge)
		} else {
			err = fmt.Errorf("unknown merge method (%d) used", mergeMethod)
			return err
		}

		if err != nil {
			return fmt.Errorf("API call failed: %w", err)
		}

		if isTerminal {
			fmt.Fprintf(opts.IO.ErrOut, "%s %s pull request #%d (%s)\n", cs.Magenta("✔"), action, pr.Number, pr.Title)
		}
	} else {
		err := prompt.SurveyAskOne(&survey.Confirm{
			Message: fmt.Sprintf("Pull request #%d (%s) was already merged. Would you like to delete this local branch and switch to the default branch?", pr.Number, pr.Title),
			Default: false,
		}, &deleteBranch)

		if err != nil {
			return fmt.Errorf("could not prompt: %w", err)
		}
	}

	if deleteBranch {
		branchSwitchString := ""

		if opts.DeleteLocalBranch && !crossRepoPR {
			currentBranch, err := opts.Branch()
			if err != nil {
				return err
			}

			var branchToSwitchTo string
			if currentBranch == pr.HeadRefName {
				branchToSwitchTo, err = api.RepoDefaultBranch(apiClient, baseRepo)
				if err != nil {
					return err
				}
				err = git.CheckoutBranch(branchToSwitchTo)
				if err != nil {

Review comment (suggestion):
If we are not in interactive mode, this prompt should never happen. Additionally, if the user supplied `--delete-branch`, this prompt should never happen either — we can just continue in that case.

If you'd like, you could try additionally addressing #2715 in this PR! It will touch on the same logic.

Code after:
				return err
			}
		}

		err = api.PullRequestMerge(apiClient, baseRepo, pr, mergeMethod)
		if err != nil {
			return err
		}

		if isTerminal {
			action := "Merged"
			switch mergeMethod {
			case api.PullRequestMergeMethodRebase:
				action = "Rebased and merged"
			case api.PullRequestMergeMethodSquash:
				action = "Squashed and merged"
			}
			fmt.Fprintf(opts.IO.ErrOut, "%s %s pull request #%d (%s)\n", cs.Magenta("✔"), action, pr.Number, pr.Title)
		}
	} else if !opts.IsDeleteBranchIndicated && opts.InteractiveMode {
		err := prompt.SurveyAskOne(&survey.Confirm{
			Message: fmt.Sprintf("Pull request #%d was already merged. Delete the branch locally?", pr.Number),
			Default: false,
		}, &deleteBranch)
		if err != nil {
			return fmt.Errorf("could not prompt: %w", err)
		}
	}

	if !deleteBranch {
		return nil
	}

	branchSwitchString := ""

	if opts.CanDeleteLocalBranch && !crossRepoPR {
		currentBranch, err := opts.Branch()
		if err != nil {
			return err
		}

		var branchToSwitchTo string
		if currentBranch == pr.HeadRefName {
			branchToSwitchTo, err = api.RepoDefaultBranch(apiClient, baseRepo)
			if err != nil {
				return err
			}
			err = git.CheckoutBranch(branchToSwitchTo)
			if err != nil {
				return err
			}