Skip to content

Commit

Permalink
Improve issue reopen re: overfetching, handling PRs
Browse files Browse the repository at this point in the history
- `issue reopen` no longer fetches all issue fields and thus avoids the
  problem when loading failed due to token not having access to projects

- `issue reopen` now accepts either issue or pull number as argument.
  • Loading branch information
mislav committed Nov 23, 2021
1 parent 07cad38 commit f99a149
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 35 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 IssueReopen(client *Client, repo ghrepo.Interface, issue Issue) error {
var mutation struct {
ReopenIssue struct {
Issue struct {
ID githubv4.ID
}
} `graphql:"reopenIssue(input: $input)"`
}

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

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

return err
}

func IssueDelete(client *Client, repo ghrepo.Interface, issue Issue) error {
var mutation struct {
DeleteIssue struct {
Expand Down
10 changes: 4 additions & 6 deletions api/queries_pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ func PullRequestClose(httpClient *http.Client, repo ghrepo.Interface, prID strin
return gql.MutateNamed(context.Background(), "PullRequestClose", &mutation, variables)
}

func PullRequestReopen(client *Client, repo ghrepo.Interface, pr *PullRequest) error {
func PullRequestReopen(httpClient *http.Client, repo ghrepo.Interface, prID string) error {
var mutation struct {
ReopenPullRequest struct {
PullRequest struct {
Expand All @@ -690,14 +690,12 @@ func PullRequestReopen(client *Client, repo ghrepo.Interface, pr *PullRequest) e

variables := map[string]interface{}{
"input": githubv4.ReopenPullRequestInput{
PullRequestID: pr.ID,
PullRequestID: prID,
},
}

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

return err
gql := graphQLClient(httpClient, repo.RepoHost())
return gql.MutateNamed(context.Background(), "PullRequestReopen", &mutation, variables)
}

func PullRequestReady(client *Client, repo ghrepo.Interface, pr *PullRequest) error {
Expand Down
32 changes: 29 additions & 3 deletions pkg/cmd/issue/reopen/reopen.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package reopen

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

Expand Down Expand Up @@ -58,9 +62,8 @@ func reopenRun(opts *ReopenOptions) 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
}
Expand All @@ -70,7 +73,7 @@ func reopenRun(opts *ReopenOptions) error {
return nil
}

err = api.IssueReopen(apiClient, baseRepo, *issue)
err = apiReopen(httpClient, baseRepo, issue)
if err != nil {
return err
}
Expand All @@ -79,3 +82,26 @@ func reopenRun(opts *ReopenOptions) error {

return nil
}

func apiReopen(httpClient *http.Client, repo ghrepo.Interface, issue *api.Issue) error {
if issue.IsPullRequest() {
return api.PullRequestReopen(httpClient, repo, issue.ID)
}

var mutation struct {
ReopenIssue struct {
Issue struct {
ID githubv4.ID
}
} `graphql:"reopenIssue(input: $input)"`
}

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

gql := graphql.NewClient(ghinstance.GraphQLEndpoint(repo.RepoHost()), httpClient)
return gql.MutateNamed(context.Background(), "IssueReopen", &mutation, variables)
}
21 changes: 18 additions & 3 deletions pkg/cmd/issue/reopen/reopen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,24 @@ func TestIssueReopen_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 2."
}
]
}`),
)

_, err := runCommand(http, true, "2")
Expand Down
3 changes: 1 addition & 2 deletions pkg/cmd/pr/reopen/reopen.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ func reopenRun(opts *ReopenOptions) error {
if err != nil {
return err
}
apiClient := api.NewClientFromHTTP(httpClient)

err = api.PullRequestReopen(apiClient, baseRepo, pr)
err = api.PullRequestReopen(httpClient, baseRepo, pr.ID)
if err != nil {
return fmt.Errorf("API call failed: %w", err)
}
Expand Down

0 comments on commit f99a149

Please sign in to comment.