/
jenkinsbee.go
215 lines (193 loc) 路 5.04 KB
/
jenkinsbee.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
/*
* Copyright (C) 2014 Daniel 'grindhold' Brendle
* 2014-2017 Christian Muehlhaeuser
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Daniel 'grindhold' Brendle <grindhold@skarphed.org>
* Christian Muehlhaeuser <muesli@gmail.com>
*/
// Package jenkinsbee is a Bee that can interface with a Jenkins server.
package jenkinsbee
import (
"encoding/json"
"io/ioutil"
"net/http"
"time"
"github.com/muesli/beehive/bees"
)
// JenkinsBee is a Bee that can interface with a Jenkins server.
type JenkinsBee struct {
bees.Bee
url string
user string
password string
Jobs map[string]Job `json:"jobs"`
eventChan chan bees.Event
}
type report struct {
Jobs []Job `json:"jobs"`
}
// Job represents the JSON API response for a Jenkins job.
type Job struct {
Name string `json:"name"`
URL string `json:"url"`
Color string `json:"color"`
LastBuild LastBuild `json:"lastBuild"`
}
type LastBuild struct {
Building bool `json:"building"`
}
func (mod *JenkinsBee) announceStatusChange(j Job) {
event := bees.Event{
Bee: mod.Name(),
Name: "status_change",
Options: []bees.Placeholder{
{
Name: "name",
Type: "string",
Value: j.Name,
},
{
Name: "url",
Type: "string",
Value: j.URL,
},
{
Name: "status",
Type: "string",
Value: j.Color,
},
},
}
mod.eventChan <- event
}
func (mod *JenkinsBee) announceBuildStateChange(j Job) {
event := bees.Event{
Bee: mod.Name(),
Name: "build_status_change",
Options: []bees.Placeholder{
{
Name: "name",
Type: "string",
Value: j.Name,
},
{
Name: "url",
Type: "string",
Value: j.URL,
},
{
Name: "status",
Type: "string",
Value: j.Color,
},
{
Name: "building",
Type: "bool",
Value: j.LastBuild.Building,
},
},
}
mod.eventChan <- event
}
func (mod *JenkinsBee) poll() {
request, err := http.NewRequest("GET", mod.url+"/api/json?tree=jobs[name,url,color,lastBuild[building]]", nil)
if err != nil {
mod.LogErrorf("Could not build request: %s", err)
return
}
request.SetBasicAuth(mod.user, mod.password)
client := http.Client{}
resp, err := client.Do(request)
if err != nil {
mod.LogErrorf("Could not call API on %s/api/json: %s", mod.url, err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
mod.LogErrorf("Could not read data from API: %s", err)
return
}
rep := new(report)
err = json.Unmarshal(body, &rep)
if err != nil {
mod.LogErrorf("Failed to unmarshal JSON: %s", err)
return
}
jobmap := make(map[string]Job)
for job := range rep.Jobs {
if oldState, ok := mod.Jobs[rep.Jobs[job].Name]; !ok {
// There is no record of this job
mod.announceStatusChange(rep.Jobs[job])
} else {
// There exists a record of this job
if oldState.Color != rep.Jobs[job].Color {
// The status is different from last time
mod.announceStatusChange(rep.Jobs[job])
}
if oldState.LastBuild.Building != rep.Jobs[job].LastBuild.Building {
mod.announceBuildStateChange(rep.Jobs[job])
}
}
jobmap[rep.Jobs[job].Name] = rep.Jobs[job]
}
mod.Jobs = jobmap
}
// Run executes the Bee's event loop.
func (mod *JenkinsBee) Run(cin chan bees.Event) {
mod.eventChan = cin
for {
select {
case <-mod.SigChan:
return
case <-time.After(time.Duration(10 * time.Second)):
mod.poll()
}
}
}
func (mod *JenkinsBee) triggerBuild(jobname string) {
client := http.Client{}
request, err := http.NewRequest("GET", mod.url+"/job/"+jobname+"/build", nil)
if err != nil {
mod.LogErrorf("Could not build request: %s", err)
return
}
request.SetBasicAuth(mod.user, mod.password)
if _, err := client.Do(request); err != nil {
mod.LogErrorf("Could not trigger build: %s", err)
}
}
// Action triggers the action passed to it.
func (mod *JenkinsBee) Action(action bees.Action) []bees.Placeholder {
outs := []bees.Placeholder{}
switch action.Name {
case "trigger":
jobname := ""
action.Options.Bind("job", &jobname)
mod.triggerBuild(jobname)
default:
panic("Unknown action triggered in " + mod.Name() + ": " + action.Name)
}
return outs
}
// ReloadOptions parses the config options and initializes the Bee.
func (mod *JenkinsBee) ReloadOptions(options bees.BeeOptions) {
mod.SetOptions(options)
options.Bind("url", &mod.url)
options.Bind("user", &mod.user)
options.Bind("password", &mod.password)
}