forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete.go
More file actions
123 lines (104 loc) · 2.96 KB
/
delete.go
File metadata and controls
123 lines (104 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package delete
import (
"context"
"fmt"
"net/http"
"strconv"
"github.com/AlecAivazis/survey/v2"
"github.com/cli/cli/v2/internal/config"
"github.com/cli/cli/v2/internal/ghinstance"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/cmd/issue/shared"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/cli/cli/v2/pkg/prompt"
graphql "github.com/cli/shurcooL-graphql"
"github.com/shurcooL/githubv4"
"github.com/spf13/cobra"
)
type DeleteOptions struct {
HttpClient func() (*http.Client, error)
Config func() (config.Config, error)
IO *iostreams.IOStreams
BaseRepo func() (ghrepo.Interface, error)
SelectorArg string
}
func NewCmdDelete(f *cmdutil.Factory, runF func(*DeleteOptions) error) *cobra.Command {
opts := &DeleteOptions{
IO: f.IOStreams,
HttpClient: f.HttpClient,
Config: f.Config,
}
cmd := &cobra.Command{
Use: "delete {<number> | <url>}",
Short: "Delete issue",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// support `-R, --repo` override
opts.BaseRepo = f.BaseRepo
if len(args) > 0 {
opts.SelectorArg = args[0]
}
if runF != nil {
return runF(opts)
}
return deleteRun(opts)
},
}
return cmd
}
func deleteRun(opts *DeleteOptions) error {
cs := opts.IO.ColorScheme()
httpClient, err := opts.HttpClient()
if err != nil {
return err
}
issue, baseRepo, err := shared.IssueFromArgWithFields(httpClient, opts.BaseRepo, opts.SelectorArg, []string{"id", "number", "title"})
if err != nil {
return err
}
if issue.IsPullRequest() {
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() {
answer := ""
err = prompt.SurveyAskOne(
&survey.Input{
Message: fmt.Sprintf("You're going to delete issue #%d. This action cannot be reversed. To confirm, type the issue number:", issue.Number),
},
&answer,
)
if err != nil {
return err
}
answerInt, err := strconv.Atoi(answer)
if err != nil || answerInt != issue.Number {
fmt.Fprintf(opts.IO.Out, "Issue #%d was not deleted.\n", issue.Number)
return nil
}
}
if err := apiDelete(httpClient, baseRepo, issue.ID); err != nil {
return err
}
if opts.IO.IsStdoutTTY() {
fmt.Fprintf(opts.IO.ErrOut, "%s Deleted issue #%d (%s).\n", cs.Red("✔"), issue.Number, issue.Title)
}
return nil
}
func apiDelete(httpClient *http.Client, repo ghrepo.Interface, issueID string) error {
var mutation struct {
DeleteIssue struct {
Repository struct {
ID githubv4.ID
}
} `graphql:"deleteIssue(input: $input)"`
}
variables := map[string]interface{}{
"input": githubv4.DeleteIssueInput{
IssueID: issueID,
},
}
gql := graphql.NewClient(ghinstance.GraphQLEndpoint(repo.RepoHost()), httpClient)
return gql.MutateNamed(context.Background(), "IssueDelete", &mutation, variables)
}