From e05889d9f19e6c6386fbe27d91c03c2c211c1af0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Tue, 8 Oct 2019 13:25:49 +0200 Subject: [PATCH 1/5] Identify as "GitHub CLI" to API requests --- github/client.go | 4 ++-- github/http.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) 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 { From 2dc521d589ecc26a10de11ac21d353887dbbff06 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Fri, 11 Oct 2019 14:23:46 -0700 Subject: [PATCH 2/5] A non-successful response returns early --- api/client.go | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/api/client.go b/api/client.go index fc6895ca1..6a9644284 100644 --- a/api/client.go +++ b/api/client.go @@ -87,23 +87,25 @@ func graphQL(query string, variables map[string]string, v interface{}) error { func handleResponse(resp *http.Response, body []byte, v 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: 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 + } func handleHTTPError(resp *http.Response, body []byte) error { From 994e9eee1081e707d47713a6e2f3a58a3252c060 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Fri, 11 Oct 2019 14:28:26 -0700 Subject: [PATCH 3/5] Use RunE instead of panicking --- command/pr.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) 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() }, } From ed4f59fe306da5ff5184685fe504f6a39163c48b Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Fri, 11 Oct 2019 14:37:28 -0700 Subject: [PATCH 4/5] rename v to data --- api/client.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/api/client.go b/api/client.go index 6a9644284..0e45c31f1 100644 --- a/api/client.go +++ b/api/client.go @@ -45,7 +45,7 @@ if err != nil { fmt.Printf("%+v\n", resp) */ -func graphQL(query string, variables map[string]string, v interface{}) error { +func graphQL(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,17 +81,17 @@ func graphQL(query string, variables map[string]string, v interface{}) error { } 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 { return handleHTTPError(resp, body) } - gr := &graphQLResponse{Data: v} + gr := &graphQLResponse{Data: data} err := json.Unmarshal(body, &gr) if err != nil { return err From 90b0a6c55a62084fcf93426e6aef27e138bc09e4 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Fri, 11 Oct 2019 14:43:04 -0700 Subject: [PATCH 5/5] use spaces --- api/queries.go | 76 +++++++++++++++++++++++++------------------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/api/queries.go b/api/queries.go index aa26c0be0..a4c023375 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