-
Notifications
You must be signed in to change notification settings - Fork 295
/
artifacts.go
143 lines (113 loc) · 3.74 KB
/
artifacts.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
package api
import (
"fmt"
)
// ArtifactsService handles communication with the artifact related methods of
// the Buildkite Artifact API.
type ArtifactsService struct {
client *Client
}
// Artifact represents an artifact on the Buildkite Agent API
type Artifact struct {
// The ID of the artifact. The ID is assigned to it after a successful
// batch creation
ID string `json:"-"`
// The path to the artifact relative to the working directory
Path string `json:"path"`
// The absolute path to the artifact
AbsolutePath string `json:"absolute_path"`
// The glob path used to find this artifact
GlobPath string `json:"glob_path"`
// The size of the file in bytes
FileSize int64 `json:"file_size"`
// A Sha1Sum calculation of the file
Sha1Sum string `json:"sha1sum"`
// The HTTP url to this artifact once it's been uploaded
URL string `json:"url,omitempty"`
// The destination specified on the command line when this file was
// uploaded
UploadDestination string `json:"upload_destination,omitempty"`
// Information on how to upload this artifact.
UploadInstructions *ArtifactUploadInstructions `json:"-"`
}
type ArtifactBatch struct {
ID string `json:"id"`
Artifacts []*Artifact `json:"artifacts"`
UploadDestination string `json:"upload_destination"`
}
type ArtifactUploadInstructions struct {
Data map[string]string `json: "data"`
Action struct {
URL string `json:"url,omitempty"`
Method string `json:"method"`
Path string `json:"path"`
FileInput string `json:"file_input"`
}
}
type ArtifactBatchCreateResponse struct {
ID string `json:"id"`
ArtifactIDs []string `json:"artifact_ids"`
UploadInstructions *ArtifactUploadInstructions `json:"upload_instructions"`
}
// ArtifactSearchOptions specifies the optional parameters to the
// ArtifactsService.Search method.
type ArtifactSearchOptions struct {
Query string `url:"query,omitempty"`
Scope string `url:"scope,omitempty"`
}
type ArtifactBatchUpdateArtifact struct {
ID string `json:"id"`
State string `json:"state"`
}
type ArtifactBatchUpdateRequest struct {
Artifacts []*ArtifactBatchUpdateArtifact `json:"artifacts"`
}
// Accepts a slice of artifacts, and creates them on Buildkite as a batch.
func (as *ArtifactsService) Create(jobId string, batch *ArtifactBatch) (*ArtifactBatchCreateResponse, *Response, error) {
u := fmt.Sprintf("jobs/%s/artifacts", jobId)
req, err := as.client.NewRequest("POST", u, batch)
if err != nil {
return nil, nil, err
}
createResponse := new(ArtifactBatchCreateResponse)
resp, err := as.client.Do(req, createResponse)
if err != nil {
return nil, resp, err
}
return createResponse, resp, err
}
// Updates a paticular artifact
func (as *ArtifactsService) Update(jobId string, artifactStates map[string]string) (*Response, error) {
u := fmt.Sprintf("jobs/%s/artifacts", jobId)
payload := ArtifactBatchUpdateRequest{}
for id, state := range artifactStates {
payload.Artifacts = append(payload.Artifacts, &ArtifactBatchUpdateArtifact{id, state})
}
req, err := as.client.NewRequest("PUT", u, payload)
if err != nil {
return nil, err
}
resp, err := as.client.Do(req, nil)
if err != nil {
return resp, err
}
return resp, err
}
// Searches Buildkite for a set of artifacts
func (as *ArtifactsService) Search(buildId string, opt *ArtifactSearchOptions) ([]*Artifact, *Response, error) {
u := fmt.Sprintf("builds/%s/artifacts/search", buildId)
u, err := addOptions(u, opt)
if err != nil {
return nil, nil, err
}
req, err := as.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
a := []*Artifact{}
resp, err := as.client.Do(req, &a)
if err != nil {
return nil, resp, err
}
return a, resp, err
}