-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
317 lines (279 loc) · 8.04 KB
/
job.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
package jenkins
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"path"
"slices"
"strings"
)
type Job struct {
*Item
credentials *Credentials
views *Views
Name string
FullName string
FullDisplayName string
}
func NewJob(url, class string, jenkins *Jenkins) *Job {
j := &Job{Item: NewItem(url, class, jenkins)}
j.setName()
return j
}
func (j *Job) Views() *Views {
if j.views == nil {
j.views = &Views{Item: NewItem(j.URL, "Views", j.jenkins)}
}
return j.views
}
func (j *Job) Credentials() *Credentials {
if j.credentials == nil {
j.credentials = &Credentials{Item: NewItem(j.URL+"credentials/store/folder/domain/_/", "Credentials", j.jenkins)}
}
return j.credentials
}
func (j *Job) Rename(name string) (newUrl *url.URL, err error) {
v := url.Values{}
v.Add("newName", name)
resp, err := j.Request("POST", "confirmRename?"+v.Encode(), nil)
if err != nil {
return nil, err
}
defer func() {
j.URL = appendSlash(newUrl.String())
j.setName()
}()
return resp.Location()
}
func (j *Job) Move(path string) (newUrl *url.URL, err error) {
v := url.Values{}
v.Add("destination", "/"+strings.Trim(path, "/"))
resp, err := j.Request("POST", "move/move?"+v.Encode(), nil)
if err != nil {
return
}
defer func() {
j.URL = appendSlash(newUrl.String())
j.setName()
}()
return resp.Location()
}
func (j *Job) Copy(src, dest string) (*http.Response, error) {
v := url.Values{}
v.Add("name", dest)
v.Add("mode", "copy")
v.Add("from", src)
return j.Request("POST", "createItem?"+v.Encode(), nil)
}
func (j *Job) GetParent() (*Job, error) {
fullName, _ := j.jenkins.URL2Name(j.URL)
dir, _ := path.Split(strings.Trim(fullName, "/"))
if dir == "" {
return nil, fmt.Errorf("%s have no parent", j)
}
return j.jenkins.GetJob(dir)
}
func (j *Job) GetConfigure() (string, error) {
return readResponseToString(j, "GET", "config.xml", nil)
}
func (j *Job) SetConfigure(xml io.Reader) (*http.Response, error) {
return j.Request("POST", "config.xml", xml)
}
func (j *Job) Disable() (*http.Response, error) {
return j.Request("POST", "disable", nil)
}
func (j *Job) Enable() (*http.Response, error) {
return j.Request("POST", "enable", nil)
}
func (j *Job) IsBuildable() (bool, error) {
var job struct {
Class string `json:"_class"`
Buildable bool `json:"buildable"`
}
err := j.ApiJson(&job, &ApiJsonOpts{Tree: "buildable"})
return job.Buildable, err
}
func (j *Job) setName() {
urlPath, _ := j.jenkins.URL2Name(j.URL)
j.FullName, _ = url.PathUnescape(urlPath)
_, j.Name = path.Split(j.FullName)
j.FullDisplayName, _ = url.PathUnescape(strings.ReplaceAll(j.FullName, "/", " » "))
}
func (j *Job) GetDescription() (string, error) {
data := make(map[string]string)
if err := j.ApiJson(&data, &ApiJsonOpts{Tree: "description"}); err != nil {
return "", err
}
return data["description"], nil
}
func (j *Job) SetDescription(description string) (*http.Response, error) {
v := url.Values{}
v.Add("description", description)
return j.Request("POST", "submitDescription?"+v.Encode(), nil)
}
func (j *Job) Build(param url.Values) (*OneQueueItem, error) {
entry := func() string {
reserved := []string{"token", "delay"}
for k := range param {
if !slices.Contains(reserved, k) {
return "buildWithParameters"
}
}
return "build"
}()
resp, err := j.Request("POST", entry+"?"+param.Encode(), nil)
if err != nil {
return nil, err
}
url, err := resp.Location()
if err != nil {
return nil, err
}
return NewQueueItem(url.String(), j.jenkins), nil
}
func (j *Job) GetBuild(number int) (*Build, error) {
if j.Class == "Folder" || j.Class == "WorkflowMultiBranchProject" {
return nil, fmt.Errorf("%s have no builds", j)
}
jobJson := &JobJson{}
if err := j.ApiJson(&jobJson, &ApiJsonOpts{Tree: "builds[number,url]"}); err != nil {
return nil, err
}
for _, build := range jobJson.Builds {
if number == build.Number {
return NewBuild(build.URL, build.Class, j.jenkins), nil
}
}
return nil, fmt.Errorf("%s have no builds #%d", j, number)
}
func (j *Job) Get(name string) (*Job, error) {
if j.Class != "Folder" && j.Class != "WorkflowMultiBranchProject" {
return nil, fmt.Errorf("%s have no jobs", j)
}
var folderJson JobJson
if err := j.ApiJson(&folderJson, &ApiJsonOpts{Tree: "jobs[url,name]"}); err != nil {
return nil, err
}
for _, job := range folderJson.Jobs {
if job.Name == name {
return NewJob(job.URL, job.Class, j.jenkins), nil
}
}
return nil, fmt.Errorf("no such job [%s%s]", j.URL, name)
}
func (j *Job) Create(name string, xml io.Reader) (*http.Response, error) {
v := url.Values{}
v.Add("name", name)
return j.Request("POST", "createItem?"+v.Encode(), xml)
}
func (j *Job) List(depth int) ([]*Job, error) {
if j.Class != "Folder" && j.Class != "WorkflowMultiBranchProject" {
return nil, fmt.Errorf("%s have no jobs", j)
}
query := "jobs[url]"
qf := "jobs[url,%s]"
for i := 0; i < depth; i++ {
query = fmt.Sprintf(qf, query)
}
var folderJson JobJson
if err := j.ApiJson(&folderJson, &ApiJsonOpts{Tree: query}); err != nil {
return nil, err
}
var jobs []*Job
var _resolve func(item *JobJson)
_resolve = func(item *JobJson) {
for _, job := range item.Jobs {
if len(job.Jobs) > 0 {
_resolve(job)
}
jobs = append(jobs, NewJob(job.URL, job.Class, j.jenkins))
}
}
_resolve(&folderJson)
return jobs, nil
}
func (j *Job) GetFirstBuild() (*Build, error) {
return j.GetBuildByName("firstBuild")
}
func (j *Job) GetLastBuild() (*Build, error) {
return j.GetBuildByName("lastBuild")
}
func (j *Job) GetLastCompleteBuild() (*Build, error) {
return j.GetBuildByName("lastCompletedBuild")
}
func (j *Job) GetLastFailedBuild() (*Build, error) {
return j.GetBuildByName("lastFailedBuild")
}
func (j *Job) GetLastStableBuild() (*Build, error) {
return j.GetBuildByName("lastStableBuild")
}
func (j *Job) GetLastUnstableBuild() (*Build, error) {
return j.GetBuildByName("lastUnstableBuild")
}
func (j *Job) GetLastSuccessfulBuild() (*Build, error) {
return j.GetBuildByName("lastSuccessfulBuild")
}
func (j *Job) GetLastUnsucessfulBuild() (*Build, error) {
return j.GetBuildByName("lastUnsuccessfulBuild")
}
func (j *Job) GetBuildByName(name string) (*Build, error) {
if j.Class == "Folder" || j.Class == "WorkflowMultiBranchProject" {
return nil, fmt.Errorf("%s have no builds", j)
}
var jobJson map[string]json.RawMessage
if err := j.ApiJson(&jobJson, &ApiJsonOpts{Tree: name + "[url]"}); err != nil {
return nil, err
}
if string(jobJson[name]) == "null" {
// build is null but no http error
return nil, nil
}
build := &BuildJson{}
if err := json.Unmarshal(jobJson[name], build); err != nil {
return nil, err
}
return NewBuild(build.URL, build.Class, j.jenkins), nil
}
func (j *Job) Delete() (*http.Response, error) {
return j.Request("POST", "doDelete", nil)
}
func (j *Job) ListBuilds() ([]*Build, error) {
if j.Class == "Folder" || j.Class == "WorkflowMultiBranchProject" {
return nil, fmt.Errorf("%s have no builds", j)
}
var jobJson JobJson
var builds []*Build
if err := j.ApiJson(&jobJson, &ApiJsonOpts{Tree: "builds[url]"}); err != nil {
return nil, err
}
for _, build := range jobJson.Builds {
builds = append(builds, NewBuild(build.URL, build.Class, j.jenkins))
}
return builds, nil
}
func (j *Job) SetNextBuildNumber(number int) (*http.Response, error) {
return j.Request("POST", fmt.Sprintf("nextbuildnumber/submit?nextBuildNumber=%d", number), nil)
}
func (j *Job) GetParameters() ([]*ParameterDefinition, error) {
jobJson := &JobJson{}
if err := j.ApiJson(jobJson, nil); err != nil {
return nil, err
}
for _, p := range jobJson.Property {
if p.Class == "hudson.model.ParametersDefinitionProperty" {
return p.ParameterDefinitions, nil
}
}
return nil, fmt.Errorf("%s has no parameters", j)
}
func (j *Job) SCMPolling() (*http.Response, error) {
return j.Request("POST", "polling", nil)
}
func (j *Job) GetMultibranchPipelineScanLog() (string, error) {
if j.Class != "WorkflowMultiBranchProject" {
return "", fmt.Errorf("%s is not a WorkflowMultiBranchProject", j)
}
return readResponseToString(j, "POST", "indexing/consoleText", nil)
}