Skip to content

Commit d71ea6b

Browse files
authored
feat: offer "Edit PR title and retry" when merge fails in add-wizard (#21261)
1 parent 0067149 commit d71ea6b

1 file changed

Lines changed: 44 additions & 3 deletions

File tree

pkg/cli/add_interactive_git.go

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func (c *AddInteractiveConfig) createWorkflowPRAndConfigureSecret(ctx context.Co
5656
type mergeAction string
5757
const (
5858
mergeActionAttempt mergeAction = "attempt"
59+
mergeActionEditTitle mergeAction = "editTitle"
5960
mergeActionReview mergeAction = "review"
6061
mergeActionConfirmed mergeAction = "confirmed"
6162
mergeActionExit mergeAction = "exit"
@@ -69,8 +70,10 @@ func (c *AddInteractiveConfig) createWorkflowPRAndConfigureSecret(ctx context.Co
6970
// Build option list based on current state
7071
var options []huh.Option[mergeAction]
7172

72-
if !mergeFailed {
73-
options = append(options, huh.NewOption("Attempt to merge", mergeActionAttempt))
73+
options = append(options, huh.NewOption("Attempt to merge", mergeActionAttempt))
74+
75+
if mergeFailed {
76+
options = append(options, huh.NewOption("Edit PR title and retry", mergeActionEditTitle))
7477
}
7578

7679
if userReviewing {
@@ -107,14 +110,39 @@ func (c *AddInteractiveConfig) createWorkflowPRAndConfigureSecret(ctx context.Co
107110
mergeDone = true
108111
} else {
109112
fmt.Fprintln(os.Stderr, console.FormatWarningMessage(fmt.Sprintf("Failed to merge PR: %v", mergeErr)))
110-
fmt.Fprintln(os.Stderr, "Please merge the PR manually: "+result.PRURL)
113+
if mergeFailed {
114+
fmt.Fprintln(os.Stderr, "Please merge the PR manually: "+result.PRURL)
115+
}
111116
mergeFailed = true
112117
}
113118
} else {
114119
fmt.Fprintln(os.Stderr, console.FormatSuccessMessage("Merged pull request "+result.PRURL))
115120
mergeDone = true
116121
}
117122

123+
case mergeActionEditTitle:
124+
var newTitle string
125+
titleForm := huh.NewForm(
126+
huh.NewGroup(
127+
huh.NewInput().
128+
Title("Enter new PR title").
129+
Description("Add a prefix if required, for example: feat: or fix:").
130+
Value(&newTitle),
131+
),
132+
).WithAccessible(console.IsAccessibleMode())
133+
if titleErr := titleForm.Run(); titleErr != nil {
134+
return fmt.Errorf("failed to get user input: %w", titleErr)
135+
}
136+
newTitle = strings.TrimSpace(newTitle)
137+
if newTitle == "" {
138+
fmt.Fprintln(os.Stderr, console.FormatWarningMessage("PR title cannot be empty, keeping current title"))
139+
} else if editErr := editPRTitle(result.PRNumber, newTitle, c.RepoOverride); editErr != nil {
140+
fmt.Fprintln(os.Stderr, console.FormatWarningMessage(fmt.Sprintf("Failed to update PR title: %v", editErr)))
141+
} else {
142+
fmt.Fprintln(os.Stderr, console.FormatSuccessMessage("PR title updated to: "+newTitle))
143+
mergeFailed = false
144+
}
145+
118146
case mergeActionReview:
119147
userReviewing = true
120148
fmt.Fprintln(os.Stderr, console.FormatInfoMessage("Please review and merge the pull request: "+result.PRURL))
@@ -254,3 +282,16 @@ func (c *AddInteractiveConfig) mergePullRequest(prNumber int) error {
254282
}
255283
return nil
256284
}
285+
286+
// editPRTitle updates the title of the specified PR via the gh CLI.
287+
func editPRTitle(prNumber int, newTitle, repoOverride string) error {
288+
args := []string{"pr", "edit", strconv.Itoa(prNumber), "--title", newTitle}
289+
if repoOverride != "" {
290+
args = append(args, "--repo", repoOverride)
291+
}
292+
output, err := workflow.RunGHCombined("Updating PR title...", args...)
293+
if err != nil {
294+
return fmt.Errorf("failed to update PR title: %w (output: %s)", err, string(output))
295+
}
296+
return nil
297+
}

0 commit comments

Comments
 (0)