forked from buildkite/cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmd_artifact_download.go
283 lines (243 loc) · 6.21 KB
/
cmd_artifact_download.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
package cli
import (
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"github.com/buildkite/cli/v2/graphql"
zglob "github.com/mattn/go-zglob"
)
type ArtifactDownloadCommandContext struct {
TerminalContext
ConfigContext
Debug bool
DebugHTTP bool
Build string
Job string
Pattern string
}
func ArtifactDownloadCommand(ctx ArtifactDownloadCommandContext) error {
bk, err := ctx.BuildkiteGraphQLClient()
if err != nil {
return NewExitError(err, 1)
}
if ctx.Job == "" && ctx.Build == "" {
return NewExitError(errors.New("--job or --build required"), 1)
}
try := ctx.Try()
try.Start("Downloading artifacts")
var artifacts []artifact
if ctx.Build != "" {
artifacts, err = findBuildkiteBuildArtifacts(bk, ctx.Build)
if err != nil {
try.Failure("Failed")
return NewExitError(err, 1)
}
} else if ctx.Job != "" {
artifacts, err = findBuildkiteJobArtifacts(bk, ctx.Job)
if err != nil {
try.Failure("Failed")
return NewExitError(err, 1)
}
}
if ctx.Pattern != "" {
glob, err := zglob.New(ctx.Pattern)
if err != nil {
try.Failure("Failed")
return NewExitError(err, 1)
}
var matchingArtifacts []artifact
for _, artifact := range artifacts {
if glob.Match(artifact.Path) {
matchingArtifacts = append(matchingArtifacts, artifact)
}
}
artifacts = matchingArtifacts
}
total := len(artifacts)
for _, artifact := range artifacts {
err := downloadArtifact(artifact)
if err != nil {
try.Failure("Failed")
return NewExitError(err, 1)
}
try.Println(fmt.Sprintf("%s (%d bytes)", artifact.Path, artifact.Size))
}
try.Success(fmt.Sprintf("Downloaded %d artifacts", total))
return nil
}
const artifactDownloadLimit = 500
type artifact struct {
ID string
Path string
Size int
DownloadURL string
}
func findBuildkiteJobArtifacts(client *graphql.Client, jobID string) ([]artifact, error) {
resp, err := client.Do(`
query($jobID: ID!, $limit: Int!) {
job(uuid: $jobID) {
...on JobTypeCommand {
artifacts(first: $limit) {
count
edges {
node {
id
path
size
downloadURL
}
}
}
}
}
}
`, map[string]interface{}{
"jobID": jobID,
"limit": artifactDownloadLimit,
})
if err != nil {
return []artifact{}, fmt.Errorf("Failed perform GraphQL query: %v", err)
}
var parsedResp struct {
Data struct {
Job struct {
Artifacts struct {
Count int `json:"count"`
Edges []struct {
Node struct {
ID string `json:"id"`
Path string `json:"path"`
Size int `json:"size"`
DownloadURL string `json:"downloadURL"`
} `json:"node"`
} `json:"edges"`
} `json:"artifacts"`
} `json:"job"`
} `json:"data"`
}
if err = resp.DecodeInto(&parsedResp); err != nil {
return []artifact{}, fmt.Errorf("Failed to parse GraphQL response: %v", err)
}
if parsedResp.Data.Job.Artifacts.Count > artifactDownloadLimit {
// GraphQL CommandJob.artifacts only supports `first` so cannot paginate
return []artifact{}, fmt.Errorf("Too many artifacts\n\nJob has %d artifacts but this tool can only download %d",
parsedResp.Data.Job.Artifacts.Count, artifactDownloadLimit)
}
var artifacts []artifact
for _, artifactEdge := range parsedResp.Data.Job.Artifacts.Edges {
artifacts = append(artifacts, artifact{
ID: artifactEdge.Node.ID,
Path: artifactEdge.Node.Path,
Size: artifactEdge.Node.Size,
DownloadURL: artifactEdge.Node.DownloadURL,
})
}
return artifacts, nil
}
func findBuildkiteBuildArtifacts(client *graphql.Client, buildID string) ([]artifact, error) {
resp, err := client.Do(`
query($buildID: ID!, $limit: Int!) {
build(uuid: $buildID) {
jobs(first: $limit) {
count
edges {
node {
...on JobTypeCommand {
artifacts(first: $limit) {
count
edges {
node {
id
path
size
downloadURL
}
}
}
}
}
}
}
}
}
`, map[string]interface{}{
"buildID": buildID,
"limit": artifactDownloadLimit,
})
if err != nil {
return []artifact{}, fmt.Errorf("Failed perform GraphQL query: %v", err)
}
var parsedResp struct {
Data struct {
Build struct {
Jobs struct {
Count int `json:"count"`
Edges []struct {
Node struct {
Artifacts struct {
Count int `json:"count"`
Edges []struct {
Node struct {
ID string `json:"id"`
Path string `json:"path"`
Size int `json:"size"`
DownloadURL string `json:"downloadURL"`
} `json:"node"`
} `json:"edges"`
} `json:"artifacts"`
} `json:"node"`
} `json:"edges"`
} `json:"jobs"`
} `json:"build"`
} `json:"data"`
}
if err = resp.DecodeInto(&parsedResp); err != nil {
return []artifact{}, fmt.Errorf("Failed to parse GraphQL response: %v", err)
}
if parsedResp.Data.Build.Jobs.Count > artifactDownloadLimit {
return []artifact{}, fmt.Errorf("Too many jobs\n\nBuild has %d jobs but this tool can only download %d",
parsedResp.Data.Build.Jobs.Count, artifactDownloadLimit)
}
var artifacts []artifact
for _, jobEdge := range parsedResp.Data.Build.Jobs.Edges {
if jobEdge.Node.Artifacts.Count > artifactDownloadLimit {
return []artifact{}, fmt.Errorf("Too many artifacts\n\nJob has %d artifacts but this tool can only download %d",
jobEdge.Node.Artifacts.Count, artifactDownloadLimit)
}
for _, artifactEdge := range jobEdge.Node.Artifacts.Edges {
artifacts = append(artifacts, artifact{
ID: artifactEdge.Node.ID,
Path: artifactEdge.Node.Path,
Size: artifactEdge.Node.Size,
DownloadURL: artifactEdge.Node.DownloadURL,
})
}
}
return artifacts, nil
}
func downloadArtifact(artifact artifact) error {
path := artifact.Path
dir := filepath.Dir(artifact.Path)
err := os.MkdirAll(dir, 0755)
if err != nil {
return err
}
resp, err := http.Get(artifact.DownloadURL)
if err != nil {
return err
}
defer resp.Body.Close()
out, err := os.Create(path)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}