Skip to content

Commit f99a149

Browse files
committed
Improve issue reopen re: overfetching, handling PRs
- `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.
1 parent 07cad38 commit f99a149

File tree

5 files changed

+52
-35
lines changed

5 files changed

+52
-35
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 IssueReopen(client *Client, repo ghrepo.Interface, issue Issue) error {
346-
var mutation struct {
347-
ReopenIssue struct {
348-
Issue struct {
349-
ID githubv4.ID
350-
}
351-
} `graphql:"reopenIssue(input: $input)"`
352-
}
353-
354-
variables := map[string]interface{}{
355-
"input": githubv4.ReopenIssueInput{
356-
IssueID: issue.ID,
357-
},
358-
}
359-
360-
gql := graphQLClient(client.http, repo.RepoHost())
361-
err := gql.MutateNamed(context.Background(), "IssueReopen", &mutation, variables)
362-
363-
return err
364-
}
365-
366345
func IssueDelete(client *Client, repo ghrepo.Interface, issue Issue) error {
367346
var mutation struct {
368347
DeleteIssue struct {

api/queries_pr.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ func PullRequestClose(httpClient *http.Client, repo ghrepo.Interface, prID strin
679679
return gql.MutateNamed(context.Background(), "PullRequestClose", &mutation, variables)
680680
}
681681

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

691691
variables := map[string]interface{}{
692692
"input": githubv4.ReopenPullRequestInput{
693-
PullRequestID: pr.ID,
693+
PullRequestID: prID,
694694
},
695695
}
696696

697-
gql := graphQLClient(client.http, repo.RepoHost())
698-
err := gql.MutateNamed(context.Background(), "PullRequestReopen", &mutation, variables)
699-
700-
return err
697+
gql := graphQLClient(httpClient, repo.RepoHost())
698+
return gql.MutateNamed(context.Background(), "PullRequestReopen", &mutation, variables)
701699
}
702700

703701
func PullRequestReady(client *Client, repo ghrepo.Interface, pr *PullRequest) error {

pkg/cmd/issue/reopen/reopen.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package reopen
22

33
import (
4+
"context"
45
"fmt"
56
"net/http"
67

78
"github.com/cli/cli/v2/api"
89
"github.com/cli/cli/v2/internal/config"
10+
"github.com/cli/cli/v2/internal/ghinstance"
911
"github.com/cli/cli/v2/internal/ghrepo"
1012
"github.com/cli/cli/v2/pkg/cmd/issue/shared"
1113
"github.com/cli/cli/v2/pkg/cmdutil"
1214
"github.com/cli/cli/v2/pkg/iostreams"
15+
graphql "github.com/cli/shurcooL-graphql"
16+
"github.com/shurcooL/githubv4"
1317
"github.com/spf13/cobra"
1418
)
1519

@@ -58,9 +62,8 @@ func reopenRun(opts *ReopenOptions) error {
5862
if err != nil {
5963
return err
6064
}
61-
apiClient := api.NewClientFromHTTP(httpClient)
6265

63-
issue, baseRepo, err := shared.IssueFromArg(apiClient, opts.BaseRepo, opts.SelectorArg)
66+
issue, baseRepo, err := shared.IssueFromArgWithFields(httpClient, opts.BaseRepo, opts.SelectorArg, []string{"id", "number", "title", "state"})
6467
if err != nil {
6568
return err
6669
}
@@ -70,7 +73,7 @@ func reopenRun(opts *ReopenOptions) error {
7073
return nil
7174
}
7275

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

8083
return nil
8184
}
85+
86+
func apiReopen(httpClient *http.Client, repo ghrepo.Interface, issue *api.Issue) error {
87+
if issue.IsPullRequest() {
88+
return api.PullRequestReopen(httpClient, repo, issue.ID)
89+
}
90+
91+
var mutation struct {
92+
ReopenIssue struct {
93+
Issue struct {
94+
ID githubv4.ID
95+
}
96+
} `graphql:"reopenIssue(input: $input)"`
97+
}
98+
99+
variables := map[string]interface{}{
100+
"input": githubv4.ReopenIssueInput{
101+
IssueID: issue.ID,
102+
},
103+
}
104+
105+
gql := graphql.NewClient(ghinstance.GraphQLEndpoint(repo.RepoHost()), httpClient)
106+
return gql.MutateNamed(context.Background(), "IssueReopen", &mutation, variables)
107+
}

pkg/cmd/issue/reopen/reopen_test.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,24 @@ func TestIssueReopen_issuesDisabled(t *testing.T) {
119119
http.Register(
120120
httpmock.GraphQL(`query IssueByNumber\b`),
121121
httpmock.StringResponse(`
122-
{ "data": { "repository": {
123-
"hasIssuesEnabled": false
124-
} } }`),
122+
{
123+
"data": {
124+
"repository": {
125+
"hasIssuesEnabled": false,
126+
"issue": null
127+
}
128+
},
129+
"errors": [
130+
{
131+
"type": "NOT_FOUND",
132+
"path": [
133+
"repository",
134+
"issue"
135+
],
136+
"message": "Could not resolve to an issue or pull request with the number of 2."
137+
}
138+
]
139+
}`),
125140
)
126141

127142
_, err := runCommand(http, true, "2")

pkg/cmd/pr/reopen/reopen.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,8 @@ func reopenRun(opts *ReopenOptions) error {
7373
if err != nil {
7474
return err
7575
}
76-
apiClient := api.NewClientFromHTTP(httpClient)
7776

78-
err = api.PullRequestReopen(apiClient, baseRepo, pr)
77+
err = api.PullRequestReopen(httpClient, baseRepo, pr.ID)
7978
if err != nil {
8079
return fmt.Errorf("API call failed: %w", err)
8180
}

0 commit comments

Comments
 (0)