Improve issue close re: overfetching, handling PRs
- `issue close` no longer fetches all issue fields and thus avoids the problem when loading failed due to token not having access to projects - `issue close` now accepts either issue or pull number as argument.
This commit is contained in:
parent
1eb790cacd
commit
07cad386a5
8 changed files with 103 additions and 44 deletions
|
|
@ -1,15 +1,19 @@
|
|||
package close
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"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"
|
||||
graphql "github.com/cli/shurcooL-graphql"
|
||||
"github.com/shurcooL/githubv4"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
|
|
@ -58,9 +62,8 @@ func closeRun(opts *CloseOptions) 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", "state"})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -70,7 +73,7 @@ func closeRun(opts *CloseOptions) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
err = api.IssueClose(apiClient, baseRepo, *issue)
|
||||
err = apiClose(httpClient, baseRepo, issue)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -79,3 +82,26 @@ func closeRun(opts *CloseOptions) error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func apiClose(httpClient *http.Client, repo ghrepo.Interface, issue *api.Issue) error {
|
||||
if issue.IsPullRequest() {
|
||||
return api.PullRequestClose(httpClient, repo, issue.ID)
|
||||
}
|
||||
|
||||
var mutation struct {
|
||||
CloseIssue struct {
|
||||
Issue struct {
|
||||
ID githubv4.ID
|
||||
}
|
||||
} `graphql:"closeIssue(input: $input)"`
|
||||
}
|
||||
|
||||
variables := map[string]interface{}{
|
||||
"input": githubv4.CloseIssueInput{
|
||||
IssueID: issue.ID,
|
||||
},
|
||||
}
|
||||
|
||||
gql := graphql.NewClient(ghinstance.GraphQLEndpoint(repo.RepoHost()), httpClient)
|
||||
return gql.MutateNamed(context.Background(), "IssueClose", &mutation, variables)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -119,9 +119,24 @@ func TestIssueClose_issuesDisabled(t *testing.T) {
|
|||
http.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(http, true, "13")
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package shared
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
|
@ -86,14 +87,17 @@ func issueMetadataFromURL(s string) (int, ghrepo.Interface) {
|
|||
func findIssueOrPR(httpClient *http.Client, repo ghrepo.Interface, number int, fields []string) (*api.Issue, error) {
|
||||
type response struct {
|
||||
Repository struct {
|
||||
Issue *api.Issue
|
||||
HasIssuesEnabled bool
|
||||
Issue *api.Issue
|
||||
}
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(`
|
||||
query IssueByNumber($owner: String!, $repo: String!, $number: Int!) {
|
||||
repository(owner: $owner, name: $repo) {
|
||||
hasIssuesEnabled
|
||||
issue: issueOrPullRequest(number: $number) {
|
||||
__typename
|
||||
...on Issue{%[1]s}
|
||||
...on PullRequest{%[1]s}
|
||||
}
|
||||
|
|
@ -109,8 +113,16 @@ func findIssueOrPR(httpClient *http.Client, repo ghrepo.Interface, number int, f
|
|||
var resp response
|
||||
client := api.NewClientFromHTTP(httpClient)
|
||||
if err := client.GraphQL(repo.RepoHost(), query, variables, &resp); err != nil {
|
||||
var gerr *api.GraphQLErrorResponse
|
||||
if errors.As(err, &gerr) && gerr.Match("NOT_FOUND", "repository.issue") && !resp.Repository.HasIssuesEnabled {
|
||||
return nil, fmt.Errorf("the '%s' repository has disabled issues", ghrepo.FullName(repo))
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.Repository.Issue == nil {
|
||||
return nil, errors.New("issue was not found but GraphQL reported no error")
|
||||
}
|
||||
|
||||
return resp.Repository.Issue, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -79,9 +79,8 @@ func closeRun(opts *CloseOptions) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
apiClient := api.NewClientFromHTTP(httpClient)
|
||||
|
||||
err = api.PullRequestClose(apiClient, baseRepo, pr)
|
||||
err = api.PullRequestClose(httpClient, baseRepo, pr.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("API call failed: %w", err)
|
||||
}
|
||||
|
|
@ -90,6 +89,7 @@ func closeRun(opts *CloseOptions) error {
|
|||
|
||||
if opts.DeleteBranch {
|
||||
branchSwitchString := ""
|
||||
apiClient := api.NewClientFromHTTP(httpClient)
|
||||
|
||||
if opts.DeleteLocalBranch {
|
||||
currentBranch, err := opts.Branch()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue