-
Notifications
You must be signed in to change notification settings - Fork 9
/
builds.go
283 lines (240 loc) · 10.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
package codeship
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/pkg/errors"
)
// BuildLinks structure of BuildLinks object for a Build
type BuildLinks struct {
Pipelines string `json:"pipelines,omitempty"`
Services string `json:"services,omitempty"`
Steps string `json:"steps,omitempty"`
}
// Build structure of Build object
type Build struct {
AllocatedAt time.Time `json:"allocated_at,omitempty"`
Branch string `json:"branch,omitempty"`
CommitMessage string `json:"commit_message,omitempty"`
CommitSha string `json:"commit_sha,omitempty"`
FinishedAt time.Time `json:"finished_at,omitempty"`
Links BuildLinks `json:"links,omitempty"`
OrganizationUUID string `json:"organization_uuid,omitempty"`
ProjectID uint `json:"project_id,omitempty"`
ProjectUUID string `json:"project_uuid,omitempty"`
QueuedAt time.Time `json:"queued_at,omitempty"`
Ref string `json:"ref,omitempty"`
Status string `json:"status,omitempty"`
Username string `json:"username,omitempty"`
UUID string `json:"uuid,omitempty"`
}
// BuildList holds a list of Build objects
type BuildList struct {
Builds []Build `json:"builds"`
pagination
}
type buildResponse struct {
Build Build `json:"build"`
}
// BuildPipelineMetrics structure of BuildPipelineMetrics object for a BuildPipeline
type BuildPipelineMetrics struct {
AmiID string `json:"ami_id,omitempty"`
Queries string `json:"queries,omitempty"`
CPUUser string `json:"cpu_user,omitempty"`
Duration string `json:"duration,omitempty"`
CPUSystem string `json:"cpu_system,omitempty"`
InstanceID string `json:"instance_id,omitempty"`
Architecture string `json:"architecture,omitempty"`
InstanceType string `json:"instance_type,omitempty"`
CPUPerSecond string `json:"cpu_per_second,omitempty"`
DiskFreeBytes string `json:"disk_free_bytes,omitempty"`
DiskUsedBytes string `json:"disk_used_bytes,omitempty"`
NetworkRxBytes string `json:"network_rx_bytes,omitempty"`
NetworkTxBytes string `json:"network_tx_bytes,omitempty"`
MaxUsedConnections string `json:"max_used_connections,omitempty"`
MemoryMaxUsageInBytes string `json:"memory_max_usage_in_bytes,omitempty"`
}
// BuildPipeline structure of BuildPipeline object for a Basic Project
type BuildPipeline struct {
UUID string `json:"uuid,omitempty"`
BuildUUID string `json:"build_uuid,omitempty"`
Type string `json:"type,omitempty"`
Status string `json:"status,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
FinishedAt time.Time `json:"finished_at,omitempty"`
Metrics BuildPipelineMetrics `json:"metrics,omitempty"`
}
// BuildPipelines holds a list of BuildPipeline objects for a Basic Project
type BuildPipelines struct {
Pipelines []BuildPipeline `json:"pipelines"`
pagination
}
// BuildStep structure of BuildStep object for a Pro Project
type BuildStep struct {
BuildUUID string `json:"build_uuid,omitempty"`
BuildingAt time.Time `json:"building_at,omitempty"`
Command string `json:"command,omitempty"`
FinishedAt time.Time `json:"finished_at,omitempty"`
ImageName string `json:"image_name,omitempty"`
Name string `json:"name,omitempty"`
Registry string `json:"registry,omitempty"`
ServiceUUID string `json:"service_uuid,omitempty"`
StartedAt time.Time `json:"started_at,omitempty"`
Status string `json:"status,omitempty"`
Steps []BuildStep `json:"steps,omitempty"`
Tag string `json:"tag,omitempty"`
Type string `json:"type,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
UUID string `json:"uuid,omitempty"`
}
// BuildSteps holds a list of BuildStep objects for a Pro Project
type BuildSteps struct {
Steps []BuildStep `json:"steps"`
pagination
}
// BuildService structure of BuildService object for a Pro Project
type BuildService struct {
BuildUUID string `json:"build_uuid,omitempty"`
BuildingAt time.Time `json:"building_at,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
FinishedAt time.Time `json:"finished_at,omitempty"`
Name string `json:"name,omitempty"`
PullingAt time.Time `json:"pulling_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
UUID string `json:"uuid,omitempty"`
Status string `json:"status,omitempty"`
}
// BuildServices holds a list of BuildService objects for a Pro Project
type BuildServices struct {
Services []BuildService `json:"services"`
pagination
}
type buildRequest struct {
CommitSha string `json:"commit_sha,omitempty"`
Ref string `json:"ref,omitempty"`
}
// CreateBuild creates a new build
//
// Codeship API docs: https://apidocs.codeship.com/v2/builds/create-build
func (o *Organization) CreateBuild(ctx context.Context, projectUUID, ref, commitSha string) (bool, Response, error) {
path := fmt.Sprintf("/organizations/%s/projects/%s/builds", o.UUID, projectUUID)
_, resp, err := o.client.request(ctx, "POST", path, buildRequest{
Ref: ref,
CommitSha: commitSha,
})
if err != nil {
return false, resp, errors.Wrap(err, "unable to create build")
}
return true, resp, nil
}
// GetBuild fetches a build by UUID
//
// Codeship API docs: https://apidocs.codeship.com/v2/builds/get-build
func (o *Organization) GetBuild(ctx context.Context, projectUUID, buildUUID string) (Build, Response, error) {
path := fmt.Sprintf("/organizations/%s/projects/%s/builds/%s", o.UUID, projectUUID, buildUUID)
body, resp, err := o.client.request(ctx, "GET", path, nil)
if err != nil {
return Build{}, resp, errors.Wrap(err, "unable to get build")
}
var build buildResponse
if err = json.Unmarshal(body, &build); err != nil {
return Build{}, resp, errors.Wrap(err, "unable to unmarshal response into Build")
}
return build.Build, resp, nil
}
// ListBuilds fetches a list of builds
//
// Codeship API docs: https://apidocs.codeship.com/v2/builds/list-builds
func (o *Organization) ListBuilds(ctx context.Context, projectUUID string, opts ...PaginationOption) (BuildList, Response, error) {
path, err := paginate(fmt.Sprintf("/organizations/%s/projects/%s/builds", o.UUID, projectUUID), opts...)
if err != nil {
return BuildList{}, Response{}, errors.Wrap(err, "unable to list builds")
}
body, resp, err := o.client.request(ctx, "GET", path, nil)
if err != nil {
return BuildList{}, resp, errors.Wrap(err, "unable to list builds")
}
var builds BuildList
if err = json.Unmarshal(body, &builds); err != nil {
return BuildList{}, resp, errors.Wrap(err, "unable to unmarshal response into BuildList")
}
return builds, resp, nil
}
// ListBuildPipelines lists Basic build pipelines
//
// Codeship API docs: https://apidocs.codeship.com/v2/builds/get-build-pipelines
func (o *Organization) ListBuildPipelines(ctx context.Context, projectUUID, buildUUID string, opts ...PaginationOption) (BuildPipelines, Response, error) {
path, err := paginate(fmt.Sprintf("/organizations/%s/projects/%s/builds/%s/pipelines", o.UUID, projectUUID, buildUUID), opts...)
if err != nil {
return BuildPipelines{}, Response{}, errors.Wrap(err, "unable to list pipelines")
}
body, resp, err := o.client.request(ctx, "GET", path, nil)
if err != nil {
return BuildPipelines{}, resp, errors.Wrap(err, "unable to list pipelines")
}
var pipelines BuildPipelines
if err = json.Unmarshal(body, &pipelines); err != nil {
return BuildPipelines{}, resp, errors.Wrap(err, "unable to unmarshal response into BuildPipelines")
}
return pipelines, resp, nil
}
// StopBuild stops a running build
//
// Codeship API docs: https://apidocs.codeship.com/v2/builds/stop-build
func (o *Organization) StopBuild(ctx context.Context, projectUUID, buildUUID string) (bool, Response, error) {
path := fmt.Sprintf("/organizations/%s/projects/%s/builds/%s/stop", o.UUID, projectUUID, buildUUID)
_, resp, err := o.client.request(ctx, "POST", path, nil)
if err != nil {
return false, resp, errors.Wrap(err, "unable to stop build")
}
return true, resp, nil
}
// RestartBuild restarts a previous build
//
// Codeship API docs: https://apidocs.codeship.com/v2/builds/restart-build
func (o *Organization) RestartBuild(ctx context.Context, projectUUID, buildUUID string) (bool, Response, error) {
path := fmt.Sprintf("/organizations/%s/projects/%s/builds/%s/restart", o.UUID, projectUUID, buildUUID)
_, resp, err := o.client.request(ctx, "POST", path, nil)
if err != nil {
return false, resp, errors.Wrap(err, "unable to restart build")
}
return true, resp, nil
}
// ListBuildServices lists Pro build services
//
// Codeship API docs: https://apidocs.codeship.com/v2/builds/get-build-services
func (o *Organization) ListBuildServices(ctx context.Context, projectUUID, buildUUID string, opts ...PaginationOption) (BuildServices, Response, error) {
path, err := paginate(fmt.Sprintf("/organizations/%s/projects/%s/builds/%s/services", o.UUID, projectUUID, buildUUID), opts...)
if err != nil {
return BuildServices{}, Response{}, errors.Wrap(err, "unable to list build services")
}
body, resp, err := o.client.request(ctx, "GET", path, nil)
if err != nil {
return BuildServices{}, resp, errors.Wrap(err, "unable to list build services")
}
var services BuildServices
if err = json.Unmarshal(body, &services); err != nil {
return BuildServices{}, resp, errors.Wrap(err, "unable to unmarshal response into BuildServices")
}
return services, resp, nil
}
// ListBuildSteps lists Pro build steps
//
// Codeship API docs: https://apidocs.codeship.com/v2/builds/get-build-steps
func (o *Organization) ListBuildSteps(ctx context.Context, projectUUID, buildUUID string, opts ...PaginationOption) (BuildSteps, Response, error) {
path, err := paginate(fmt.Sprintf("/organizations/%s/projects/%s/builds/%s/steps", o.UUID, projectUUID, buildUUID), opts...)
if err != nil {
return BuildSteps{}, Response{}, errors.Wrap(err, "unable to list build steps")
}
body, resp, err := o.client.request(ctx, "GET", path, nil)
if err != nil {
return BuildSteps{}, resp, errors.Wrap(err, "unable to list build steps")
}
var steps BuildSteps
if err = json.Unmarshal(body, &steps); err != nil {
return BuildSteps{}, resp, errors.Wrap(err, "unable to unmarshal response into BuildSteps")
}
return steps, resp, nil
}