Remove overfetching from issue delete

This commit is contained in:
Mislav Marohnić 2021-11-23 19:33:58 +01:00
parent f99a149eb2
commit 5be6b67ce7
3 changed files with 49 additions and 30 deletions

View file

@ -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 {

View file

@ -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"
)
@ -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() {
@ -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)
}

View file

@ -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")