Skip to content

Commit

Permalink
Remove overfetching from issue delete
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Nov 23, 2021
1 parent f99a149 commit 5be6b67
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 30 deletions.
21 changes: 0 additions & 21 deletions api/queries_issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,27 +342,6 @@ func IssueByNumber(client *Client, repo ghrepo.Interface, number int) (*Issue, e
return &resp.Repository.Issue, nil
}

func IssueDelete(client *Client, repo ghrepo.Interface, issue Issue) error {
var mutation struct {
DeleteIssue struct {
Repository struct {
ID githubv4.ID
}
} `graphql:"deleteIssue(input: $input)"`
}

variables := map[string]interface{}{
"input": githubv4.DeleteIssueInput{
IssueID: issue.ID,
},
}

gql := graphQLClient(client.http, repo.RepoHost())
err := gql.MutateNamed(context.Background(), "IssueDelete", &mutation, variables)

return err
}

func IssueUpdate(client *Client, repo ghrepo.Interface, params githubv4.UpdateIssueInput) error {
var mutation struct {
UpdateIssue struct {
Expand Down
37 changes: 31 additions & 6 deletions pkg/cmd/issue/delete/delete.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package delete

import (
"context"
"fmt"
"net/http"
"strconv"

"github.com/AlecAivazis/survey/v2"
"github.com/cli/cli/v2/api"
"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"
)

Expand Down Expand Up @@ -61,12 +64,14 @@ func deleteRun(opts *DeleteOptions) error {
if err != nil {
return err
}
apiClient := api.NewClientFromHTTP(httpClient)

issue, baseRepo, err := shared.IssueFromArg(apiClient, opts.BaseRepo, opts.SelectorArg)
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() {
Expand All @@ -87,12 +92,32 @@ func deleteRun(opts *DeleteOptions) error {
}
}

err = api.IssueDelete(apiClient, baseRepo, *issue)
if err != nil {
if err := apiDelete(httpClient, baseRepo, issue.ID); err != nil {
return err
}

fmt.Fprintf(opts.IO.ErrOut, "%s Deleted issue #%d (%s).\n", cs.Red("✔"), issue.Number, issue.Title)
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)
}
21 changes: 18 additions & 3 deletions pkg/cmd/issue/delete/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,24 @@ func TestIssueDelete_issuesDisabled(t *testing.T) {
httpRegistry.Register(
httpmock.GraphQL(`query IssueByNumber\b`),
httpmock.StringResponse(`
{ "data": { "repository": {
"hasIssuesEnabled": false
} } }`),
{
"data": {
"repository": {
"hasIssuesEnabled": false,
"issue": null
}
},
"errors": [
{
"type": "NOT_FOUND",
"path": [
"repository",
"issue"
],
"message": "Could not resolve to an issue or pull request with the number of 13."
}
]
}`),
)

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

0 comments on commit 5be6b67

Please sign in to comment.