X Tutup
Skip to content

Commit 5be6b67

Browse files
committed
Remove overfetching from issue delete
1 parent f99a149 commit 5be6b67

File tree

3 files changed

+49
-30
lines changed

3 files changed

+49
-30
lines changed

api/queries_issue.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -342,27 +342,6 @@ func IssueByNumber(client *Client, repo ghrepo.Interface, number int) (*Issue, e
342342
return &resp.Repository.Issue, nil
343343
}
344344

345-
func IssueDelete(client *Client, repo ghrepo.Interface, issue Issue) error {
346-
var mutation struct {
347-
DeleteIssue struct {
348-
Repository struct {
349-
ID githubv4.ID
350-
}
351-
} `graphql:"deleteIssue(input: $input)"`
352-
}
353-
354-
variables := map[string]interface{}{
355-
"input": githubv4.DeleteIssueInput{
356-
IssueID: issue.ID,
357-
},
358-
}
359-
360-
gql := graphQLClient(client.http, repo.RepoHost())
361-
err := gql.MutateNamed(context.Background(), "IssueDelete", &mutation, variables)
362-
363-
return err
364-
}
365-
366345
func IssueUpdate(client *Client, repo ghrepo.Interface, params githubv4.UpdateIssueInput) error {
367346
var mutation struct {
368347
UpdateIssue struct {

pkg/cmd/issue/delete/delete.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
package delete
22

33
import (
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+
}

pkg/cmd/issue/delete/delete_test.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,24 @@ func TestIssueDelete_issuesDisabled(t *testing.T) {
145145
httpRegistry.Register(
146146
httpmock.GraphQL(`query IssueByNumber\b`),
147147
httpmock.StringResponse(`
148-
{ "data": { "repository": {
149-
"hasIssuesEnabled": false
150-
} } }`),
148+
{
149+
"data": {
150+
"repository": {
151+
"hasIssuesEnabled": false,
152+
"issue": null
153+
}
154+
},
155+
"errors": [
156+
{
157+
"type": "NOT_FOUND",
158+
"path": [
159+
"repository",
160+
"issue"
161+
],
162+
"message": "Could not resolve to an issue or pull request with the number of 13."
163+
}
164+
]
165+
}`),
151166
)
152167

153168
_, err := runCommand(httpRegistry, true, "13")

0 commit comments

Comments
 (0)
X Tutup