-
Notifications
You must be signed in to change notification settings - Fork 453
Expand file tree
/
Copy pathqueue.go
More file actions
163 lines (145 loc) · 4.44 KB
/
queue.go
File metadata and controls
163 lines (145 loc) · 4.44 KB
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
// Copyright 2015 Vadim Kravcenko
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package gojenkins
import (
"context"
"strconv"
)
// Queue represents the Jenkins build queue.
type Queue struct {
Jenkins *Jenkins
Raw *queueResponse
Base string
}
type queueResponse struct {
Items []taskResponse
}
// Task represents a single item in the Jenkins build queue.
type Task struct {
Raw *taskResponse
Jenkins *Jenkins
Queue *Queue
Base string
}
type taskResponse struct {
Actions []generalAction `json:"actions"`
Blocked bool `json:"blocked"`
Buildable bool `json:"buildable"`
BuildableStartMilliseconds int64 `json:"buildableStartMilliseconds"`
ID int64 `json:"id"`
InQueueSince int64 `json:"inQueueSince"`
Params string `json:"params"`
Pending bool `json:"pending"`
Stuck bool `json:"stuck"`
Task struct {
Color string `json:"color"`
Name string `json:"name"`
URL string `json:"url"`
} `json:"task"`
URL string `json:"url"`
Why string `json:"why"`
Executable struct {
Number int64 `json:"number"`
URL string `json:"url"`
} `json:"executable"`
}
type generalAction struct {
Causes []map[string]interface{}
Parameters []parameter
}
// Tasks returns all tasks currently in the queue.
func (q *Queue) Tasks() []*Task {
tasks := make([]*Task, len(q.Raw.Items))
for i := range q.Raw.Items {
tasks[i] = &Task{Jenkins: q.Jenkins, Queue: q, Raw: &q.Raw.Items[i]}
}
return tasks
}
// GetTaskById returns a task from the queue by its ID.
func (q *Queue) GetTaskById(id int64) *Task {
for i := range q.Raw.Items {
if q.Raw.Items[i].ID == id {
return &Task{Jenkins: q.Jenkins, Queue: q, Raw: &q.Raw.Items[i]}
}
}
return nil
}
// GetTasksForJob returns all queued tasks for a specific job.
func (q *Queue) GetTasksForJob(name string) []*Task {
tasks := make([]*Task, 0)
for i := range q.Raw.Items {
if q.Raw.Items[i].Task.Name == name {
tasks = append(tasks, &Task{Jenkins: q.Jenkins, Queue: q, Raw: &q.Raw.Items[i]})
}
}
return tasks
}
// CancelTask cancels a queued task by its ID.
func (q *Queue) CancelTask(ctx context.Context, id int64) (bool, error) {
task := q.GetTaskById(id)
return task.Cancel(ctx)
}
// Cancel removes the task from the build queue.
func (t *Task) Cancel(ctx context.Context) (bool, error) {
qr := map[string]string{
"id": strconv.FormatInt(t.Raw.ID, 10),
}
response, err := t.Jenkins.Requester.Post(ctx, t.Jenkins.GetQueueUrl()+"/cancelItem", nil, t.Raw, qr)
if err != nil {
return false, err
}
return response.StatusCode == 200, nil
}
// GetJob returns the job associated with this task.
func (t *Task) GetJob(ctx context.Context) (*Job, error) {
return t.Jenkins.GetJob(ctx, t.Raw.Task.Name)
}
// GetWhy returns the reason why the task is waiting in the queue.
func (t *Task) GetWhy() string {
return t.Raw.Why
}
// GetParameters returns the build parameters for this task.
func (t *Task) GetParameters() []parameter {
for _, a := range t.Raw.Actions {
if a.Parameters != nil {
return a.Parameters
}
}
return nil
}
// GetCauses returns the causes that triggered this task.
func (t *Task) GetCauses() []map[string]interface{} {
for _, a := range t.Raw.Actions {
if a.Causes != nil {
return a.Causes
}
}
return nil
}
// Poll fetches the latest queue data from Jenkins.
func (q *Queue) Poll(ctx context.Context) (int, error) {
response, err := q.Jenkins.Requester.GetJSON(ctx, q.Base, q.Raw, nil)
if err != nil {
return 0, err
}
return response.StatusCode, nil
}
// Poll fetches the latest task data from Jenkins.
func (t *Task) Poll(ctx context.Context) (int, error) {
response, err := t.Jenkins.Requester.GetJSON(ctx, t.Base, t.Raw, nil)
if err != nil {
return 0, err
}
return response.StatusCode, nil
}