Add 'confirm' flag for issue delete subcommand

This commit is contained in:
Ryan Opel 2022-04-02 18:26:49 -07:00
parent 2df3dd4f7d
commit a97dae6269
2 changed files with 37 additions and 2 deletions

View file

@ -26,6 +26,7 @@ type DeleteOptions struct {
BaseRepo func() (ghrepo.Interface, error)
SelectorArg string
Confirmed bool
}
func NewCmdDelete(f *cmdutil.Factory, runF func(*DeleteOptions) error) *cobra.Command {
@ -54,6 +55,7 @@ func NewCmdDelete(f *cmdutil.Factory, runF func(*DeleteOptions) error) *cobra.Co
},
}
cmd.Flags().BoolVar(&opts.Confirmed, "confirm", false, "confirm deletion without prompting")
return cmd
}
@ -73,8 +75,9 @@ func deleteRun(opts *DeleteOptions) error {
return fmt.Errorf("issue #%d is a pull request and cannot be deleted", issue.Number)
}
// When executed in an interactive shell, require confirmation. Otherwise skip confirmation.
if opts.IO.CanPrompt() {
// When executed in an interactive shell, require confirmation, unless
// already provided. Otherwise skip confirmation.
if opts.IO.CanPrompt() && !opts.Confirmed {
answer := ""
err = prompt.SurveyAskOne(
&survey.Input{

View file

@ -91,6 +91,38 @@ func TestIssueDelete(t *testing.T) {
}
}
func TestIssueDelete_confirm(t *testing.T) {
httpRegistry := &httpmock.Registry{}
defer httpRegistry.Verify(t)
httpRegistry.Register(
httpmock.GraphQL(`query IssueByNumber\b`),
httpmock.StringResponse(`
{ "data": { "repository": {
"hasIssuesEnabled": true,
"issue": { "id": "THE-ID", "number": 13, "title": "The title of the issue"}
} } }`),
)
httpRegistry.Register(
httpmock.GraphQL(`mutation IssueDelete\b`),
httpmock.GraphQLMutation(`{"id": "THE-ID"}`,
func(inputs map[string]interface{}) {
assert.Equal(t, inputs["issueId"], "THE-ID")
}),
)
output, err := runCommand(httpRegistry, true, "13 --confirm")
if err != nil {
t.Fatalf("error running command `issue delete`: %v", err)
}
r := regexp.MustCompile(`Deleted issue #13 \(The title of the issue\)`)
if !r.MatchString(output.Stderr()) {
t.Fatalf("output did not match regexp /%s/\n> output\n%q\n", r, output.Stderr())
}
}
func TestIssueDelete_cancel(t *testing.T) {
httpRegistry := &httpmock.Registry{}
defer httpRegistry.Verify(t)