-
Notifications
You must be signed in to change notification settings - Fork 59
/
pipelines.go
282 lines (244 loc) · 10.9 KB
/
pipelines.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
package buildkite
import (
"context"
"encoding/json"
"fmt"
)
// PipelinesService handles communication with the pipeline related
// methods of the buildkite API.
//
// buildkite API docs: https://buildkite.com/docs/api/pipelines
type PipelinesService struct {
client *Client
}
// CreatePipeline - Create a Pipeline.
type CreatePipeline struct {
Name string `json:"name"`
Repository string `json:"repository"`
// Either configuration needs to be specified as a yaml string or steps.
Configuration string `json:"configuration,omitempty"`
Steps []Step `json:"steps,omitempty"`
// Optional fields
DefaultBranch string `json:"default_branch,omitempty"`
Description string `json:"description,omitempty"`
Env map[string]string `json:"env,omitempty"`
ProviderSettings ProviderSettings `json:"provider_settings,omitempty"`
BranchConfiguration string `json:"branch_configuration,omitempty"`
SkipQueuedBranchBuilds bool `json:"skip_queued_branch_builds"`
SkipQueuedBranchBuildsFilter string `json:"skip_queued_branch_builds_filter,omitempty"`
CancelRunningBranchBuilds bool `json:"cancel_running_branch_builds"`
CancelRunningBranchBuildsFilter string `json:"cancel_running_branch_builds_filter,omitempty"`
TeamUuids []string `json:"team_uuids,omitempty"`
ClusterID string `json:"cluster_id,omitempty"`
Visibility string `json:"visibility,omitempty"`
Tags []string `json:"tags,omitempty"`
}
type UpdatePipeline struct {
// Either configuration needs to be specified as a yaml string or steps (based on what the pipeline uses)
Configuration string `json:"configuration,omitempty"`
Steps []Step `json:"steps,omitempty"`
Name string `json:"name,omitempty"`
Repository string `json:"repository,omitempty"`
DefaultBranch string `json:"default_branch,omitempty"`
Description string `json:"description,omitempty"`
ProviderSettings ProviderSettings `json:"provider_settings,omitempty"`
BranchConfiguration string `json:"branch_configuration,omitempty"`
SkipQueuedBranchBuilds bool `json:"skip_queued_branch_builds"`
SkipQueuedBranchBuildsFilter string `json:"skip_queued_branch_builds_filter,omitempty"`
CancelRunningBranchBuilds bool `json:"cancel_running_branch_builds"`
CancelRunningBranchBuildsFilter string `json:"cancel_running_branch_builds_filter,omitempty"`
ClusterID string `json:"cluster_id,omitempty"`
Visibility string `json:"visibility,omitempty"`
Tags []string `json:"tags,omitempty"`
}
// Pipeline represents a buildkite pipeline.
type Pipeline struct {
ID string `json:"id,omitempty"`
GraphQLID string `json:"graphql_id,omitempty"`
URL string `json:"url,omitempty"`
WebURL string `json:"web_url,omitempty"`
Name string `json:"name,omitempty"`
Slug string `json:"slug,omitempty"`
Repository string `json:"repository,omitempty"`
BuildsURL string `json:"builds_url,omitempty"`
BadgeURL string `json:"badge_url,omitempty"`
CreatedAt *Timestamp `json:"created_at,omitempty"`
ArchivedAt *Timestamp `json:"archived_at,omitempty"`
DefaultBranch string `json:"default_branch,omitempty"`
Description string `json:"description,omitempty"`
BranchConfiguration string `json:"branch_configuration,omitempty"`
SkipQueuedBranchBuilds bool `json:"skip_queued_branch_builds"`
SkipQueuedBranchBuildsFilter string `json:"skip_queued_branch_builds_filter,omitempty"`
CancelRunningBranchBuilds bool `json:"cancel_running_branch_builds"`
CancelRunningBranchBuildsFilter string `json:"cancel_running_branch_builds_filter,omitempty"`
ClusterID string `json:"cluster_id,omitempty"`
Visibility string `json:"visibility,omitempty"`
Tags []string `json:"tags,omitempty"`
ScheduledBuildsCount int `json:"scheduled_builds_count,omitempty"`
RunningBuildsCount int `json:"running_builds_count,omitempty"`
ScheduledJobsCount int `json:"scheduled_jobs_count,omitempty"`
RunningJobsCount int `json:"running_jobs_count,omitempty"`
WaitingJobsCount int `json:"waiting_jobs_count,omitempty"`
// the provider of sources
Provider Provider `json:"provider,omitempty"`
// build steps
Steps []Step `json:"steps,omitempty"`
Configuration string `json:"configuration,omitempty"`
Env map[string]any `json:"env,omitempty"`
}
// Step represents a build step in buildkites build pipeline
type Step struct {
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
Label string `json:"label,omitempty"`
Command string `json:"command,omitempty"`
ArtifactPaths string `json:"artifact_paths,omitempty"`
BranchConfiguration string `json:"branch_configuration,omitempty"`
Env map[string]string `json:"env,omitempty"`
TimeoutInMinutes *int `json:"timeout_in_minutes,omitempty"`
AgentQueryRules []string `json:"agent_query_rules,omitempty"`
Plugins Plugins `json:"plugins,omitempty"`
}
type Plugins map[string]Plugin
// Support deprecated map structure as well as array structure
func (p *Plugins) UnmarshalJSON(bs []byte) error {
type plugins2 Plugins // avoid unmarshal recursion
err := json.Unmarshal(bs, (*plugins2)(p))
if err == nil {
return nil
}
asArray := []map[string]Plugin{}
if err2 := json.Unmarshal(bs, &asArray); err2 != nil {
return fmt.Errorf("plugins are neither a map or an array: %s, %s", err.Error(), err2.Error())
}
for _, plugin := range asArray {
if len(plugin) != 1 {
return fmt.Errorf("plugins as arrays must have a single key")
}
if *p == nil {
*p = map[string]Plugin{}
}
for k, v := range plugin {
(*p)[k] = v
}
}
return nil
}
// This is kept vague (map of string to whatever) as there are a lot of custom
// plugins out there.
type Plugin map[string]interface{}
// PipelineListOptions specifies the optional parameters to the
// PipelinesService.List method.
type PipelineListOptions struct{ ListOptions }
// Create - Creates a pipeline for a given organisation.
//
// buildkite API docs: https://buildkite.com/docs/rest-api/pipelines#create-a-pipeline
func (ps *PipelinesService) Create(ctx context.Context, org string, p CreatePipeline) (Pipeline, *Response, error) {
u := fmt.Sprintf("v2/organizations/%s/pipelines", org)
req, err := ps.client.NewRequest(ctx, "POST", u, p)
if err != nil {
return Pipeline{}, nil, err
}
var pipeline Pipeline
resp, err := ps.client.Do(req, &pipeline)
if err != nil {
return Pipeline{}, resp, err
}
return pipeline, resp, err
}
// Get fetches a pipeline.
//
// buildkite API docs: https://buildkite.com/docs/rest-api/pipelines#get-a-pipeline
func (ps *PipelinesService) Get(ctx context.Context, org string, slug string) (Pipeline, *Response, error) {
u := fmt.Sprintf("v2/organizations/%s/pipelines/%s", org, slug)
req, err := ps.client.NewRequest(ctx, "GET", u, nil)
if err != nil {
return Pipeline{}, nil, err
}
var pipeline Pipeline
resp, err := ps.client.Do(req, &pipeline)
if err != nil {
return Pipeline{}, resp, err
}
return pipeline, resp, err
}
// List the pipelines for a given organisation.
//
// buildkite API docs: https://buildkite.com/docs/api/pipelines#list-pipelines
func (ps *PipelinesService) List(ctx context.Context, org string, opt *PipelineListOptions) ([]Pipeline, *Response, error) {
u := fmt.Sprintf("v2/organizations/%s/pipelines", org)
u, err := addOptions(u, opt)
if err != nil {
return nil, nil, err
}
req, err := ps.client.NewRequest(ctx, "GET", u, nil)
if err != nil {
return nil, nil, err
}
var pipelines []Pipeline
resp, err := ps.client.Do(req, &pipelines)
if err != nil {
return nil, resp, err
}
return pipelines, resp, err
}
// Delete a pipeline.
//
// buildkite API docs: https://buildkite.com/docs/rest-api/pipelines#delete-a-pipeline
func (ps *PipelinesService) Delete(ctx context.Context, org, slug string) (*Response, error) {
u := fmt.Sprintf("v2/organizations/%s/pipelines/%s", org, slug)
req, err := ps.client.NewRequest(ctx, "DELETE", u, nil)
if err != nil {
return nil, err
}
return ps.client.Do(req, nil)
}
// Update - Updates a pipeline.
//
// buildkite API docs: https://buildkite.com/docs/rest-api/pipelines#update-a-pipeline
func (ps *PipelinesService) Update(ctx context.Context, org, slug string, up UpdatePipeline) (Pipeline, *Response, error) {
u := fmt.Sprintf("v2/organizations/%s/pipelines/%s", org, slug)
req, err := ps.client.NewRequest(ctx, "PATCH", u, up)
if err != nil {
return Pipeline{}, nil, err
}
var p Pipeline
resp, err := ps.client.Do(req, &p)
if err != nil {
return Pipeline{}, resp, err
}
return p, resp, err
}
// AddWebhook - Adds webhook in github for pipeline.
//
// buildkite API docs: https://buildkite.com/docs/apis/rest-api/pipelines#add-a-webhook
func (ps *PipelinesService) AddWebhook(ctx context.Context, org string, slug string) (*Response, error) {
u := fmt.Sprintf("v2/organizations/%s/pipelines/%s/webhook", org, slug)
req, err := ps.client.NewRequest(ctx, "POST", u, nil)
if err != nil {
return nil, err
}
return ps.client.Do(req, nil)
}
// Archive - Archives a pipeline.
//
// buildkite API docs: https://buildkite.com/docs/apis/rest-api/pipelines#archive-a-pipeline
func (ps *PipelinesService) Archive(ctx context.Context, org string, slug string) (*Response, error) {
u := fmt.Sprintf("v2/organizations/%s/pipelines/%s/archive", org, slug)
req, err := ps.client.NewRequest(ctx, "POST", u, nil)
if err != nil {
return nil, err
}
return ps.client.Do(req, nil)
}
// Unarchive - Unarchive a pipeline.
//
// buildkite API docs: https://buildkite.com/docs/apis/rest-api/pipelines#unarchive-a-pipeline
func (ps *PipelinesService) Unarchive(ctx context.Context, org string, slug string) (*Response, error) {
u := fmt.Sprintf("v2/organizations/%s/pipelines/%s/unarchive", org, slug)
req, err := ps.client.NewRequest(ctx, "POST", u, nil)
if err != nil {
return nil, err
}
return ps.client.Do(req, nil)
}