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
|
|
@ -124,7 +124,18 @@ type graphQLResponse struct {
|
|||
type GraphQLError struct {
|
||||
Type string
|
||||
Message string
|
||||
// Path []interface // mixed strings and numbers
|
||||
Path []interface{} // mixed strings and numbers
|
||||
}
|
||||
|
||||
func (ge GraphQLError) PathString() string {
|
||||
var res strings.Builder
|
||||
for i, v := range ge.Path {
|
||||
if i > 0 {
|
||||
res.WriteRune('.')
|
||||
}
|
||||
fmt.Fprintf(&res, "%v", v)
|
||||
}
|
||||
return res.String()
|
||||
}
|
||||
|
||||
// GraphQLErrorResponse contains errors returned in a GraphQL response
|
||||
|
|
@ -140,6 +151,14 @@ func (gr GraphQLErrorResponse) Error() string {
|
|||
return fmt.Sprintf("GraphQL error: %s", strings.Join(errorMessages, "\n"))
|
||||
}
|
||||
|
||||
// Match checks if this error is only about a specific type on a specific path.
|
||||
func (gr GraphQLErrorResponse) Match(expectType, expectPath string) bool {
|
||||
if len(gr.Errors) != 1 {
|
||||
return false
|
||||
}
|
||||
return gr.Errors[0].Type == expectType && gr.Errors[0].PathString() == expectPath
|
||||
}
|
||||
|
||||
// HTTPError is an error returned by a failed API call
|
||||
type HTTPError struct {
|
||||
StatusCode int
|
||||
|
|
@ -221,7 +240,8 @@ func EndpointNeedsScopes(resp *http.Response, s string) *http.Response {
|
|||
return resp
|
||||
}
|
||||
|
||||
// GraphQL performs a GraphQL request and parses the response
|
||||
// GraphQL performs a GraphQL request and parses the response. If there are errors in the response,
|
||||
// *GraphQLErrorResponse will be returned, but the data will also be parsed into the receiver.
|
||||
func (c Client) GraphQL(hostname string, query string, variables map[string]interface{}, data interface{}) error {
|
||||
reqBody, err := json.Marshal(map[string]interface{}{"query": query, "variables": variables})
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -50,8 +50,16 @@ func TestGraphQLError(t *testing.T) {
|
|||
httpmock.GraphQL(""),
|
||||
httpmock.StringResponse(`
|
||||
{ "errors": [
|
||||
{"message":"OH NO"},
|
||||
{"message":"this is fine"}
|
||||
{
|
||||
"type": "NOT_FOUND",
|
||||
"message": "OH NO",
|
||||
"path": ["repository", "issue"]
|
||||
},
|
||||
{
|
||||
"type": "ACTUALLY_ITS_FINE",
|
||||
"message": "this is fine",
|
||||
"path": ["repository", "issues", 0, "comments"]
|
||||
}
|
||||
]
|
||||
}
|
||||
`),
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ type IssuesAndTotalCount struct {
|
|||
}
|
||||
|
||||
type Issue struct {
|
||||
Typename string `json:"__typename"`
|
||||
ID string
|
||||
Number int
|
||||
Title string
|
||||
|
|
@ -41,6 +42,10 @@ type Issue struct {
|
|||
ReactionGroups ReactionGroups
|
||||
}
|
||||
|
||||
func (i Issue) IsPullRequest() bool {
|
||||
return i.Typename == "PullRequest"
|
||||
}
|
||||
|
||||
type Assignees struct {
|
||||
Nodes []GitHubUser
|
||||
TotalCount int
|
||||
|
|
@ -337,31 +342,6 @@ func IssueByNumber(client *Client, repo ghrepo.Interface, number int) (*Issue, e
|
|||
return &resp.Repository.Issue, nil
|
||||
}
|
||||
|
||||
func IssueClose(client *Client, repo ghrepo.Interface, issue Issue) error {
|
||||
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 := graphQLClient(client.http, repo.RepoHost())
|
||||
err := gql.MutateNamed(context.Background(), "IssueClose", &mutation, variables)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func IssueReopen(client *Client, repo ghrepo.Interface, issue Issue) error {
|
||||
var mutation struct {
|
||||
ReopenIssue struct {
|
||||
|
|
|
|||
|
|
@ -660,7 +660,7 @@ func isBlank(v interface{}) bool {
|
|||
}
|
||||
}
|
||||
|
||||
func PullRequestClose(client *Client, repo ghrepo.Interface, pr *PullRequest) error {
|
||||
func PullRequestClose(httpClient *http.Client, repo ghrepo.Interface, prID string) error {
|
||||
var mutation struct {
|
||||
ClosePullRequest struct {
|
||||
PullRequest struct {
|
||||
|
|
@ -671,14 +671,12 @@ func PullRequestClose(client *Client, repo ghrepo.Interface, pr *PullRequest) er
|
|||
|
||||
variables := map[string]interface{}{
|
||||
"input": githubv4.ClosePullRequestInput{
|
||||
PullRequestID: pr.ID,
|
||||
PullRequestID: prID,
|
||||
},
|
||||
}
|
||||
|
||||
gql := graphQLClient(client.http, repo.RepoHost())
|
||||
err := gql.MutateNamed(context.Background(), "PullRequestClose", &mutation, variables)
|
||||
|
||||
return err
|
||||
gql := graphQLClient(httpClient, repo.RepoHost())
|
||||
return gql.MutateNamed(context.Background(), "PullRequestClose", &mutation, variables)
|
||||
}
|
||||
|
||||
func PullRequestReopen(client *Client, repo ghrepo.Interface, pr *PullRequest) error {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue