github.com/bndr/gojenkins@v1.1.0/queue.go (about) 1 // Copyright 2015 Vadim Kravcenko 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"): you may 4 // not use this file except in compliance with the License. You may obtain 5 // a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations 13 // under the License. 14 15 package gojenkins 16 17 import ( 18 "context" 19 "strconv" 20 ) 21 22 type Queue struct { 23 Jenkins *Jenkins 24 Raw *queueResponse 25 Base string 26 } 27 28 type queueResponse struct { 29 Items []taskResponse 30 } 31 32 type Task struct { 33 Raw *taskResponse 34 Jenkins *Jenkins 35 Queue *Queue 36 Base string 37 } 38 39 type taskResponse struct { 40 Actions []generalAction `json:"actions"` 41 Blocked bool `json:"blocked"` 42 Buildable bool `json:"buildable"` 43 BuildableStartMilliseconds int64 `json:"buildableStartMilliseconds"` 44 ID int64 `json:"id"` 45 InQueueSince int64 `json:"inQueueSince"` 46 Params string `json:"params"` 47 Pending bool `json:"pending"` 48 Stuck bool `json:"stuck"` 49 Task struct { 50 Color string `json:"color"` 51 Name string `json:"name"` 52 URL string `json:"url"` 53 } `json:"task"` 54 URL string `json:"url"` 55 Why string `json:"why"` 56 Executable struct { 57 Number int64 `json:"number"` 58 URL string `json:"url"` 59 } `json:"executable"` 60 } 61 62 type generalAction struct { 63 Causes []map[string]interface{} 64 Parameters []parameter 65 } 66 67 func (q *Queue) Tasks() []*Task { 68 tasks := make([]*Task, len(q.Raw.Items)) 69 for i, t := range q.Raw.Items { 70 tasks[i] = &Task{Jenkins: q.Jenkins, Queue: q, Raw: &t} 71 } 72 return tasks 73 } 74 75 func (q *Queue) GetTaskById(id int64) *Task { 76 for _, t := range q.Raw.Items { 77 if t.ID == id { 78 return &Task{Jenkins: q.Jenkins, Queue: q, Raw: &t} 79 } 80 } 81 return nil 82 } 83 84 func (q *Queue) GetTasksForJob(name string) []*Task { 85 tasks := make([]*Task, 0) 86 for _, t := range q.Raw.Items { 87 if t.Task.Name == name { 88 tasks = append(tasks, &Task{Jenkins: q.Jenkins, Queue: q, Raw: &t}) 89 } 90 } 91 return tasks 92 } 93 94 func (q *Queue) CancelTask(ctx context.Context, id int64) (bool, error) { 95 task := q.GetTaskById(id) 96 return task.Cancel(ctx) 97 } 98 99 func (t *Task) Cancel(ctx context.Context) (bool, error) { 100 qr := map[string]string{ 101 "id": strconv.FormatInt(t.Raw.ID, 10), 102 } 103 response, err := t.Jenkins.Requester.Post(ctx, t.Jenkins.GetQueueUrl()+"/cancelItem", nil, t.Raw, qr) 104 if err != nil { 105 return false, err 106 } 107 return response.StatusCode == 200, nil 108 } 109 110 func (t *Task) GetJob(ctx context.Context) (*Job, error) { 111 return t.Jenkins.GetJob(ctx, t.Raw.Task.Name) 112 } 113 114 func (t *Task) GetWhy() string { 115 return t.Raw.Why 116 } 117 118 func (t *Task) GetParameters() []parameter { 119 for _, a := range t.Raw.Actions { 120 if a.Parameters != nil { 121 return a.Parameters 122 } 123 } 124 return nil 125 } 126 127 func (t *Task) GetCauses() []map[string]interface{} { 128 for _, a := range t.Raw.Actions { 129 if a.Causes != nil { 130 return a.Causes 131 } 132 } 133 return nil 134 } 135 136 func (q *Queue) Poll(ctx context.Context) (int, error) { 137 response, err := q.Jenkins.Requester.GetJSON(ctx, q.Base, q.Raw, nil) 138 if err != nil { 139 return 0, err 140 } 141 return response.StatusCode, nil 142 } 143 144 func (t *Task) Poll(ctx context.Context) (int, error) { 145 response, err := t.Jenkins.Requester.GetJSON(ctx, t.Base, t.Raw, nil) 146 if err != nil { 147 return 0, err 148 } 149 return response.StatusCode, nil 150 }