diff --git a/api/client.go b/api/client.go index fa96dbf0b..1dcf7c0ce 100644 --- a/api/client.go +++ b/api/client.go @@ -45,7 +45,7 @@ if err != nil { fmt.Printf("%+v\n", resp) */ -var GraphQL = func(query string, variables map[string]string, v interface{}) error { +var GraphQL = func(query string, variables map[string]string, data interface{}) error { url := "https://api.github.com/graphql" reqBody, err := json.Marshal(map[string]interface{}{"query": query, "variables": variables}) if err != nil { @@ -81,29 +81,31 @@ var GraphQL = func(query string, variables map[string]string, v interface{}) err } debugResponse(resp, string(body)) - return handleResponse(resp, body, v) + return handleResponse(resp, body, data) } -func handleResponse(resp *http.Response, body []byte, v interface{}) error { +func handleResponse(resp *http.Response, body []byte, data interface{}) error { success := resp.StatusCode >= 200 && resp.StatusCode < 300 - if success { - gr := &graphQLResponse{Data: v} - err := json.Unmarshal(body, &gr) - if err != nil { - return err - } - if len(gr.Errors) > 0 { - errorMessages := gr.Errors[0].Message - for _, e := range gr.Errors[1:] { - errorMessages += ", " + e.Message - } - return fmt.Errorf("graphql error: '%s'", errorMessages) - } - return nil + if !success { + return handleHTTPError(resp, body) } - return handleHTTPError(resp, body) + gr := &graphQLResponse{Data: data} + err := json.Unmarshal(body, &gr) + if err != nil { + return err + } + + if len(gr.Errors) > 0 { + errorMessages := gr.Errors[0].Message + for _, e := range gr.Errors[1:] { + errorMessages += ", " + e.Message + } + return fmt.Errorf("graphql error: '%s'", errorMessages) + } + return nil + } func handleHTTPError(resp *http.Response, body []byte) error { diff --git a/api/queries.go b/api/queries.go index 616be409d..cc7a5e512 100644 --- a/api/queries.go +++ b/api/queries.go @@ -41,45 +41,45 @@ func PullRequests() (PullRequestsPayload, error) { } query := ` - fragment pr on PullRequest { - number - title - url - headRefName - } + fragment pr on PullRequest { + number + title + url + headRefName + } - query($owner: String!, $repo: String!, $headRefName: String!, $viewerQuery: String!, $reviewerQuery: String!, $per_page: Int = 10) { - repository(owner: $owner, name: $repo) { - pullRequests(headRefName: $headRefName, first: 1) { - edges { - node { - ...pr - } - } - } - } - viewerCreated: search(query: $viewerQuery, type: ISSUE, first: $per_page) { - edges { - node { - ...pr - } - } - pageInfo { - hasNextPage - } - } - reviewRequested: search(query: $reviewerQuery, type: ISSUE, first: $per_page) { - edges { - node { - ...pr - } - } - pageInfo { - hasNextPage - } - } - } - ` + query($owner: String!, $repo: String!, $headRefName: String!, $viewerQuery: String!, $reviewerQuery: String!, $per_page: Int = 10) { + repository(owner: $owner, name: $repo) { + pullRequests(headRefName: $headRefName, first: 1) { + edges { + node { + ...pr + } + } + } + } + viewerCreated: search(query: $viewerQuery, type: ISSUE, first: $per_page) { + edges { + node { + ...pr + } + } + pageInfo { + hasNextPage + } + } + reviewRequested: search(query: $reviewerQuery, type: ISSUE, first: $per_page) { + edges { + node { + ...pr + } + } + pageInfo { + hasNextPage + } + } + } + ` project := project() owner := project.Owner diff --git a/command/pr.go b/command/pr.go index d1caadeae..3dc8821c8 100644 --- a/command/pr.go +++ b/command/pr.go @@ -26,11 +26,8 @@ work with pull requests.`, var prListCmd = &cobra.Command{ Use: "list", Short: "List pull requests", - Run: func(cmd *cobra.Command, args []string) { - err := ExecutePr() - if err != nil { - panic(err) // In the future this should handle the error better, but for now panic seems like a valid reaction - } + RunE: func(cmd *cobra.Command, args []string) error { + return ExecutePr() }, } diff --git a/github/client.go b/github/client.go index 07339fb92..e1855e15c 100644 --- a/github/client.go +++ b/github/client.go @@ -17,10 +17,10 @@ import ( const ( GitHubHost string = "github.com" - OAuthAppURL string = "https://hub.github.com/" + OAuthAppURL string = "https://github.com/github/gh-cli" ) -var UserAgent = "Hub " + version.Version +var userAgent = "GitHub CLI " + version.Version func NewClient(h string) *Client { return NewClientWithHost(&Host{Host: h}) diff --git a/github/http.go b/github/http.go index f0dfd6e49..25abcb351 100644 --- a/github/http.go +++ b/github/http.go @@ -221,7 +221,7 @@ func (c *simpleClient) performRequestUrl(method string, url *url.URL, body io.Re if c.PrepareRequest != nil { c.PrepareRequest(req) } - req.Header.Set("User-Agent", UserAgent) + req.Header.Set("User-Agent", userAgent) req.Header.Set("Accept", apiPayloadVersion) if configure != nil {