-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathpipeline.go
321 lines (267 loc) · 9.84 KB
/
pipeline.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
package cfclient
import (
"errors"
"fmt"
"strings"
)
type Pipelines struct {
Docs []Pipeline `json:"docs,omitempty"`
Count int `json:"count,omitempty"`
}
type ErrorResponse struct {
Status int `json:"status,omitempty"`
Message string `json:"message,omitempty"`
Error string `json:"error,omitempty"`
}
type Labels struct {
Tags []string `json:"tags,omitempty"`
}
type Metadata struct {
Name string `json:"name,omitempty"`
ID string `json:"id,omitempty"`
IsPublic bool `json:"isPublic,omitempty"`
Labels Labels `json:"labels,omitempty"`
OriginalYamlString string `json:"originalYamlString,omitempty"`
Project string `json:"project,omitempty"`
ProjectId string `json:"projectId,omitempty"`
Revision int `json:"revision,omitempty"`
}
type SpecTemplate struct {
Location string `json:"location,omitempty"`
Repo string `json:"repo,omitempty"`
Path string `json:"path,omitempty"`
Revision string `json:"revision,omitempty"`
Context string `json:"context,omitempty"`
}
type Trigger struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Type string `json:"type,omitempty"`
Repo string `json:"repo,omitempty"`
Events []string `json:"events,omitempty"`
BranchRegex string `json:"branchRegex,omitempty"`
BranchRegexInput string `json:"branchRegexInput,omitempty"`
PullRequestTargetBranchRegex string `json:"pullRequestTargetBranchRegex,omitempty"`
CommentRegex string `json:"commentRegex,omitempty"`
ModifiedFilesGlob string `json:"modifiedFilesGlob,omitempty"`
Provider string `json:"provider,omitempty"`
Disabled bool `json:"disabled,omitempty"`
Options *TriggerOptions `json:"options,omitempty"`
PullRequestAllowForkEvents bool `json:"pullRequestAllowForkEvents,omitempty"`
CommitStatusTitle string `json:"commitStatusTitle,omitempty"`
Context string `json:"context,omitempty"`
Contexts []string `json:"contexts,omitempty"`
RuntimeEnvironment *RuntimeEnvironment `json:"runtimeEnvironment,omitempty"`
Variables []Variable `json:"variables,omitempty"`
}
type CronTrigger struct {
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Expression string `json:"expression,omitempty"`
Message string `json:"message,omitempty"`
GitTriggerId string `json:"gitTriggerId,omitempty"`
Branch string `json:"branch,omitempty"`
Disabled bool `json:"disabled,omitempty"`
Options *TriggerOptions `json:"options,omitempty"`
RuntimeEnvironment *RuntimeEnvironment `json:"runtimeEnvironment,omitempty"`
Variables []Variable `json:"variables,omitempty"`
}
type TriggerOptions struct {
NoCache bool `json:"noCache,omitempty"`
NoCfCache bool `json:"noCfCache,omitempty"`
ResetVolume bool `json:"resetVolume,omitempty"`
EnableNotifications bool `json:"enableNotifications,omitempty"`
}
type RuntimeEnvironment struct {
Name string `json:"name,omitempty"`
Memory string `json:"memory,omitempty"`
CPU string `json:"cpu,omitempty"`
DindStorage string `json:"dindStorage,omitempty"`
RequiredAvailableStorage string `json:"requiredAvailableStorage,omitempty"`
}
type ExternalResource struct {
ID string `json:"id,omitempty"`
Type string `json:"type"`
Source string `json:"source"`
Context string `json:"context"`
Destination string `json:"destination"`
IsFolder bool `json:"isFolder"`
Repo string `json:"repo"`
Revision string `json:"revision"`
}
func (t *Trigger) SetVariables(variables map[string]interface{}, encrypted bool) {
for key, value := range variables {
t.Variables = append(t.Variables, Variable{Key: key, Value: value.(string), Encrypted: encrypted})
}
}
func (t *CronTrigger) SetVariables(variables map[string]interface{}, encrypted bool) {
for key, value := range variables {
t.Variables = append(t.Variables, Variable{Key: key, Value: value.(string), Encrypted: encrypted})
}
}
type Spec struct {
Variables []Variable `json:"variables,omitempty"`
SpecTemplate *SpecTemplate `json:"specTemplate,omitempty"`
Triggers []Trigger `json:"triggers,omitempty"`
CronTriggers []CronTrigger `json:"cronTriggers,omitempty"`
Priority int `json:"priority,omitempty"`
Concurrency int `json:"concurrency,omitempty"`
BranchConcurrency int `json:"branchConcurrency,omitempty"`
TriggerConcurrency int `json:"triggerConcurrency,omitempty"`
Contexts []interface{} `json:"contexts,omitempty"`
Steps *Steps `json:"steps,omitempty"`
Stages *Stages `json:"stages,omitempty"`
Mode string `json:"mode,omitempty"`
FailFast *bool `json:"fail_fast,omitempty"`
RuntimeEnvironment RuntimeEnvironment `json:"runtimeEnvironment,omitempty"`
TerminationPolicy []map[string]interface{} `json:"terminationPolicy,omitempty"`
PackId string `json:"packId,omitempty"`
RequiredAvailableStorage string `json:"requiredAvailableStorage,omitempty"`
Hooks *Hooks `json:"hooks,omitempty"`
Options map[string]bool `json:"options,omitempty"`
PermitRestartFromFailedSteps *bool `json:"permitRestartFromFailedSteps,omitempty"`
ExternalResources []ExternalResource `json:"externalResources,omitempty"`
}
type Steps struct {
Steps string
}
type Stages struct {
Stages string
}
type Hooks struct {
Hooks string
}
func (d Steps) MarshalJSON() ([]byte, error) {
bytes := []byte(d.Steps)
return bytes, nil
}
func (d Stages) MarshalJSON() ([]byte, error) {
bytes := []byte(d.Stages)
return bytes, nil
}
func (d Hooks) MarshalJSON() ([]byte, error) {
bytes := []byte(d.Hooks)
return bytes, nil
}
func (d *Steps) UnmarshalJSON(data []byte) error {
d.Steps = string(data)
return nil
}
func (d *Stages) UnmarshalJSON(data []byte) error {
d.Stages = string(data)
return nil
}
func (d *Hooks) UnmarshalJSON(data []byte) error {
d.Hooks = string(data)
return nil
}
type Pipeline struct {
Metadata Metadata `json:"metadata,omitempty"`
Spec Spec `json:"spec,omitempty"`
Version string `json:"version,omitempty"`
}
func (p *Pipeline) SetVariables(variables map[string]interface{}, encrypted bool) {
for key, value := range variables {
p.Spec.Variables = append(p.Spec.Variables, Variable{Key: key, Value: value.(string), Encrypted: encrypted})
}
}
func (pipeline *Pipeline) GetID() string {
if pipeline.Metadata.ID != "" {
return pipeline.Metadata.ID
} else {
return pipeline.Metadata.Name
}
}
func (client *Client) GetPipeline(name string) (*Pipeline, error) {
fullPath := fmt.Sprintf("/pipelines/%s", strings.Replace(name, "/", "%2F", 1))
opts := RequestOptions{
Path: fullPath,
Method: "GET",
}
resp, err := client.RequestAPI(&opts)
if err != nil {
return nil, err
}
var pipeline Pipeline
err = DecodeResponseInto(resp, &pipeline)
if err != nil {
return nil, err
}
return &pipeline, nil
}
func (client *Client) GetPipelines() (*[]Pipeline, error) {
fullPath := "/pipelines"
opts := RequestOptions{
Path: fullPath,
Method: "GET",
}
resp, err := client.RequestAPI(&opts)
if err != nil {
return nil, err
}
var getPipelines Pipelines
err = DecodeResponseInto(resp, &getPipelines)
if err != nil {
return nil, err
}
return &getPipelines.Docs, nil
}
func (client *Client) CreatePipeline(pipeline *Pipeline) (*Pipeline, error) {
body, err := EncodeToJSON(pipeline)
if err != nil {
return nil, err
}
opts := RequestOptions{
Path: "/pipelines",
Method: "POST",
Body: body,
}
resp, err := client.RequestAPI(&opts)
if err != nil {
return nil, err
}
var respPipeline Pipeline
err = DecodeResponseInto(resp, &respPipeline)
if err != nil {
return nil, err
}
return &respPipeline, nil
}
func (client *Client) UpdatePipeline(pipeline *Pipeline) (*Pipeline, error) {
body, err := EncodeToJSON(pipeline)
if err != nil {
return nil, err
}
id := pipeline.GetID()
if id == "" {
return nil, errors.New("[ERROR] Both Pipeline ID and Name are empty")
}
fullPath := fmt.Sprintf("/pipelines/%s", strings.Replace(id, "/", "%2F", 1))
opts := RequestOptions{
Path: fullPath,
Method: "PUT",
Body: body,
}
resp, err := client.RequestAPI(&opts)
if err != nil {
return nil, err
}
var respPipeline Pipeline
err = DecodeResponseInto(resp, &respPipeline)
if err != nil {
return nil, err
}
return &respPipeline, nil
}
func (client *Client) DeletePipeline(name string) error {
fullPath := fmt.Sprintf("/pipelines/%s", strings.Replace(name, "/", "%2F", 1))
opts := RequestOptions{
Path: fullPath,
Method: "DELETE",
}
_, err := client.RequestAPI(&opts)
if err != nil {
return err
}
return nil
}