-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcircle.go
373 lines (334 loc) · 10.1 KB
/
circle.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package circle
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/Shyp/go-types"
"github.com/kevinburke/rest"
"golang.org/x/sync/errgroup"
)
var client http.Client
// TODO switch all clients to use this
var v11client *rest.Client
func init() {
client = http.Client{
Timeout: 10 * time.Second,
}
v11client = rest.NewClient("", "", v11BaseUri)
}
const VERSION = "0.27"
const baseUri = "https://circleci.com/api/v1/project"
const v11BaseUri = "https://circleci.com/api/v1.1/project"
type TreeBuild struct {
BuildNum int `json:"build_num"`
BuildURL string `json:"build_url"`
CompareURL string `json:"compare"`
// Tree builds have a `previous_successful_build` field but as far as I can
// tell it is always null. Instead this field is set
Previous PreviousBuild `json:"previous"`
QueuedAt types.NullTime `json:"queued_at"`
RepoName string `json:"reponame"`
Status string `json:"status"`
StartTime types.NullTime `json:"start_time"`
StopTime types.NullTime `json:"stop_time"`
UsageQueuedAt types.NullTime `json:"usage_queued_at"`
Username string `json:"username"`
VCSRevision string `json:"vcs_revision"`
VCSType string `json:"vcs_type"`
}
func (tb TreeBuild) Passed() bool {
return tb.Status == "success" || tb.Status == "fixed"
}
func (tb TreeBuild) NotRunning() bool {
return tb.Status == "not_running" || tb.Status == "scheduled" || tb.Status == "queued"
}
func (tb TreeBuild) Running() bool {
return tb.Status == "running"
}
func (tb TreeBuild) Failed() bool {
return tb.Status == "failed" || tb.Status == "timedout" || tb.Status == "no_tests" || tb.Status == "infrastructure_fail"
}
type CircleArtifact struct {
Path string `json:"path"`
PrettyPath string `json:"pretty_path"`
NodeIndex uint8 `json:"node_index"`
Url string `json:"url"`
}
type CircleBuild struct {
BuildNum uint32 `json:"build_num"`
Parallel uint8 `json:"parallel"`
PreviousSuccessfulBuild PreviousBuild `json:"previous_successful_build"`
QueuedAt types.NullTime `json:"queued_at"`
RepoName string `json:"reponame"` // "go"
Steps []Step `json:"steps"`
VCSType string `json:"vcs_type"` // "github", "bitbucket"
UsageQueuedAt types.NullTime `json:"usage_queued_at"`
Username string `json:"username"` // "golang"
}
// Failures returns an array of (buildStep, containerID) integers identifying
// the IDs of container/build step pairs that failed.
func (cb *CircleBuild) Failures() [][2]int {
failures := make([][2]int, 0)
for i, step := range cb.Steps {
for j, action := range step.Actions {
if action.Failed() {
failures = append(failures, [...]int{i, j})
}
}
}
return failures
}
type CircleOutput struct {
Message string `json:"message"`
Time time.Time `json:"time"`
Type string `json:"type"`
}
type CircleOutputs []*CircleOutput
func (cb *CircleBuild) FailureTexts(ctx context.Context) ([]string, error) {
group, errctx := errgroup.WithContext(ctx)
// todo this is not great design
token, err := getToken(cb.Username)
if err != nil {
return nil, err
}
failures := cb.Failures()
results := make([]string, len(failures))
for i, failure := range failures {
failure := failure
i := i
group.Go(func() error {
// URL we are trying to fetch looks like:
// https://circleci.com/api/v1.1/project/github/Shyp/go-circle/11/output/9/0
uri := fmt.Sprintf("/%s/%s/%s/%d/output/%d/%d?circle-token=%s", cb.VCSType, cb.Username, cb.RepoName, cb.BuildNum, failure[0], failure[1], token)
req, err := v11client.NewRequest("GET", uri, nil)
if err != nil {
return err
}
req = req.WithContext(errctx)
var outputs []*CircleOutput
if err := v11client.Do(req, &outputs); err != nil {
return err
}
var message string
for i := range outputs {
message = message + outputs[i].Message + "\n"
}
results[i] = message
return nil
})
}
if err := group.Wait(); err != nil {
return nil, err
}
return results, nil
}
type PreviousBuild struct {
BuildNum int `json:"build_num"`
// would be neat to make this a time.Duration, easier to use the passed in
// value.
Status string `json:"status"`
BuildDurationMs int `json:"build_time_millis"`
}
type Step struct {
Name string `json:"name"`
Actions []Action `json:"actions"`
}
type Action struct {
Name string `json:"name"`
OutputURL URL `json:"output_url"`
Runtime CircleDuration `json:"run_time_millis"`
Status string `json:"status"`
}
func (a *Action) Failed() bool {
return a.Status == "failed" || a.Status == "timedout"
}
func getTreeUri(org string, project string, branch string, token string) string {
return fmt.Sprintf("/%s/%s/tree/%s?circle-token=%s", org, project, branch, token)
}
func getBuildUri(org string, project string, build int, token string) string {
return fmt.Sprintf("/%s/%s/%d?circle-token=%s", org, project, build, token)
}
func getCancelUri(org string, project string, build int, token string) string {
return fmt.Sprintf("%s/%s/%s/%d/cancel?circle-token=%s", baseUri, org, project, build, token)
}
func getArtifactsUri(org string, project string, build int, token string) string {
return fmt.Sprintf("%s/%s/%s/%d/artifacts?circle-token=%s", baseUri, org, project, build, token)
}
type CircleTreeResponse []TreeBuild
func makeRequest(method, uri string) (io.ReadCloser, error) {
req, err := http.NewRequest(method, uri, nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/json")
req.Header.Add("User-Agent", fmt.Sprintf("circle-command-line-client/%s", VERSION))
resp, err := client.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode >= 400 {
// TODO handle build not found, you get back {"message": "Build not found"}
return nil, fmt.Errorf("Request failed with status [%d]", resp.StatusCode)
}
return resp.Body, nil
}
type FollowResponse struct {
Following bool `json:"following"`
// TODO...
}
func Enable(ctx context.Context, host string, org string, repoName string) error {
token, err := getToken(org)
if err != nil {
return err
}
var vcs string
switch {
case strings.Contains(host, "github.com"):
vcs = "github"
case strings.Contains(host, "bitbucket.org"):
vcs = "bitbucket"
default:
return fmt.Errorf("can't enable unknown host %s", host)
}
uri := fmt.Sprintf("/%s/%s/%s/follow?circle-token=%s", vcs, org, repoName, token)
req, err := v11client.NewRequest("POST", uri, nil)
if err != nil {
return err
}
req = req.WithContext(ctx)
fr := new(FollowResponse)
if err := v11client.Do(req, fr); err != nil {
return err
}
if !fr.Following {
return errors.New("not following the project")
}
return nil
}
func Rebuild(ctx context.Context, tb *TreeBuild) error {
token, err := getToken(tb.Username)
if err != nil {
return err
}
// https://circleci.com/gh/segmentio/db-service/1488
// url we have is https://circleci.com/api/v1.1/project/github/segmentio/db-service/1486/retry
uri := fmt.Sprintf("/%s/%s/%s/%d/retry?circle-token=%s", tb.VCSType, tb.Username, tb.RepoName, tb.BuildNum, token)
req, err := v11client.NewRequest("POST", uri, strings.NewReader("null"))
if err != nil {
return err
}
req = req.WithContext(ctx)
return v11client.Do(req, nil)
}
func GetTree(org string, project string, branch string) (*CircleTreeResponse, error) {
return GetTreeContext(context.Background(), org, project, branch)
}
func GetTreeContext(ctx context.Context, org, project, branch string) (*CircleTreeResponse, error) {
token, err := getToken(org)
if err != nil {
return nil, err
}
uri := getTreeUri(org, project, branch, token)
client := rest.NewClient("", "", baseUri)
req, err := client.NewRequest("GET", uri, nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
cr := new(CircleTreeResponse)
if err := client.Do(req, cr); err != nil {
return nil, err
}
return cr, nil
}
func GetBuild(org string, project string, buildNum int) (*CircleBuild, error) {
token, err := getToken(org)
if err != nil {
return nil, err
}
uri := getBuildUri(org, project, buildNum, token)
client := rest.NewClient("", "", baseUri)
req, err := client.NewRequest("GET", uri, nil)
if err != nil {
return nil, err
}
cb := new(CircleBuild)
if err := client.Do(req, cb); err != nil {
return nil, err
}
return cb, nil
}
func GetArtifactsForBuild(org string, project string, buildNum int) ([]*CircleArtifact, error) {
token, err := getToken(org)
if err != nil {
return []*CircleArtifact{}, err
}
uri := getArtifactsUri(org, project, buildNum, token)
body, err := makeRequest("GET", uri)
if err != nil {
return []*CircleArtifact{}, err
}
defer body.Close()
var r io.Reader
if os.Getenv("CIRCLE_DEBUG") == "true" {
r = io.TeeReader(body, os.Stdout)
} else {
r = body
}
var arts []*CircleArtifact
if err = json.NewDecoder(r).Decode(&arts); err != nil {
return nil, err
}
return arts, nil
}
func DownloadArtifact(artifact *CircleArtifact, directory string, org string) error {
token, err := getToken(org)
if err != nil {
return err
}
fname := fmt.Sprintf("%d.%s", artifact.NodeIndex, path.Base(artifact.Url))
fmt.Fprintf(os.Stderr, "Downloading artifact to %s\n", fname)
f, err := os.Create(filepath.Join(directory, fname))
if err != nil {
return err
}
defer f.Close()
url := fmt.Sprintf("%s?circle-token=%s", artifact.Url, token)
body, err := makeRequest("GET", url)
if err != nil {
return err
}
defer body.Close()
_, copyErr := io.Copy(f, body)
return copyErr
}
func CancelBuild(org string, project string, buildNum int) (*CircleBuild, error) {
token, err := getToken(org)
if err != nil {
return nil, err
}
uri := getCancelUri(org, project, buildNum, token)
body, err := makeRequest("POST", uri)
if err != nil {
return nil, err
}
defer body.Close()
var r io.Reader
if os.Getenv("CIRCLE_DEBUG") == "true" {
r = io.TeeReader(body, os.Stdout)
} else {
r = body
}
d := json.NewDecoder(r)
var cb CircleBuild
err = d.Decode(&cb)
return &cb, err
}