-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
server.go
277 lines (256 loc) · 7.51 KB
/
server.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
/*
Copyright 2016 The Kubernetes Authors.
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 hook
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/sirupsen/logrus"
"k8s.io/test-infra/prow/config"
"k8s.io/test-infra/prow/github"
"k8s.io/test-infra/prow/githubeventserver"
_ "k8s.io/test-infra/prow/hook/plugin-imports"
"k8s.io/test-infra/prow/plugins"
)
// Server implements http.Handler. It validates incoming GitHub webhooks and
// then dispatches them to the appropriate plugins.
type Server struct {
ClientAgent *plugins.ClientAgent
Plugins *plugins.ConfigAgent
ConfigAgent *config.Agent
TokenGenerator func() []byte
Metrics *githubeventserver.Metrics
RepoEnabled func(org, repo string) bool
// c is an http client used for dispatching events
// to external plugin services.
c http.Client
// Tracks running handlers for graceful shutdown
wg sync.WaitGroup
}
// ServeHTTP validates an incoming webhook and puts it into the event channel.
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
eventType, eventGUID, payload, ok, resp := github.ValidateWebhook(w, r, s.TokenGenerator)
if counter, err := s.Metrics.ResponseCounter.GetMetricWithLabelValues(strconv.Itoa(resp)); err != nil {
logrus.WithFields(logrus.Fields{
"status-code": resp,
}).WithError(err).Error("Failed to get metric for reporting webhook status code")
} else {
counter.Inc()
}
if !ok {
return
}
fmt.Fprint(w, "Event received. Have a nice day.")
if err := s.demuxEvent(eventType, eventGUID, payload, r.Header); err != nil {
logrus.WithError(err).Error("Error parsing event.")
}
}
func (s *Server) demuxEvent(eventType, eventGUID string, payload []byte, h http.Header) error {
l := logrus.WithFields(
logrus.Fields{
eventTypeField: eventType,
github.EventGUID: eventGUID,
},
)
// We don't want to fail the webhook due to a metrics error.
if counter, err := s.Metrics.WebhookCounter.GetMetricWithLabelValues(eventType); err != nil {
l.WithError(err).Warn("Failed to get metric for eventType " + eventType)
} else {
counter.Inc()
}
var srcRepo string
switch eventType {
case "issues":
var i github.IssueEvent
if err := json.Unmarshal(payload, &i); err != nil {
return err
}
i.GUID = eventGUID
srcRepo = i.Repo.FullName
if s.RepoEnabled(i.Repo.Owner.Login, i.Repo.Name) {
s.wg.Add(1)
go s.handleIssueEvent(l, i)
}
case "issue_comment":
var ic github.IssueCommentEvent
if err := json.Unmarshal(payload, &ic); err != nil {
return err
}
ic.GUID = eventGUID
srcRepo = ic.Repo.FullName
if s.RepoEnabled(ic.Repo.Owner.Login, ic.Repo.Name) {
s.wg.Add(1)
go s.handleIssueCommentEvent(l, ic)
}
case "pull_request":
var pr github.PullRequestEvent
if err := json.Unmarshal(payload, &pr); err != nil {
return err
}
pr.GUID = eventGUID
srcRepo = pr.Repo.FullName
if s.RepoEnabled(pr.Repo.Owner.Login, pr.Repo.Name) {
s.wg.Add(1)
go s.handlePullRequestEvent(l, pr)
}
case "pull_request_review":
var re github.ReviewEvent
if err := json.Unmarshal(payload, &re); err != nil {
return err
}
re.GUID = eventGUID
srcRepo = re.Repo.FullName
if s.RepoEnabled(re.Repo.Owner.Login, re.Repo.Name) {
s.wg.Add(1)
go s.handleReviewEvent(l, re)
}
case "pull_request_review_comment":
var rce github.ReviewCommentEvent
if err := json.Unmarshal(payload, &rce); err != nil {
return err
}
rce.GUID = eventGUID
srcRepo = rce.Repo.FullName
if s.RepoEnabled(rce.Repo.Owner.Login, rce.Repo.Name) {
s.wg.Add(1)
go s.handleReviewCommentEvent(l, rce)
}
case "push":
var pe github.PushEvent
if err := json.Unmarshal(payload, &pe); err != nil {
return err
}
pe.GUID = eventGUID
srcRepo = pe.Repo.FullName
if s.RepoEnabled(pe.Repo.Owner.Login, pe.Repo.Name) {
s.wg.Add(1)
go s.handlePushEvent(l, pe)
}
case "status":
var se github.StatusEvent
if err := json.Unmarshal(payload, &se); err != nil {
return err
}
se.GUID = eventGUID
srcRepo = se.Repo.FullName
if s.RepoEnabled(se.Repo.Owner.Login, se.Repo.Name) {
s.wg.Add(1)
go s.handleStatusEvent(l, se)
}
default:
l.Debug("Ignoring unhandled event type. (Might still be handled by external plugins.)")
}
// Demux events only to external plugins that require this event.
if external := s.needDemux(eventType, srcRepo); len(external) > 0 {
go s.demuxExternal(l, external, payload, h)
}
return nil
}
// needDemux returns whether there are any external plugins that need to
// get the present event.
func (s *Server) needDemux(eventType, orgRepo string) []plugins.ExternalPlugin {
var matching []plugins.ExternalPlugin
split := strings.Split(orgRepo, "/")
srcOrg := split[0]
var srcRepo string
if len(split) > 1 {
srcRepo = split[1]
}
if !s.RepoEnabled(srcOrg, srcRepo) {
return nil
}
for repo, plugins := range s.Plugins.Config().ExternalPlugins {
// Make sure the repositories match
if repo != orgRepo && repo != srcOrg {
continue
}
// Make sure the events match
for _, p := range plugins {
if len(p.Events) == 0 {
matching = append(matching, p)
} else {
for _, et := range p.Events {
if et != eventType {
continue
}
matching = append(matching, p)
break
}
}
}
}
return matching
}
// demuxExternal dispatches the provided payload to the external plugins.
func (s *Server) demuxExternal(l *logrus.Entry, externalPlugins []plugins.ExternalPlugin, payload []byte, h http.Header) {
h.Set("User-Agent", "ProwHook")
for _, p := range externalPlugins {
s.wg.Add(1)
go func(p plugins.ExternalPlugin) {
defer s.wg.Done()
if err := s.dispatch(p.Endpoint, payload, h); err != nil {
l.WithError(err).WithField("external-plugin", p.Name).Error("Error dispatching event to external plugin.")
} else {
l.WithField("external-plugin", p.Name).Info("Dispatched event to external plugin")
}
}(p)
}
}
// dispatch creates a new request using the provided payload and headers
// and dispatches the request to the provided endpoint.
func (s *Server) dispatch(endpoint string, payload []byte, h http.Header) error {
req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(payload))
if err != nil {
return err
}
req.Header = h
resp, err := s.do(req)
if err != nil {
return err
}
defer resp.Body.Close()
rb, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return fmt.Errorf("response has status %q and body %q", resp.Status, string(rb))
}
return nil
}
// GracefulShutdown implements a graceful shutdown protocol. It handles all requests sent before
// receiving the shutdown signal.
func (s *Server) GracefulShutdown() {
s.wg.Wait() // Handle remaining requests
}
func (s *Server) do(req *http.Request) (*http.Response, error) {
var resp *http.Response
var err error
backoff := 100 * time.Millisecond
maxRetries := 5
for retries := 0; retries < maxRetries; retries++ {
resp, err = s.c.Do(req)
if err == nil {
break
}
time.Sleep(backoff)
backoff *= 2
}
return resp, err
}