forked from docker-archive/leeroy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
316 lines (276 loc) · 7.41 KB
/
handlers.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"strconv"
"leeroy/github"
"leeroy/jenkins"
log "github.com/Sirupsen/logrus"
"github.com/Sirupsen/logrus"
"github.com/crosbymichael/octokat"
"github.com/pkg/errors"
)
func pingHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "pong")
return
}
func jenkinsHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
fmt.Errorf("%q is not a valid method", r.Method)
w.WriteHeader(405)
return
}
// decode the body
decoder := json.NewDecoder(r.Body)
var j jenkins.JenkinsResponse
if err := decoder.Decode(&j); err != nil {
log.Errorf("decoding the jenkins request as json failed: %v", err)
return
}
log.Infof("Received Jenkins notification for %s %d (%s): %s", j.Name, j.Build.Number, j.Build.Url, j.Build.Phase)
// if the phase is not started or completed
// we don't care
if j.Build.Phase != "STARTED" && j.Build.Phase != "COMPLETED" {
return
}
// get the status for github
// and create a status description
desc := fmt.Sprintf("Jenkins build %s %d", j.Name, j.Build.Number)
var state string
if j.Build.Phase == "STARTED" {
state = "pending"
desc += " is running"
j.Build.Url += "console"
} else {
switch j.Build.Status {
case "SUCCESS":
state = "success"
desc += " has succeeded"
case "FAILURE":
state = "failure"
desc += " has failed"
case "UNSTABLE":
state = "failure"
desc += " was unstable"
case "ABORTED":
state = "error"
desc += " has encountered an error"
default:
log.Errorf("Did not understand %q build status. Aborting.", j.Build.Status)
return
}
}
// get the build
build, err := config.getBuildByJob(j.Name)
if err != nil {
log.Error(err)
return
}
// update the github status
if err := config.updateGithubStatus(j.Build.Parameters.GitBaseRepo, build.Context, j.Build.Parameters.GitSha, state, desc, j.Build.Url); err != nil {
log.Error(err)
}
if state == "success" {
for _, DownstreamBuild := range build.DownstreamBuilds {
BuildDownstream, err := config.getBuildByContextAndRepo(DownstreamBuild, j.Build.Parameters.GitBaseRepo)
if err != nil {
log.Error(err)
w.WriteHeader(500)
return
}
pr_number, _ := strconv.Atoi(j.Build.Parameters.PR)
if err := config.scheduleJenkinsDownstreamBuild(BuildDownstream.Repo, j.Build.Parameters.GitHeadRepo, pr_number, BuildDownstream, j.Build.Parameters.GitSha); err != nil {
log.Error(err)
w.WriteHeader(500)
}
}
}
return
}
func githubHandler(w http.ResponseWriter, r *http.Request) {
event := r.Header.Get("X-GitHub-Event")
switch event {
case "":
log.Error("Got GitHub notification without a type")
return
case "ping":
w.WriteHeader(200)
return
case "pull_request":
log.Debugf("Got a pull request hook")
default:
fmt.Errorf("Got unknown GitHub notification event type: %s", event)
return
}
// parse the pull request
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Errorf("Error reading github handler body: %v", err)
w.WriteHeader(500)
return
}
prHook, err := octokat.ParsePullRequestHook(body)
if err != nil {
log.Errorf("Error parsing hook: %v", err)
w.WriteHeader(500)
return
}
pr := prHook.PullRequest
baseRepo := fmt.Sprintf("%s/%s", pr.Base.Repo.Owner.Login, pr.Base.Repo.Name)
log.Infof("Received GitHub pull request notification for %s %d (%s): %s", baseRepo, pr.Number, pr.URL, prHook.Action)
// ignore everything we don't care about
if prHook.Action != "opened" && prHook.Action != "reopened" && prHook.Action != "synchronize" {
log.Debugf("Ignoring PR hook action %q", prHook.Action)
return
}
g := github.GitHub{
AuthToken: config.GHToken,
User: config.GHUser,
}
attempt, totalAttempts := 1, 5
delay := time.Second
retry:
pullRequest, err := g.LoadPullRequest(prHook)
if err != nil {
logrus.Errorf("Error loading the pull request (attempt %d/%d): %v", attempt, totalAttempts, err)
if attempt <= totalAttempts && errors.Cause(err).Error() == "Not Found" {
time.Sleep(delay)
attempt++
delay *= 2
goto retry
}
w.WriteHeader(500)
return
}
mergeable, err := g.IsMergeable(pullRequest)
if err != nil {
logrus.Errorf("Error checking if PR is mergeable: %v", err)
w.WriteHeader(500)
return
}
// PR is not mergeable, so don't start the build
if !mergeable {
logrus.Errorf("Unmergeable PR for %s #%d. Aborting build", baseRepo, pr.Number)
w.WriteHeader(200)
return
}
// get the builds
builds, err := config.getBuilds(baseRepo, false)
if err != nil {
log.Error(err)
w.WriteHeader(500)
return
}
// cancel existing jenkins builds associated with the job
for _, build := range builds {
if err := config.cancelJenkinsBuild(baseRepo, pr.Number, build); err != nil {
log.Error(err)
w.WriteHeader(500)
}
}
// schedule the jenkins builds
for _, build := range builds {
if !build.Downstream {
if err := config.scheduleJenkinsBuild(baseRepo, pr.Number, build); err != nil {
log.Error(err)
w.WriteHeader(500)
}
}
}
return
}
type requestBuild struct {
Number int `json:"number"`
Repo string `json:"repo"`
Context string `json:"context"`
}
func customBuildHandler(w http.ResponseWriter, r *http.Request) {
// setup auth
user, pass, ok := r.BasicAuth()
if !ok {
w.WriteHeader(401)
return
}
if user != config.User && pass != config.Pass {
w.WriteHeader(401)
return
}
if r.Method != "POST" {
fmt.Errorf("%q is not a valid method", r.Method)
w.WriteHeader(405)
return
}
// decode the body
decoder := json.NewDecoder(r.Body)
var b requestBuild
if err := decoder.Decode(&b); err != nil {
log.Errorf("decoding the retry request as json failed: %v", err)
w.WriteHeader(500)
return
}
// get the build
build, err := config.getBuildByContextAndRepo(b.Context, b.Repo)
if err != nil {
log.Error(err)
w.WriteHeader(500)
return
}
// schedule the jenkins build
if err := config.scheduleJenkinsBuild(b.Repo, b.Number, build); err != nil {
w.WriteHeader(500)
log.Error(err)
return
}
w.WriteHeader(204)
return
}
func cronBuildHandler(w http.ResponseWriter, r *http.Request) {
// setup auth
user, pass, ok := r.BasicAuth()
if !ok {
w.WriteHeader(401)
return
}
if user != config.User && pass != config.Pass {
w.WriteHeader(401)
return
}
if r.Method != "POST" {
fmt.Errorf("%q is not a valid method", r.Method)
w.WriteHeader(405)
return
}
// decode the body
decoder := json.NewDecoder(r.Body)
var b requestBuild
if err := decoder.Decode(&b); err != nil {
log.Errorf("decoding the retry request as json failed: %v", err)
w.WriteHeader(500)
return
}
// get the build
build, err := config.getBuildByContextAndRepo(b.Context, b.Repo)
if err != nil {
log.Error(err)
w.WriteHeader(500)
return
}
// get PRs that have failed for the context
nums, err := config.getFailedPRs(b.Context, b.Repo)
if err != nil {
log.Error(err)
w.WriteHeader(500)
return
}
for _, prNum := range nums {
// schedule the jenkins build
if err := config.scheduleJenkinsBuild(b.Repo, prNum, build); err != nil {
log.Error(err)
}
}
w.WriteHeader(204)
return
}