11package delete
22
33import (
4+ "context"
45 "fmt"
56 "net/http"
67 "strconv"
78
89 "github.com/AlecAivazis/survey/v2"
9- "github.com/cli/cli/v2/api"
1010 "github.com/cli/cli/v2/internal/config"
11+ "github.com/cli/cli/v2/internal/ghinstance"
1112 "github.com/cli/cli/v2/internal/ghrepo"
1213 "github.com/cli/cli/v2/pkg/cmd/issue/shared"
1314 "github.com/cli/cli/v2/pkg/cmdutil"
1415 "github.com/cli/cli/v2/pkg/iostreams"
1516 "github.com/cli/cli/v2/pkg/prompt"
17+ graphql "github.com/cli/shurcooL-graphql"
18+ "github.com/shurcooL/githubv4"
1619 "github.com/spf13/cobra"
1720)
1821
@@ -61,12 +64,14 @@ func deleteRun(opts *DeleteOptions) error {
6164 if err != nil {
6265 return err
6366 }
64- apiClient := api .NewClientFromHTTP (httpClient )
6567
66- issue , baseRepo , err := shared .IssueFromArg ( apiClient , opts .BaseRepo , opts .SelectorArg )
68+ issue , baseRepo , err := shared .IssueFromArgWithFields ( httpClient , opts .BaseRepo , opts .SelectorArg , [] string { "id" , "number" , "title" } )
6769 if err != nil {
6870 return err
6971 }
72+ if issue .IsPullRequest () {
73+ return fmt .Errorf ("issue #%d is a pull request and cannot be deleted" , issue .Number )
74+ }
7075
7176 // When executed in an interactive shell, require confirmation. Otherwise skip confirmation.
7277 if opts .IO .CanPrompt () {
@@ -87,12 +92,32 @@ func deleteRun(opts *DeleteOptions) error {
8792 }
8893 }
8994
90- err = api .IssueDelete (apiClient , baseRepo , * issue )
91- if err != nil {
95+ if err := apiDelete (httpClient , baseRepo , issue .ID ); err != nil {
9296 return err
9397 }
9498
95- fmt .Fprintf (opts .IO .ErrOut , "%s Deleted issue #%d (%s).\n " , cs .Red ("✔" ), issue .Number , issue .Title )
99+ if opts .IO .IsStdoutTTY () {
100+ fmt .Fprintf (opts .IO .ErrOut , "%s Deleted issue #%d (%s).\n " , cs .Red ("✔" ), issue .Number , issue .Title )
101+ }
96102
97103 return nil
98104}
105+
106+ func apiDelete (httpClient * http.Client , repo ghrepo.Interface , issueID string ) error {
107+ var mutation struct {
108+ DeleteIssue struct {
109+ Repository struct {
110+ ID githubv4.ID
111+ }
112+ } `graphql:"deleteIssue(input: $input)"`
113+ }
114+
115+ variables := map [string ]interface {}{
116+ "input" : githubv4.DeleteIssueInput {
117+ IssueID : issueID ,
118+ },
119+ }
120+
121+ gql := graphql .NewClient (ghinstance .GraphQLEndpoint (repo .RepoHost ()), httpClient )
122+ return gql .MutateNamed (context .Background (), "IssueDelete" , & mutation , variables )
123+ }
0 commit comments