-
Notifications
You must be signed in to change notification settings - Fork 75
/
uploader.go
148 lines (127 loc) · 3.13 KB
/
uploader.go
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package upload
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"github.com/Sirupsen/logrus"
"github.com/codeclimate/test-reporter/formatters"
"github.com/codeclimate/test-reporter/version"
"github.com/pkg/errors"
)
type Uploader struct {
ReporterID string
EndpointURL string
BatchSize int
Input io.Reader
}
func (u Uploader) Upload() error {
if u.ReporterID == "" {
return errors.New("you must supply a CC_TEST_REPORTER_ID ENV variable or pass it via the -r flag")
}
rep, err := formatters.NewReport()
if err != nil {
return errors.WithStack(err)
}
err = json.NewDecoder(u.Input).Decode(&rep)
if err != nil {
return errors.WithStack(err)
}
testReport := NewTestReport(rep)
pr, pw := io.Pipe()
go func() {
defer pw.Close()
bb := &bytes.Buffer{}
w := io.MultiWriter(pw, bb)
err := json.NewEncoder(w).Encode(JSONWraper{Data: testReport})
if err != nil {
logrus.Error(err)
return
}
logrus.Debug(bb.String())
}()
res, err := u.doRequest(pr, u.EndpointURL)
if err != nil {
return errors.WithStack(err)
}
batchLinks := struct {
Links struct {
PostBatch string `json:"post_batch"`
} `json:"links"`
}{}
err = json.NewDecoder(res.Body).Decode(&batchLinks)
if err != nil {
return errors.WithStack(err)
}
return u.SendBatches(testReport, batchLinks.Links.PostBatch)
}
func (u Uploader) SendBatches(rep *TestReport, url string) error {
batch := [][]SourceFile{}
pos := 0
count := len(rep.SourceFiles) / u.BatchSize
remainder := len(rep.SourceFiles) % u.BatchSize
for i := 0; i < count; i++ {
end := pos + u.BatchSize
batch = append(batch, rep.SourceFiles[pos:end])
pos = end
}
if remainder > 0 {
batch = append(batch, rep.SourceFiles[pos:])
}
for i, b := range batch {
pr, pw := io.Pipe()
go func() {
defer pw.Close()
bb := &bytes.Buffer{}
w := io.MultiWriter(pw, bb)
err := json.NewEncoder(w).Encode(JSONWraper{
Data: b,
Meta: map[string]int{
"current": i + 1,
"total": len(batch),
},
})
if err != nil {
logrus.Error(err)
return
}
logrus.Debug(bb.String())
}()
_, err := u.doRequest(pr, url)
if err != nil {
return errors.WithStack(err)
}
}
return nil
}
func (u Uploader) doRequest(in io.Reader, url string) (*http.Response, error) {
c := http.Client{
Timeout: 30 * time.Second,
}
req, err := u.newRequest(in, url)
if err != nil {
return nil, errors.WithStack(err)
}
logrus.Debugf("posting request to %s", url)
res, err := c.Do(req)
if err != nil {
return res, errors.WithStack(err)
}
if res.StatusCode < 200 || res.StatusCode >= 300 {
return res, fmt.Errorf("response from %s was %d", url, res.StatusCode)
}
return res, nil
}
func (u Uploader) newRequest(in io.Reader, url string) (*http.Request, error) {
req, err := http.NewRequest("POST", url, in)
if err != nil {
return nil, errors.WithStack(err)
}
req.Header.Set("User-Agent", fmt.Sprintf("TestReporter/%s (Code Climate, Inc.)", version.Version))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-CC-Test-Reporter-Id", u.ReporterID)
req.Header.Set("Accept", "application/vnd.api+json")
return req, err
}