-
Notifications
You must be signed in to change notification settings - Fork 59
/
builds.go
311 lines (257 loc) · 9.5 KB
/
builds.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
package buildkite
import (
"context"
"fmt"
"net/url"
"time"
)
// BuildsService handles communication with the build related
// methods of the buildkite API.
//
// buildkite API docs: https://buildkite.com/docs/api/builds
type BuildsService struct {
client *Client
}
// Author of a commit (used in CreateBuild)
type Author struct {
Username string `json:"username,omitempty"`
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
}
// CreateBuild - Create a build.
type CreateBuild struct {
Commit string `json:"commit"`
Branch string `json:"branch"`
Message string `json:"message"`
// Optional fields
Author Author `json:"author,omitempty"`
CleanCheckout bool `json:"clean_checkout"`
Env map[string]string `json:"env,omitempty"`
MetaData map[string]string `json:"meta_data,omitempty"`
IgnorePipelineBranchFilters bool `json:"ignore_pipeline_branch_filters"`
PullRequestBaseBranch string `json:"pull_request_base_branch,omitempty"`
PullRequestID int64 `json:"pull_request_id,omitempty"`
PullRequestRepository string `json:"pull_request_repository,omitempty"`
}
// Creator represents who created a build
type Creator struct {
AvatarURL string `json:"avatar_url"`
CreatedAt *Timestamp `json:"created_at"`
Email string `json:"email"`
ID string `json:"id"`
Name string `json:"name"`
}
// RebuiltFrom references a previous build
type RebuiltFrom struct {
ID string `json:"id"`
Number int `json:"number"`
URL string `json:"url"`
}
// PullRequest represents a Github PR
type PullRequest struct {
ID string `json:"id,omitempty"`
Base string `json:"base,omitempty"`
Repository string `json:"repository,omitempty"`
}
type TriggeredFrom struct {
BuildID string `json:"build_id,omitempty"`
BuildNumber int `json:"build_number,omitempty"`
BuildPipelineSlug string `json:"build_pipeline_slug,omitempty"`
}
// Build represents a build which has run in buildkite
type Build struct {
ID string `json:"id,omitempty"`
GraphQLID string `json:"graphql_id,omitempty"`
URL string `json:"url,omitempty"`
WebURL string `json:"web_url,omitempty"`
Number int `json:"number,omitempty"`
State string `json:"state,omitempty"`
Blocked bool `json:"blocked"`
Message string `json:"message,omitempty"`
Commit string `json:"commit,omitempty"`
Branch string `json:"branch,omitempty"`
Author Author `json:"author,omitempty"`
Env map[string]interface{} `json:"env,omitempty"`
CreatedAt *Timestamp `json:"created_at,omitempty"`
ScheduledAt *Timestamp `json:"scheduled_at,omitempty"`
StartedAt *Timestamp `json:"started_at,omitempty"`
FinishedAt *Timestamp `json:"finished_at,omitempty"`
MetaData map[string]string `json:"meta_data,omitempty"`
Creator Creator `json:"creator,omitempty"`
Source string `json:"source,omitempty"`
// jobs run during the build
Jobs []Job `json:"jobs,omitempty"`
// the pipeline this build is associated with
Pipeline *Pipeline `json:"pipeline,omitempty"`
// the build this build is a rebuild of
RebuiltFrom *RebuiltFrom `json:"rebuilt_from,omitempty"`
// the pull request this build is associated with
PullRequest *PullRequest `json:"pull_request,omitempty"`
// the build that this build is triggered from
// https://buildkite.com/docs/pipelines/trigger-step
TriggeredFrom *TriggeredFrom `json:"triggered_from,omitempty"`
}
type MetaDataFilters struct {
MetaData map[string]string
}
// Encodes MetaData in the format expected by the buildkite API
// Example: ?meta_data[some-key]=some-value
func (mo MetaDataFilters) EncodeValues(parent_key string, v *url.Values) error {
for key, val := range mo.MetaData {
keyString := fmt.Sprintf("%s[%s]", parent_key, key)
v.Add(keyString, val)
}
return nil
}
// BuildsListOptions specifies the optional parameters to the
// BuildsService.List method.
type BuildsListOptions struct {
// Filters the results by the user who created the build
Creator string `url:"creator,omitempty"`
// Filters the results by builds created on or after the given time
CreatedFrom time.Time `url:"created_from,omitempty"`
// Filters the results by builds created before the given time
CreatedTo time.Time `url:"created_to,omitempty"`
// Filters the results by builds finished on or after the given time
FinishedFrom time.Time `url:"finished_from,omitempty"`
// State of builds to list. Possible values are: running, scheduled, passed,
// failed, canceled, skipped and not_run. Default is "".
State []string `url:"state,brackets,omitempty"`
// Filters the results by branch name(s)
Branch []string `url:"branch,brackets,omitempty"`
// Filters the results by builds for the specific commit SHA (full, not shortened). Default is "".
Commit string `url:"commit,omitempty"`
// Include all retried jobs in each build’s jobs list
IncludeRetriedJobs bool `url:"include_retried_jobs,omitempty"`
// Filters results by metadata.
MetaData MetaDataFilters `url:"meta_data,omitempty"`
ListOptions
}
// Cancel - Trigger a cancel for the target build
//
// buildkite API docs: https://buildkite.com/docs/apis/rest-api/builds#cancel-a-build
func (bs *BuildsService) Cancel(ctx context.Context, org, pipeline, build string) (Build, error) {
u := fmt.Sprintf("v2/organizations/%s/pipelines/%s/builds/%s/cancel", org, pipeline, build)
req, err := bs.client.NewRequest(ctx, "PUT", u, nil)
if err != nil {
return Build{}, err
}
var result Build
_, err = bs.client.Do(req, &result)
if err != nil {
return Build{}, err
}
return result, nil
}
// Create - Create a pipeline
//
// buildkite API docs: https://buildkite.com/docs/api/builds#create-a-build
func (bs *BuildsService) Create(ctx context.Context, org string, pipeline string, b CreateBuild) (Build, *Response, error) {
u := fmt.Sprintf("v2/organizations/%s/pipelines/%s/builds", org, pipeline)
req, err := bs.client.NewRequest(ctx, "POST", u, b)
if err != nil {
return Build{}, nil, err
}
var build Build
resp, err := bs.client.Do(req, &build)
if err != nil {
return Build{}, resp, err
}
return build, resp, err
}
// Get fetches a build.
//
// buildkite API docs: https://buildkite.com/docs/api/builds#get-a-build
func (bs *BuildsService) Get(ctx context.Context, org string, pipeline string, id string, opt *BuildsListOptions) (Build, *Response, error) {
u := fmt.Sprintf("v2/organizations/%s/pipelines/%s/builds/%s", org, pipeline, id)
u, err := addOptions(u, opt)
if err != nil {
return Build{}, nil, err
}
req, err := bs.client.NewRequest(ctx, "GET", u, nil)
if err != nil {
return Build{}, nil, err
}
var build Build
resp, err := bs.client.Do(req, &build)
if err != nil {
return Build{}, resp, err
}
return build, resp, err
}
// List the builds for the current user.
//
// buildkite API docs: https://buildkite.com/docs/api/builds#list-all-builds
func (bs *BuildsService) List(ctx context.Context, opt *BuildsListOptions) ([]Build, *Response, error) {
u := "v2/builds"
u, err := addOptions(u, opt)
if err != nil {
return nil, nil, err
}
req, err := bs.client.NewRequest(ctx, "GET", u, nil)
if err != nil {
return nil, nil, err
}
var builds []Build
resp, err := bs.client.Do(req, &builds)
if err != nil {
return nil, resp, err
}
return builds, resp, err
}
// ListByOrg lists the builds within the specified orginisation.
//
// buildkite API docs: https://buildkite.com/docs/api/builds#list-builds-for-an-organization
func (bs *BuildsService) ListByOrg(ctx context.Context, org string, opt *BuildsListOptions) ([]Build, *Response, error) {
u := fmt.Sprintf("v2/organizations/%s/builds", org)
u, err := addOptions(u, opt)
if err != nil {
return nil, nil, err
}
req, err := bs.client.NewRequest(ctx, "GET", u, nil)
if err != nil {
return nil, nil, err
}
var builds []Build
resp, err := bs.client.Do(req, &builds)
if err != nil {
return nil, resp, err
}
return builds, resp, err
}
// ListByPipeline lists the builds for a pipeline within the specified originisation.
//
// buildkite API docs: https://buildkite.com/docs/api/builds#list-builds-for-a-pipeline
func (bs *BuildsService) ListByPipeline(ctx context.Context, org string, pipeline string, opt *BuildsListOptions) ([]Build, *Response, error) {
u := fmt.Sprintf("v2/organizations/%s/pipelines/%s/builds", org, pipeline)
u, err := addOptions(u, opt)
if err != nil {
return nil, nil, err
}
req, err := bs.client.NewRequest(ctx, "GET", u, nil)
if err != nil {
return nil, nil, err
}
var builds []Build
resp, err := bs.client.Do(req, &builds)
if err != nil {
return nil, resp, err
}
return builds, resp, err
}
// Rebuild triggers a rebuild for the target build
//
// buildkite API docs: https://buildkite.com/docs/apis/rest-api/builds#rebuild-a-build
func (bs *BuildsService) Rebuild(ctx context.Context, org, pipeline, build string) (Build, error) {
u := fmt.Sprintf("v2/organizations/%s/pipelines/%s/builds/%s/rebuild", org, pipeline, build)
req, err := bs.client.NewRequest(ctx, "PUT", u, nil)
if err != nil {
return Build{}, err
}
var result Build
_, err = bs.client.Do(req, &result)
if err != nil {
return Build{}, err
}
return result, nil
}