-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.go
103 lines (93 loc) · 2.24 KB
/
queue.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
package jenkins
import (
"fmt"
"net/http"
)
type OneQueueItem struct {
*Item
ID int
build *Build
}
func NewQueueItem(url string, jenkins *Jenkins) *OneQueueItem {
return &OneQueueItem{
Item: NewItem(url, "QueueItem", jenkins),
ID: parseId(url),
build: nil,
}
}
func (q *OneQueueItem) GetJob() (*Job, error) {
var queueJson QueueItem
if err := q.ApiJson(&queueJson, nil); err != nil {
return nil, err
}
if parseClass(queueJson.Class) == "BuildableItem" {
return q.build.GetJob()
}
return NewJob(queueJson.Task.URL, queueJson.Task.Class, q.jenkins), nil
}
func (q *OneQueueItem) GetBuild() (*Build, error) {
if q.build != nil {
return q.build, nil
}
var queueJson QueueItem
if err := q.ApiJson(&queueJson, nil); err != nil {
return nil, err
}
var err error
switch parseClass(queueJson.Class) {
case "LeftItem":
q.build = NewBuild(queueJson.Executable.URL, queueJson.Executable.Class, q.jenkins)
case "BuildableItem", "WaitingItem":
q.build, err = q.getWaitingBuild()
}
return q.build, err
}
func (q *OneQueueItem) getWaitingBuild() (*Build, error) {
builds, err := q.jenkins.Nodes().GetBuilds()
if err != nil {
return nil, err
}
var buildJson struct {
Class string `json:"_class"`
QueueId int `json:"queueId"`
}
for _, build := range builds {
if err := build.ApiJson(&buildJson, &ApiJsonOpts{Tree: "queueId"}); err != nil {
return nil, err
}
if buildJson.QueueId == q.ID {
return build, nil
}
}
// build is not avaliable and no error
return nil, nil
}
type Queue struct {
*Item
}
func (q *Queue) List() ([]*OneQueueItem, error) {
queue := &QueueJson{}
if err := q.ApiJson(queue, nil); err != nil {
return nil, err
}
var items []*OneQueueItem
for _, item := range queue.Items {
items = append(items, NewQueueItem(item.URL, q.jenkins))
}
return items, nil
}
func (q *Queue) Get(id int) (*OneQueueItem, error) {
var queue QueueJson
if err := q.ApiJson(&queue, nil); err != nil {
return nil, err
}
for _, item := range queue.Items {
if item.ID == id {
return NewQueueItem(item.URL, q.jenkins), nil
}
}
return nil, fmt.Errorf("no such queue item #%d", id)
}
func (q *Queue) Cancel(id int) (*http.Response, error) {
return q.Request("POST", fmt.Sprintf("cancelItem?id=%d", id), nil)
}