forked from Ableton/go-travis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjobs.go
209 lines (173 loc) · 5.19 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
// 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.
//
// Fragments of this file have been copied from the go-github (https://github.com/google/go-github)
// project, and is therefore licensed under the following copyright:
// Copyright 2013 The go-github AUTHORS. All rights reserved.
package travis
import (
"fmt"
"net/http"
"github.com/fatih/structs"
)
// JobsService handles communication with the jobs
// related methods of the Travis CI API.
type JobsService struct {
client *Client
}
// Job represents a Travis CI job
type Job struct {
Id uint `json:"id,omitempty"`
BuildId uint `json:"build_id,omitempty"`
RepositoryId uint `json:"repository_id,omitempty"`
CommitId uint `json:"commit_id,omitempty"`
LogId uint `json:"log_id,omitempty"`
Number string `json:"number,omitempty"`
// Config Config `json:"config,omitempty"`
State string `json:"state,omitempty"`
StartedAt string `json:"started_at,omitempty"`
FinishedAt string `json:"finished_at,omitempty"`
Duration uint `json:"duration,omitempty"`
Queue string `json:"queue,omitempty"`
AllowFailure bool `json:"allow_failure,omitempty"`
AnnotationIds []uint `json:"annotation_ids,omitempty"`
}
type findJobsResponse struct {
Jobs []Job `json:"jobs"`
}
// getJobResponse represents the response of a call
// to the Travis CI get build endpoint.
type getJobResponse struct {
Job Job `json:"job"`
}
// JobListOptions specifies the optional parameters to the
// JobsService.List method. You need to provide exactly one
// of the below attribute. If you provide State or Queue, a
// maximum of 250 jobs will be returned.
type JobFindOptions struct {
ListOptions
// List of job ids
Ids []uint `url:"ids,omitempty"`
// Job state to filter by
State string `url:"state,omitempty"`
// Job queue to filter by
Queue string `url:"queue,omitempty"`
}
// IsValid asserts the JobFindOptions instance has one
// and only one value set to a non-zero value.
//
// This method is particularly useful to check a JobFindOptions
// instance before passing it to JobsService.Find method.
func (jfo *JobFindOptions) IsValid() bool {
s := structs.New(jfo)
f := s.Fields()
nonZeroValues := 0
for _, field := range f {
if !field.IsZero() {
nonZeroValues += 1
}
}
return nonZeroValues == 0 || nonZeroValues == 1
}
// Get fetches job with the provided id.
//
// Travis CI API docs: http://docs.travis-ci.com/api/#jobs
func (js *JobsService) Get(id uint) (*Job, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("/jobs/%d", id), nil)
if err != nil {
return nil, nil, err
}
req, err := js.client.NewRequest("GET", u, nil, nil)
if err != nil {
return nil, nil, err
}
var jobResp getJobResponse
resp, err := js.client.Do(req, &jobResp)
if err != nil {
return nil, resp, err
}
return &jobResp.Job, resp, err
}
// ListByBuild retrieve a build jobs from its provided id.
//
// Travis CI API docs: http://docs.travis-ci.com/api/#jobs
func (js *JobsService) ListFromBuild(buildId uint) ([]Job, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("/builds/%d", buildId), nil)
if err != nil {
return nil, nil, err
}
req, err := js.client.NewRequest("GET", u, nil, nil)
if err != nil {
return nil, nil, err
}
var buildResp getBuildResponse
resp, err := js.client.Do(req, &buildResp)
if err != nil {
return nil, resp, err
}
return buildResp.Jobs, resp, err
}
// Find jobs using the provided options.
// You need to provide exactly one of the opt fields value.
// If you provide State or Queue, a maximum of 250 jobs will be returned.
//
// Travis CI API docs: http://docs.travis-ci.com/api/#jobs
func (js *JobsService) Find(opt *JobFindOptions) ([]Job, *http.Response, error) {
if opt != nil && !opt.IsValid() {
return nil, nil, fmt.Errorf(
"More than one value set in provided JobFindOptions instance",
)
}
u, err := urlWithOptions("/jobs", opt)
if err != nil {
return nil, nil, err
}
req, err := js.client.NewRequest("GET", u, nil, nil)
if err != nil {
return nil, nil, err
}
var jobsResp findJobsResponse
resp, err := js.client.Do(req, &jobsResp)
if err != nil {
return nil, resp, err
}
return jobsResp.Jobs, resp, err
}
// Cancel job with the provided id.
//
// Travis CI API docs: http://docs.travis-ci.com/api/#jobs
func (js *JobsService) Cancel(id uint) (*http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("/jobs/%d/cancel", id), nil)
if err != nil {
return nil, err
}
req, err := js.client.NewRequest("POST", u, nil, nil)
if err != nil {
return nil, err
}
resp, err := js.client.Do(req, nil)
if err != nil {
return resp, err
}
return resp, err
}
// Restart job with the provided id.
//
// Travis CI API docs: http://docs.travis-ci.com/api/#jobs
func (js *JobsService) Restart(id uint) (*http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("/jobs/%d/restart", id), nil)
if err != nil {
return nil, err
}
req, err := js.client.NewRequest("POST", u, nil, nil)
if err != nil {
return nil, err
}
resp, err := js.client.Do(req, nil)
if err != nil {
return resp, err
}
return resp, err
}