Add debug output

This commit is contained in:
Corey Johnson 2019-10-09 10:23:47 -07:00
parent 212df1725d
commit bcf3eb75c8
3 changed files with 50 additions and 15 deletions

View file

@ -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)
}

View file

@ -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)
}

View file

@ -9,9 +9,9 @@ import (
)
type PullRequestsPayload struct {
viewerCreated []PullRequest
reviewRequested []PullRequest
currentPR *PullRequest
ViewerCreated []PullRequest
ReviewRequested []PullRequest
CurrentPR *PullRequest
}
type PullRequest struct {