From bcf3eb75c85d97e9d5568d51e1bc7dfe317b0708 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Wed, 9 Oct 2019 10:23:47 -0700 Subject: [PATCH] Add debug output --- command/pr.go | 18 +++++++++++++++++- graphql/client.go | 41 ++++++++++++++++++++++++++++++----------- graphql/queries.go | 6 +++--- 3 files changed, 50 insertions(+), 15 deletions(-) diff --git a/command/pr.go b/command/pr.go index 78557c999..67cc58468 100644 --- a/command/pr.go +++ b/command/pr.go @@ -37,5 +37,21 @@ func ExecutePr() { if err != nil { panic(err) } - fmt.Printf("%+v!\n", prPayload) + + if prPayload.CurrentPR != nil { + fmt.Printf("Current Pr\n") + printPr(*prPayload.CurrentPR) + } + for _, pr := range prPayload.ViewerCreated { + fmt.Printf("Your Prs\n") + printPr(pr) + } + for _, pr := range prPayload.ReviewRequested { + fmt.Printf("Prs you need to review\n") + printPr(pr) + } +} + +func printPr(pr graphql.PullRequest) { + fmt.Printf("%d %s [%s]\n", pr.Number, pr.Title, pr.HeadRefName) } diff --git a/graphql/client.go b/graphql/client.go index 5c154a453..71ee8ce13 100644 --- a/graphql/client.go +++ b/graphql/client.go @@ -6,6 +6,7 @@ import ( "fmt" "io/ioutil" "net/http" + "os" "os/user" "regexp" ) @@ -53,6 +54,7 @@ func graphQL(query string, variables map[string]string, v interface{}) error { if err != nil { panic(err) } + debugRequest(req, reqBody) req.Header.Set("Authorization", "token "+getToken()) req.Header.Set("Content-Type", "application/json; charset=utf-8") @@ -64,14 +66,20 @@ func graphQL(query string, variables map[string]string, v interface{}) error { } defer resp.Body.Close() - return handleResponse(resp, v) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return err + } + debugResponse(resp, string(body)) + return handleResponse(resp, body, v) } -func handleResponse(resp *http.Response, 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.NewDecoder(resp.Body).Decode(gr) + err := json.Unmarshal(body, &gr) if err != nil { return err } @@ -85,20 +93,15 @@ func handleResponse(resp *http.Response, v interface{}) error { return nil } - return handleHTTPError(resp) + return handleHTTPError(resp, body) } -func handleHTTPError(resp *http.Response) error { - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return err - } - +func handleHTTPError(resp *http.Response, body []byte) error { var message string var parsedBody struct { Message string `json:"message"` } - err = json.Unmarshal(body, &parsedBody) + err := json.Unmarshal(body, &parsedBody) if err != nil { message = string(body) } else { @@ -126,3 +129,19 @@ func getToken() string { token := r.FindStringSubmatch(string(content)) return token[1] } + +func debugRequest(req *http.Request, body string) { + if _, ok := os.LookupEnv("DEBUG"); !ok { + return + } + + fmt.Printf("DEBUG: GraphQL query to %s:\n %s\n\n", req.URL, body) +} + +func debugResponse(resp *http.Response, body string) { + if _, ok := os.LookupEnv("DEBUG"); !ok { + return + } + + fmt.Printf("DEBUG: GraphQL response:\n%+v\n\n%s\n\n", resp, body) +} diff --git a/graphql/queries.go b/graphql/queries.go index dec455f71..a885e5c5a 100644 --- a/graphql/queries.go +++ b/graphql/queries.go @@ -9,9 +9,9 @@ import ( ) type PullRequestsPayload struct { - viewerCreated []PullRequest - reviewRequested []PullRequest - currentPR *PullRequest + ViewerCreated []PullRequest + ReviewRequested []PullRequest + CurrentPR *PullRequest } type PullRequest struct {