forked from muesli/beehive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtravisbee.go
233 lines (210 loc) · 5.6 KB
/
travisbee.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
/*
* Copyright (C) 2019 CalmBit
* 2014-2019 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:
* CalmBit <[email protected]>
* Christian Muehlhaeuser <[email protected]>
*/
// Package travisbee is a bee for monitoring and reacting to the status of
// TravisCI builds.
package travisbee
import (
"context"
"time"
"github.com/muesli/beehive/bees"
"github.com/shuheiktgw/go-travis"
)
// TravisBee is a bee for monitoring and reacting to the status of
// TravisCI builds.
type TravisBee struct {
bees.Bee
eventChan chan bees.Event
client *travis.Client
apiToken string
builds map[uint]BuildTracker
}
// BuildTracker is a marker struct to denote "tracked builds" that TravisBee
// cares about.
type BuildTracker struct {
id uint
state string
lastTime time.Time
}
// Run executes the Bee's event loop.
func (mod *TravisBee) Run(eventChan chan bees.Event) {
mod.eventChan = eventChan
mod.client = travis.NewClient(travis.ApiOrgUrl, mod.apiToken)
mod.builds = make(map[uint]BuildTracker)
since := time.Now()
timeout := time.Duration(time.Second * 10)
for {
select {
case <-mod.SigChan:
return
case <-time.After(timeout):
mod.getBuilds(since)
}
since = time.Now()
timeout = time.Duration(time.Minute)
}
}
func (mod *TravisBee) getBuilds(since time.Time) {
var opt = travis.BuildsOption{Limit: 10, Offset: 0, SortBy: "started_at:desc"}
builds, _, err := mod.client.Builds.List(context.Background(), &opt)
if err != nil {
mod.LogFatal("Error getting builds from travis-ci: ", err)
}
for i, currentBuild := range builds {
if b, ok := mod.builds[currentBuild.Id]; ok {
b.lastTime = time.Now()
if b.state != currentBuild.State {
mod.handleStateChange(&b, currentBuild)
b.state = currentBuild.State
}
mod.builds[currentBuild.Id] = b
} else {
mod.LogDebugf("[%d] %d - %s / %s", i, currentBuild.Id, currentBuild.StartedAt, currentBuild.State)
// If a build was just barely created (i.e. hasn't moved into any other
// stage yet) then we can't even get a started_at time - instead, let's
// just make it a new build, figuring it's obviously something we havent
// seen before.
if currentBuild.State == "created" {
mod.handleNewBuild(currentBuild, since)
} else {
t, err := time.Parse(time.RFC3339, currentBuild.StartedAt)
if err != nil {
mod.LogErrorf("Error parsing time %s - %v", currentBuild.StartedAt, err)
}
if t.After(since) {
mod.handleNewBuild(currentBuild, since)
}
}
}
}
}
func (mod *TravisBee) handleNewBuild(build *travis.Build, since time.Time) {
mod.builds[build.Id] = BuildTracker{id: build.Id, state: build.State, lastTime: time.Now()}
mod.LogDebugf("Tracking build %d - state %s", build.Id, build.State)
ev := bees.Event{
Bee: mod.Name(),
Name: "build_started",
Options: []bees.Placeholder{
{
Name: "id",
Type: "uint",
Value: build.Id,
},
{
Name: "state",
Type: "string",
Value: build.State,
},
{
Name: "repo_slug",
Type: "string",
Value: build.Repository.Slug,
},
{
Name: "duration",
Type: "uint",
Value: build.Duration,
},
},
}
mod.eventChan <- ev
}
func (mod *TravisBee) handleStateChange(bt *BuildTracker, build *travis.Build) {
mod.LogDebugf("State changed! (was %s, is now %s)", bt.state, build.State)
ev := bees.Event{
Bee: mod.Name(),
Name: "build_status_change",
Options: []bees.Placeholder{
{
Name: "id",
Type: "uint",
Value: build.Id,
},
{
Name: "state",
Type: "string",
Value: build.State,
},
{
Name: "last_state",
Type: "string",
Value: bt.state,
},
{
Name: "repo_slug",
Type: "string",
Value: build.Repository.Slug,
},
{
Name: "duration",
Type: "uint",
Value: build.Duration,
},
},
}
mod.eventChan <- ev
if build.State == "canceled" || build.State == "passed" ||
build.State == "failed" || build.State == "errored" {
mod.handleBuildFinish(bt, build)
}
}
func (mod *TravisBee) handleBuildFinish(bt *BuildTracker, build *travis.Build) {
mod.LogDebugf("Build %d has finished with state %s", build.Id, build.State)
ev := bees.Event{
Bee: mod.Name(),
Name: "build_finished",
Options: []bees.Placeholder{
{
Name: "id",
Type: "uint",
Value: build.Id,
},
{
Name: "state",
Type: "string",
Value: build.State,
},
{
Name: "previous_state",
Type: "string",
Value: build.PreviousState,
},
{
Name: "repo_slug",
Type: "string",
Value: build.Repository.Slug,
},
{
Name: "duration",
Type: "uint",
Value: build.Duration,
},
},
}
mod.eventChan <- ev
mod.LogDebugf("Now untracking build %d", bt.id)
delete(mod.builds, bt.id)
}
// ReloadOptions parses the config options and initializes the Bee.
func (mod *TravisBee) ReloadOptions(options bees.BeeOptions) {
mod.SetOptions(options)
options.Bind("api_key", &mod.apiToken)
}