forked from shuheiktgw/go-travis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builds.go
276 lines (241 loc) · 8.45 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
// Copyright (c) 2015 Ableton AG, Berlin. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package travis
import (
"context"
"fmt"
"net/http"
"net/url"
)
// BuildsService handles communication with the builds
// related methods of the Travis CI API.
type BuildsService struct {
client *Client
}
// Build represents a Travis CI build
//
// Travis CI API docs: https://developer.travis-ci.com/resource/build#standard-representation
type Build struct {
// Value uniquely identifying the build
Id *uint `json:"id,omitempty"`
// Incremental number for a repository's builds
Number *string `json:"number,omitempty"`
// Current state of the build
State *string `json:"state,omitempty"`
// Wall clock time in seconds
// Duration *uint `json:"duration,omitempty"`
Duration *string `json:"duration,omitempty"`
// Event that triggered the build
EventType *string `json:"event_type,omitempty"`
// State of the previous build (useful to see if state changed)
PreviousState *string `json:"previous_state,omitempty"`
// Title of the build's pull request
PullRequestTitle *string `json:"pull_request_title,omitempty"`
// Number of the build's pull request
PullRequestNumber *uint `json:"pull_request_number,omitempty"`
// When the build started
StartedAt *string `json:"started_at,omitempty"`
// When the build finished
FinishedAt *string `json:"finished_at,omitempty"`
// The last time the build was updated
UpdatedAt *string `json:"updated_at,omitempty"`
// Whether or not the build is private
Private *bool `json:"private,omitempty"`
// GitHub repository the build is associated with
Repository *Repository `json:"repository,omitempty"`
// The branch the build is associated with
Branch *Branch `json:"branch,omitempty"`
// The build's tag
Tag *Tag `json:"tag,omitempty"`
// The commit the build is associated with
Commit *Commit `json:"commit,omitempty"`
// List of jobs that are part of the build's matrix
Jobs []*Job `json:"jobs,omitempty"`
// The stages of the build
Stages []*Stage `json:"stages,omitempty"`
// The User or Organization that created the build
CreatedBy *Owner `json:"created_by,omitempty"`
*Metadata
}
// BuildsOption specifies the optional parameters for builds endpoint
type BuildsOption struct {
// How many builds to include in the response
Limit int `url:"limit,omitempty"`
// How many builds to skip before the first entry in the response
Offset int `url:"offset,omitempty"`
// Attributes to sort builds by
SortBy string `url:"sort_by,omitempty"`
// List of attributes to eager load
Include []string `url:"include,omitempty,comma"`
}
// BuildsByRepoOption specifies the optional parameters for builds endpoint
type BuildsByRepoOption struct {
// Filters builds by name of the git branch
BranchName []string `url:"branch.name,omitempty,comma"`
// The User or Organization that created the build
CreatedBy []string `url:"created_by,omitempty,comma"`
// Event that triggered the build
EventType []string `url:"event_type,omitempty,comma"`
// State of the previous build (useful to see if state changed)
PreviousState []string `url:"previous_state,omitempty,comma"`
// Current state of the build
State []string `url:"state,omitempty,comma"`
// How many builds to include in the response
Limit int `url:"limit,omitempty"`
// How many builds to skip before the first entry in the response
Offset int `url:"offset,omitempty"`
// Attributes to sort builds by
SortBy string `url:"sort_by,omitempty"`
// List of attributes to eager load
Include []string `url:"include,omitempty,comma"`
}
// BuildOption specifies the optional parameters for build endpoint
type BuildOption struct {
// List of attributes to eager load
Include []string `url:"include,omitempty,comma"`
}
type buildsResponse struct {
Builds []*Build `json:"builds"`
}
// buildResponse is only used to parse responses from Restart, Cancel
type buildResponse struct {
Build *Build `json:"build,omitempty"`
}
const (
// BuildStateCreated represents the build state `created`
BuildStateCreated = "created"
// BuildStateReceived represents the build state `received`
BuildStateReceived = "received"
// BuildStateStarted represents the build state `started`
BuildStateStarted = "started"
// BuildStatePassed represents the build state `passed`
BuildStatePassed = "passed"
// BuildStateFailed represents the build state `failed`
BuildStateFailed = "failed"
// BuildStateErrored represents the build state `errored`
BuildStateErrored = "errored"
// BuildStateCanceled represents the build state `canceled`
BuildStateCanceled = "canceled"
)
const (
// BuildEventTypePush represents the build event type `push`
BuildEventTypePush = "push"
// BuildEventTypePullRequest represents the build event type `pull_request`
BuildEventTypePullRequest = "pull_request"
)
// Find fetches a build based on the provided build id
//
// Travis CI API docs: https://developer.travis-ci.com/resource/build#find
func (bs *BuildsService) Find(ctx context.Context, id uint, opt *BuildOption) (*Build, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("/build/%d", id), opt)
if err != nil {
return nil, nil, err
}
req, err := bs.client.NewRequest(http.MethodGet, u, nil, nil)
if err != nil {
return nil, nil, err
}
var build Build
resp, err := bs.client.Do(ctx, req, &build)
if err != nil {
return nil, resp, err
}
return &build, resp, err
}
// List fetches current user's builds based on the provided options
//
// Travis CI API docs: https://developer.travis-ci.com/resource/builds#for_current_user
func (bs *BuildsService) List(ctx context.Context, opt *BuildsOption) ([]*Build, *http.Response, error) {
u, err := urlWithOptions("/builds", opt)
if err != nil {
return nil, nil, err
}
req, err := bs.client.NewRequest(http.MethodGet, u, nil, nil)
if err != nil {
return nil, nil, err
}
var br buildsResponse
resp, err := bs.client.Do(ctx, req, &br)
if err != nil {
return nil, resp, err
}
return br.Builds, resp, err
}
// ListByRepoId fetches current user's builds based on the repository id and options
//
// Travis CI API docs: https://developer.travis-ci.com/resource/builds#find
func (bs *BuildsService) ListByRepoId(ctx context.Context, repoId uint, opt *BuildsByRepoOption) ([]*Build, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("/repo/%d/builds", repoId), opt)
if err != nil {
return nil, nil, err
}
req, err := bs.client.NewRequest(http.MethodGet, u, nil, nil)
if err != nil {
return nil, nil, err
}
var br buildsResponse
resp, err := bs.client.Do(ctx, req, &br)
if err != nil {
return nil, resp, err
}
return br.Builds, resp, err
}
// ListByRepoSlug fetches current user's builds based on the repository slug and options
//
// Travis CI API docs: https://developer.travis-ci.com/resource/builds#find
func (bs *BuildsService) ListByRepoSlug(ctx context.Context, repoSlug string, opt *BuildsByRepoOption) ([]*Build, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("/repo/%s/builds", url.QueryEscape(repoSlug)), opt)
if err != nil {
return nil, nil, err
}
req, err := bs.client.NewRequest(http.MethodGet, u, nil, nil)
if err != nil {
return nil, nil, err
}
var br buildsResponse
resp, err := bs.client.Do(ctx, req, &br)
if err != nil {
return nil, resp, err
}
return br.Builds, resp, err
}
// Cancel cancels a build based on the provided build id
//
// Travis CI API docs: https://developer.travis-ci.com/resource/build#cancel
func (bs *BuildsService) Cancel(ctx context.Context, id uint) (*Build, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("/build/%d/cancel", id), nil)
if err != nil {
return nil, nil, err
}
req, err := bs.client.NewRequest(http.MethodPost, u, nil, nil)
if err != nil {
return nil, nil, err
}
var response buildResponse
resp, err := bs.client.Do(ctx, req, &response)
if err != nil {
return nil, resp, err
}
return response.Build, resp, err
}
// Restart restarts a build based on the provided build id
//
// Travis CI API docs: https://developer.travis-ci.com/resource/build#restart
func (bs *BuildsService) Restart(ctx context.Context, id uint) (*Build, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("/build/%d/restart", id), nil)
if err != nil {
return nil, nil, err
}
req, err := bs.client.NewRequest(http.MethodPost, u, nil, nil)
if err != nil {
return nil, nil, err
}
var br buildResponse
resp, err := bs.client.Do(ctx, req, &br)
if err != nil {
return nil, resp, err
}
return br.Build, resp, err
}