-
Notifications
You must be signed in to change notification settings - Fork 5
/
cart.go
497 lines (444 loc) · 15.8 KB
/
cart.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
)
const (
// API v1.1 : <https://circleci.com/docs/api/v1-reference/>
// but beware that the summary is missing some method/URL pairs which are
// described further down in the page.
buildListURL = "https://circleci.com/api/v1.1/project/github/${project}/tree/${branch}?limit=${retrieve_count}&filter=successful&circle-token=${circle_token}"
artifactsURL = "https://circleci.com/api/v1.1/project/github/${project}/${build_num}/artifacts?circle-token=${circle_token}"
// We need to account for multiple workflows, and multiple builds within workflows
defaultRetrieveCount = 10
)
// censorURLfields caveat: keys in the query-map are case-sensitive
var censorURLfields = []string{"circle-token"}
type workflow struct {
JobName string `json:"job_name"`
JobID string `json:"job_id"`
WorkflowName string `json:"workflow_name"`
WorkflowID string `json:"workflow_id"`
}
type build struct {
BuildNum int `json:"build_num"`
Revision string `json:"vcs_revision"`
Workflows *workflow `json:"workflows"` // plural name but singleton struct
// We want to skip bad builds, and perhaps print the others so that if
// there's a mismatch from expectations, folks might notice.
Outcome string `json:"outcome"`
Subject string `json:"subject"`
StopTime string `json:"stop_time"`
}
type artifact struct {
URL string `json:"url"`
Path string `json:"path"`
NodeIndex int `json:"node_index"`
}
// FilterSet is the collection of attributes upon which we filter the results
// from Circle CI (or provide in URL to pre-filter).
type FilterSet struct {
branch string
workflow string
jobname string
anyFlowID bool
}
// Expander is used to take strings containing ${var} and interpolate them,
// so that we don't have URLs which have %s/%s/%s and cross-referencing across
// places to figure out which those fields are.
type Expander map[string]string
// Get is just a map lookup which panics, as a function for use with os.Expand
func (e Expander) Get(key string) string {
if val, ok := e[key]; ok {
return val
}
// There is no recovery, we don't want to pass a bad URL out, we're
// a client tool and we'll need to fix the hardcoded template strings.
panic("bad key " + key)
}
// Expand converts "${foo}/${bar}" into "football/goal".
// It also handles some $foo without parens, but we avoid using that.
func (e *Expander) Expand(src string) string {
return os.Expand(src, e.Get)
}
// ExpandURL does the same as Expand but call normalize() on the result,
// so that the output will be consistent whether censored or sent on the
// wire.
func (e *Expander) ExpandURL(src string) string {
return normalizeURL(os.Expand(src, e.Get))
}
var (
circleToken string
filter FilterSet
dryRun bool
verbosity int
)
func verbosenln(level int, items ...interface{}) {
if level > verbosity {
return
}
fmt.Println(items...)
}
func verbosenf(level int, spec string, args ...interface{}) {
if level > verbosity {
return
}
fmt.Printf(spec, args...)
}
func verbosef(spec string, args ...interface{}) { verbosenf(1, spec, args...) }
func verboseln(items ...interface{}) { verbosenln(1, items...) }
func main() {
var (
project string
buildNum int
outputPath string
retrieveBuildsCount int
flagVerbose bool
flagListArtifacts bool
)
log.SetFlags(log.Lshortfile)
log.SetOutput(os.Stderr)
flag.StringVar(&circleToken, "token", "", "CircleCI auth token")
flag.StringVar(&outputPath, "o", "", "output file `path`")
flag.BoolVar(&flagVerbose, "v", false, "verbose output (env $VERBOSITY=2|3|.. to see more)")
flag.BoolVar(&dryRun, "dry-run", false, "skip artifact download")
flag.BoolVar(&dryRun, "n", false, "(short for -dry-run)")
flag.BoolVar(&flagListArtifacts, "list-artifacts", false, "list artifacts")
flag.BoolVar(&flagListArtifacts, "l", false, "short for -list-artifacts")
flag.StringVar(&project, "repo", "", "github `username/repo`")
flag.IntVar(&buildNum, "build", 0, "get artifact for build number, ignoring branch")
flag.StringVar(&filter.branch, "branch", "master", "search builds for branch `name`")
// Workflows:
// If there are multiple workflows, then the latest "build" is perhaps unrelated to building,
// not even a later step in a workflow where an earlier step did build. Eg, we have
// stuff to automate dependencies checking, scheduled from cron.
// So to retrieve an artifact, we want to only consider specific workflow names.
// However, those are config items in `.circleci/config.yml` and we should avoid hardcoding
// such arbitrary choices across more than one repo, so our default for now is empty,
// thus not filtered.
//
// Within a workflow, the build might not be the last step in the flow; it usually won't be.
// Later steps might be "deploy", "stash image somewhere", etc.
// So we need to step back from the last step within a workflow until we find the specific
// step we're told.
//
// Eg, for one project, at this time, we use "commit_workflow" as the workflow to search for
// and "build" as the job within that workflow.
//
// By default, we want the build found for a workflow to be part of the
// same workflow invocation as the latest build seen for that workflow, so
// that we don't skip back to an older generation. If instead you just want
// "the latest build of that name, in any workflow matching this name",
// then use -ignore-later-workflows.
flag.StringVar(&filter.workflow, "workflow", "", "only consider builds which are part of this workflow")
flag.StringVar(&filter.workflow, "w", "", "(short for -workflow)")
flag.StringVar(&filter.jobname, "job", "", "look within workflow for artifacts from this build/step/job")
flag.StringVar(&filter.jobname, "j", "", "(short for -job)")
flag.IntVar(&retrieveBuildsCount, "search-depth", defaultRetrieveCount, "how far back to search in build history")
flag.BoolVar(&filter.anyFlowID, "ignore-later-workflows", false, "latest build of any matching workflow will do")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [flags] <artifact>\n\n", filepath.Base(os.Args[0]))
flag.PrintDefaults()
}
flag.Parse()
// TODO: should we support multiple downloads in one invocation?
if len(flag.Args()) > 1 {
flag.Usage()
log.Fatal("stray unparsed parameters left in command-line")
}
if flagVerbose {
verbosity = 1
if t := os.Getenv("VERBOSITY"); t != "" {
var err error
if verbosity, err = strconv.Atoi(t); err != nil {
log.Fatalf("parse $VERBOSITY %q: %s", t, err)
}
}
}
if project == "" {
out, err := exec.Command("git", "remote", "get-url", "origin").Output()
if err != nil {
log.Fatalf("exec git: %s", err)
}
project = gitProject(string(out))
}
artifactName := flag.Arg(0)
if circleToken == "" {
circleToken = os.Getenv("CIRCLE_TOKEN")
}
// for URL expansion with sane named parameters, and put in everything
// we might want too, including filters, in case there are better
// URLs we can switch to in future.
expansions := Expander{
"project": project,
"artifact": artifactName,
"retrieve_count": strconv.Itoa(retrieveBuildsCount),
"build_num": strconv.Itoa(buildNum),
"circle_token": circleToken,
"branch": filter.branch,
"workflow": filter.workflow,
"jobname": filter.jobname,
}
switch {
case project == "":
flag.Usage()
log.Fatal("no <username>/<project> provided")
case filter.branch == "":
flag.Usage()
log.Fatal("no <branch> provided")
case artifactName == "" && !flagListArtifacts:
flag.Usage()
log.Fatal("no <artifact> provided")
case circleToken == "":
// This one is common enough that showing usage obscures the actual issue,
// because ~everyone should be passing the value in through environ, so
// there's unlikely to be a problem with parameters, only with loading
// sensitive data into environ. So we skip flag.Usage()
log.Fatal("no auth token set: use $CIRCLE_TOKEN or flag -token (try -help)")
case retrieveBuildsCount < 1:
flag.Usage()
log.Fatal("workflow depth must be a positive (smallish) integer")
case buildNum > 0:
// Don't look for a green build.
fmt.Printf("Build: %d\n", buildNum)
default:
buildNum = circleFindBuild(expansions, filter)
expansions["build_num"] = strconv.Itoa(buildNum)
}
// Get artifact from buildNum
u := expansions.ExpandURL(artifactsURL)
verboseln("Artifact list:", censorURL(u))
req, err := http.NewRequest("GET", u, nil)
if err != nil {
log.Fatal(err)
}
req.Header.Set("Accept", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
var artifacts []artifact
if err := json.NewDecoder(res.Body).Decode(&artifacts); err != nil {
log.Fatal(err)
}
if flagListArtifacts {
for i := range artifacts {
fmt.Printf("[%d] node_index %d: path %q URL %q\n",
i, artifacts[i].NodeIndex, artifacts[i].Path, artifacts[i].URL)
}
}
if artifactName == "" {
return
}
if outputPath == "" {
outputPath = filepath.Base(artifactName)
}
n, err := downloadArtifact(artifacts, artifactName, outputPath)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Wrote %s (%d bytes) to %s\n", artifactName, n, outputPath)
}
func circleFindBuild(expansions Expander, filter FilterSet) (buildNum int) {
u := expansions.ExpandURL(buildListURL)
verboseln("Build list:", censorURL(u))
req, err := http.NewRequest("GET", u, nil)
if err != nil {
log.Fatal(err)
}
req.Header.Set("Accept", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
body := new(bytes.Buffer)
if _, err := io.Copy(body, res.Body); err != nil {
log.Fatal(err)
}
var builds []build
if err := json.Unmarshal(body.Bytes(), &builds); err != nil {
log.Fatalf("%s: %s", err, body.String())
}
if len(builds) == 0 {
log.Fatalf("no builds found for branch: %s", filter.branch)
}
// We _want_ to find the last successful workflow; as of APIv1.1 there's
// nothing to filter directly by workflow, nor to tell if a workflow has
// completed successfully, to know if we're grabbing something which later
// failed, etc.
//
// So we just look for the last green build within a workflow and rely upon
// the build we want being either that one, or earlier, with no prep steps
// pre-build. Unless the caller told us they don't care about matching
// workflow ID to the latest workflow for which we see any builds.
foundBuild := -1
onlyWorkflowID := ""
for i := 0; i < len(builds); i++ {
headOfWorkflow := false
if builds[i].Workflows == nil && (filter.workflow != "" || filter.jobname != "") {
verbosenf(2, "[%d][%d] SKIP, no workflow: %+v\n", i, builds[i].BuildNum, builds[i])
// -- these happen, they show in the UI, I wonder if it's a manual trigger?
continue
}
if builds[i].Outcome != "success" {
verbosenf(2, "[%d][%d] SKIP: build outcome is %q\n",
i, builds[i].BuildNum, builds[i].Outcome)
continue
}
if onlyWorkflowID != "" && builds[i].Workflows.WorkflowID != onlyWorkflowID {
verbosenf(3, "[%d][%d] SKIP: workflow-id %q, need latched workflow-id %q\n",
i, builds[i].BuildNum, builds[i].Workflows.WorkflowID, onlyWorkflowID)
continue
}
if filter.workflow != "" && builds[i].Workflows.WorkflowName != filter.workflow {
verbosenf(2, "[%d][%d] SKIP: workflow is %q, need %q\n",
i, builds[i].BuildNum, builds[i].Workflows.WorkflowName, filter.workflow)
continue
}
if onlyWorkflowID == "" && filter.workflow != "" && !filter.anyFlowID {
onlyWorkflowID = builds[i].Workflows.WorkflowID
verbosenf(2, "[%d][%d] Note: first match on workflow %q, workflow id is %q\n",
i, builds[i].BuildNum, filter.workflow, onlyWorkflowID)
headOfWorkflow = true
}
if filter.jobname != "" && builds[i].Workflows.JobName != filter.jobname {
if headOfWorkflow {
fmt.Printf("build: branch %q build %d is a %q, part of workflow %q, searching for build %q\n",
filter.branch, builds[i].BuildNum,
builds[i].Workflows.JobName, builds[i].Workflows.WorkflowName,
filter.jobname)
} else {
verbosenf(2, "[%d][%d] SKIP, has matching workflow %q, not yet right jobname (saw %q)\n",
i, builds[i].BuildNum, builds[i].Workflows.WorkflowName, builds[i].Workflows.JobName)
}
continue
}
if builds[i].Workflows == nil {
// must mean no filters, so i == 0
fmt.Printf("build: workflow-less on branch %q found a build at offset %d\n",
filter.branch, i)
} else {
fmt.Printf("build: workflow %q branch %q found build %q at offset %d\n",
builds[i].Workflows.WorkflowName, filter.branch, builds[i].Workflows.JobName, i)
}
foundBuild = i
break
}
if foundBuild < 0 {
labelFlow := filter.workflow
labelName := filter.jobname
if labelFlow == "" {
labelFlow = "*"
}
if labelName == "" {
labelName = "*"
}
log.Fatalf("build: failed to find a build matching workflow=%q jobname=%q in branch %q",
labelFlow, labelName, filter.branch)
}
verbosef("\nBuild Subject : %s\nBuild Finished : %s\n",
builds[foundBuild].Subject, builds[foundBuild].StopTime)
fmt.Printf("build: %d branch: %s rev: %s\n",
builds[foundBuild].BuildNum, filter.branch, builds[foundBuild].Revision[:8])
return builds[foundBuild].BuildNum
}
func downloadArtifact(artifacts []artifact, name, outputPath string) (int64, error) {
for _, a := range artifacts {
verboseln("Artifact URL:", a.URL)
if !strings.HasSuffix(a.URL, name) {
continue
}
u, err := url.Parse(a.URL)
if err != nil {
return 0, err
}
q := u.Query()
q.Add("circle-token", circleToken)
u.RawQuery = q.Encode()
verboseln("Artifact found:", name)
if dryRun {
fmt.Println("Dry run: skipped download")
os.Exit(0)
}
fmt.Printf("Downloading %s...\n", name)
res, err := http.Get(u.String())
if err != nil {
return 0, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return 0, fmt.Errorf("http: remote server responded %s (check http://status.circleci.com)", res.Status)
}
f, err := os.Create(outputPath)
if err != nil {
return 0, err
}
return io.Copy(f, res.Body)
}
return 0, fmt.Errorf("unable to find artifact: %s", name)
}
var ghURL = regexp.MustCompile(`github\.com(?:/|:)(\w+/\w+)`)
func gitProject(url string) string {
remote := ghURL.FindStringSubmatch(url)
if len(remote) > 1 {
return strings.Replace(remote[1], ".git", "", 1)
}
return ""
}
// We want to be able to censor a string for printing, to avoid showing
// credentials, to make it easier to copy/paste.
func censorURL(original string) string { return mutateURL(original, true) }
// After my first look at the output and seeing the options returned, I
// realized that they were being sorted and what we were logging was now
// sufficiently far enough from what we were sending that it would cause debug
// problems in future. So, we also have a normalize approach, to keep the
// two at least consistent.
func normalizeURL(original string) string { return mutateURL(original, false) }
func mutateURL(original string, mutate bool) string {
// We construct the URL from internal data, so any parse errors are coding
// bugs to be fixed. This applies to original URL parse and query-string
// parse below.
safe, err := url.Parse(original)
if err != nil {
panic(err)
}
if safe.User != nil {
if _, hasPassword := safe.User.Password(); hasPassword && mutate {
safe.User = url.UserPassword(safe.User.Username(), "censored")
}
}
if safe.RawQuery == "" {
return safe.String()
}
values, err := url.ParseQuery(safe.RawQuery)
if err != nil {
panic(err)
}
changed := false
for _, censor := range censorURLfields {
if v := values.Get(censor); v != "" {
if mutate {
values.Set(censor, "censored")
}
changed = true
}
}
if changed {
safe.RawQuery = values.Encode()
}
return safe.String()
}