forked from shuheiktgw/go-travis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjobs.go
241 lines (208 loc) · 6.73 KB
/
jobs.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
// 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"
)
// JobsService handles communication with the jobs
// related methods of the Travis CI API.
type JobsService struct {
client *Client
}
// Job represents a Travis CI job
//
// Travis CI API docs: https://developer.travis-ci.com/resource/job#standard-representation
type Job struct {
// Value uniquely identifying the job
Id *uint `json:"id,omitempty"`
// The job's allow_failure
AllowFailure *bool `json:"allow_failure,omitempty"`
// Incremental number for a repository's builds
Number *string `json:"number,omitempty"`
// Current state of the job
State *string `json:"state,omitempty"`
// When the job started
StartedAt *string `json:"started_at,omitempty"`
// When the job finished
FinishedAt *string `json:"finished_at,omitempty"`
// The build the job is associated with
Build *Build `json:"build,omitempty"`
// Worker queue this job is/was scheduled on
Queue *string `json:"queue,omitempty"`
// GitHub repository the job is associated with
Repository *Repository `json:"repository,omitempty"`
// The commit the job is associated with
Commit *Commit `json:"commit,omitempty"`
// GitHub user or organization the job belongs to
Owner *Owner `json:"owner,omitempty"`
// The stages of the job
Stage *Stage `json:"stage,omitempty"`
// When the job was created
CreatedAt *string `json:"created_at,omitempty"`
// When the job was updated
UpdatedAt *string `json:"updated_at,omitempty"`
// Whether or not the job is private
Private *bool `json:"private,omitempty"`
*Metadata
}
// JobsOption is query parameters to one can specify to find jobs
type JobsOption struct {
// How many jobs to include in the response
Limit int `url:"limit,omitempty"`
// How many jobs to skip before the first entry in the response
Offset int `url:"offset,omitempty"`
// Attributes to sort jobs by
SortBy []string `url:"sort_by,omitempty,comma"`
// // Current state of the job
State []string `url:"state,omitempty,comma"`
// List of attributes to eager load
Include []string `url:"include,omitempty,comma"`
}
// JobOption is query parameters to one can specify to find job
type JobOption struct {
// List of attributes to eager load
Include []string `url:"include,omitempty,comma"`
}
type jobsResponse struct {
Jobs []*Job `json:"jobs"`
}
// jobResponse is only used to parse responses from Restart, Cancel and Debug
type jobResponse struct {
Job *Job `json:"job,omitempty"`
}
const (
// JobStatusCreated represents the job state `created`
JobStatusCreated = "created"
// JobStatusQueued represents the job state `queued`
JobStatusQueued = "queued"
// JobStatusReceived represents the job state `received`
JobStatusReceived = "received"
// JobStatusStarted represents the job state `started`
JobStatusStarted = "started"
// JobStatusCanceled represents the job state `canceled`
JobStatusCanceled = "canceled"
// JobStatusPassed represents the job state `passed`
JobStatusPassed = "passed"
)
// Find fetches a job based on the provided job id
//
// Travis CI API docs: https://developer.travis-ci.com/resource/job#find
func (js *JobsService) Find(ctx context.Context, id uint, opt *JobOption) (*Job, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("/job/%d", id), opt)
if err != nil {
return nil, nil, err
}
req, err := js.client.NewRequest(http.MethodGet, u, nil, nil)
if err != nil {
return nil, nil, err
}
var job Job
resp, err := js.client.Do(ctx, req, &job)
if err != nil {
return nil, resp, err
}
return &job, resp, err
}
// ListByBuild fetches jobs based on the provided build id
//
// Travis CI API docs: https://developer.travis-ci.csom/resource/jobs#find
func (js *JobsService) ListByBuild(ctx context.Context, buildId uint) ([]*Job, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("/build/%d/jobs", buildId), nil)
if err != nil {
return nil, nil, err
}
req, err := js.client.NewRequest(http.MethodGet, u, nil, nil)
if err != nil {
return nil, nil, err
}
var jr jobsResponse
resp, err := js.client.Do(ctx, req, &jr)
if err != nil {
return nil, resp, err
}
return jr.Jobs, resp, err
}
// List fetches current user's jobs based on the provided options
// As of 2018/9/4, this endpoint returns 500 and does not seem to work correctly
// See jobs_integration_test.go, TestJobsService_Find
//
// Travis CI API docs: https://developer.travis-ci.com/resource/jobs#find
func (js *JobsService) List(ctx context.Context, opt *JobsOption) ([]*Job, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("/jobs"), opt)
if err != nil {
return nil, nil, err
}
req, err := js.client.NewRequest(http.MethodGet, u, nil, nil)
if err != nil {
return nil, nil, err
}
var jr jobsResponse
resp, err := js.client.Do(ctx, req, &jr)
if err != nil {
return nil, resp, err
}
return jr.Jobs, resp, err
}
// Cancel cancels a job based on the provided job id
//
// Travis CI API docs: https://developer.travis-ci.com/resource/job#cancel
func (js *JobsService) Cancel(ctx context.Context, id uint) (*Job, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("/job/%d/cancel", id), nil)
if err != nil {
return nil, nil, err
}
req, err := js.client.NewRequest(http.MethodPost, u, nil, nil)
if err != nil {
return nil, nil, err
}
var jr jobResponse
resp, err := js.client.Do(ctx, req, &jr)
if err != nil {
return nil, resp, err
}
return jr.Job, resp, err
}
// Restart restarts a job based on the provided job id
//
// Travis CI API docs: https://developer.travis-ci.com/resource/job#restart
func (js *JobsService) Restart(ctx context.Context, id uint) (*Job, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("/job/%d/restart", id), nil)
if err != nil {
return nil, nil, err
}
req, err := js.client.NewRequest(http.MethodPost, u, nil, nil)
if err != nil {
return nil, nil, err
}
var jr jobResponse
resp, err := js.client.Do(ctx, req, &jr)
if err != nil {
return nil, resp, err
}
return jr.Job, resp, err
}
// Debug restarts a job in debug mode based on the provided job id
// Debug is only available on the travis-ci.com domain, and you need
// to enable the debug feature
//
// Travis CI API docs: https://developer.travis-ci.com/resource/job#debug
func (js *JobsService) Debug(ctx context.Context, id uint) (*Job, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("/job/%d/debug", id), nil)
if err != nil {
return nil, nil, err
}
req, err := js.client.NewRequest(http.MethodPost, u, nil, nil)
if err != nil {
return nil, nil, err
}
var jr jobResponse
resp, err := js.client.Do(ctx, req, &jr)
if err != nil {
return nil, resp, err
}
return jr.Job, resp, err
}