-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathfix_github_review_error_response.go
More file actions
93 lines (76 loc) · 2.53 KB
/
fix_github_review_error_response.go
File metadata and controls
93 lines (76 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package github
// github returns errors in different format for review API
// this file "fixes" it by transforming response on transport level
// it allows handle such errors correctly in client code and log them
// issue in go-github: https://github.com/google/go-github/issues/540
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"github.com/google/go-github/github"
)
var reviewPathRegexp = regexp.MustCompile("/repos/.+/.+/pulls/[0-9]+/review")
// fixReviewTransport converts error response from review api endpoint
// to the format known by github library
type fixReviewTransport struct {
Transport http.RoundTripper
}
// reviewErrorResp defines structure of review error response
type reviewErrorResp struct {
Message string
Errors []string
DocumentationURL string
}
// ToGithubErrorResponse converts review errors response to github.ErrorResponse
func (r *reviewErrorResp) ToGithubErrorResponse() github.ErrorResponse {
errors := make([]github.Error, len(r.Errors))
for i, errorStr := range r.Errors {
code := "custom"
if errorStr == "User can only have one pending review per pull request" {
code = "already_exists"
}
errors[i] = github.Error{
Resource: "Review",
Code: code,
Message: errorStr,
}
}
return github.ErrorResponse{
Message: r.Message,
Errors: errors,
DocumentationURL: r.DocumentationURL,
}
}
// RoundTrip implemements http.RoundTripper interface
func (t *fixReviewTransport) RoundTrip(req *http.Request) (*http.Response, error) {
baseT := t.Transport
if baseT == nil {
baseT = http.DefaultTransport
}
resp, err := baseT.RoundTrip(req)
// match only errors for review path
if err == nil && resp.StatusCode == http.StatusUnprocessableEntity && reviewPathRegexp.MatchString(req.URL.Path) {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("can't read body of review error response: %s", err)
}
var errorResp reviewErrorResp
err = json.Unmarshal(b, &errorResp)
// if we aren't able to parse response - pass original one
// in case github fixed the wrong format or started returned something new
if err != nil || errorResp.Message == "" || len(errorResp.Errors) == 0 {
resp.Body = ioutil.NopCloser(bytes.NewBuffer(b))
return resp, nil
}
newB, err := json.Marshal(errorResp.ToGithubErrorResponse())
if err != nil {
// we marshal known struct so it should never happen in theory
return nil, err
}
resp.Body = ioutil.NopCloser(bytes.NewBuffer(newB))
}
return resp, err
}