forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 86
/
webhook.go
382 lines (348 loc) · 9.91 KB
/
webhook.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// Copyright 2017 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package stash
import (
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
"time"
"github.com/jenkins-x/go-scm/pkg/hmac"
"github.com/jenkins-x/go-scm/scm"
)
// TODO(bradrydzewski) push hook does not include commit message
// TODO(bradrydzewski) push hook does not include commit link
// TODO(bradrydzewski) push hook does not include repository git+http link
// TODO(bradrydzewski) push hook does not include repository git+ssh link
// TODO(bradrydzewski) push hook does not include repository html link
// TODO(bradrydzewski) missing pull request synchrnoized webhook. See https://jira.atlassian.com/browse/BSERV-10279
// TODO(bradrydzewski) pr hook does not include repository git+http link
// TODO(bradrydzewski) pr hook does not include repository git+ssh link
// TODO(bradrydzewski) pr hook does not include repository html link
type webhookService struct {
client *wrapper
}
// Parse for the bitbucket server webhook payloads see: https://confluence.atlassian.com/bitbucketserver/event-payload-938025882.html
func (s *webhookService) Parse(req *http.Request, fn scm.SecretFunc) (scm.Webhook, error) {
data, err := ioutil.ReadAll(
io.LimitReader(req.Body, 10000000),
)
if err != nil {
return nil, err
}
var hook scm.Webhook
event := req.Header.Get("X-Event-Key")
switch event {
case "repo:refs_changed":
hook, err = s.parsePushHook(data)
case "pr:opened", "pr:declined", "pr:merged", "pr:from_ref_updated", "pr:modified":
hook, err = s.parsePullRequest(data)
case "pr:comment:added", "pr:comment:edited":
hook, err = s.parsePullRequestComment(data)
case "pr:reviewer:approved", "pr:reviewer:unapproved", "pr:reviewer:needs_work":
hook, err = s.parsePullRequestApproval(data)
default:
return nil, scm.UnknownWebhook{event}
}
if err != nil {
return nil, err
}
if hook == nil {
return nil, nil
}
// get the gogs signature key to verify the payload
// signature. If no key is provided, no validation
// is performed.
key, err := fn(hook)
if err != nil {
return hook, err
} else if key == "" {
return hook, nil
}
sig := req.Header.Get("X-Hub-Signature")
if !hmac.ValidatePrefix(data, []byte(key), sig) {
return hook, scm.ErrSignatureInvalid
}
return hook, nil
}
func (s *webhookService) parsePushHook(data []byte) (scm.Webhook, error) {
dst := new(pushHook)
err := json.Unmarshal(data, dst)
if err != nil {
return nil, err
}
if len(dst.Changes) == 0 {
return nil, errors.New("Push hook has empty changeset")
}
change := dst.Changes[0]
switch {
case change.Ref.Type == "BRANCH" && change.Type != "UPDATE":
return convertBranchHook(dst), nil
case change.Ref.Type == "TAG":
return convertTagHook(dst), nil
default:
return convertPushHook(dst), err
}
}
func (s *webhookService) parsePullRequest(data []byte) (scm.Webhook, error) {
src := new(pullRequestHook)
err := json.Unmarshal(data, src)
if err != nil {
return nil, err
}
dst := convertPullRequestHook(src)
switch src.EventKey {
case "pr:opened":
dst.Action = scm.ActionOpen
case "pr:declined":
dst.Action = scm.ActionClose
case "pr:merged":
dst.Action = scm.ActionMerge
case "pr:from_ref_updated":
dst.Action = scm.ActionSync
case "pr:modified":
dst.Action = scm.ActionUpdate
default:
return nil, nil
}
return dst, nil
}
func (s *webhookService) parsePullRequestComment(data []byte) (scm.Webhook, error) {
src := new(pullRequestCommentHook)
err := json.Unmarshal(data, src)
if err != nil {
return nil, err
}
dst := convertPullRequestCommentHook(src)
return dst, nil
}
func (s *webhookService) parsePullRequestApproval(data []byte) (scm.Webhook, error) {
src := new(pullRequestApprovalHook)
err := json.Unmarshal(data, src)
if err != nil {
return nil, err
}
dst := convertPullRequestApprovalHook(src)
switch src.EventKey {
case "pr:reviewer:approved":
dst.Action = scm.ActionSubmitted
case "pr:reviewer:unapproved":
dst.Action = scm.ActionDismissed
case "pr:reviewer:needs_work":
dst.Action = scm.ActionEdited
default:
return nil, nil
}
return dst, nil
}
//
// native data structures
//
type pushHook struct {
EventKey string `json:"eventKey"`
Date string `json:"date"`
Actor *user `json:"actor"`
Repository *repository `json:"repository"`
Changes []*change `json:"changes"`
}
type pullRequestHook struct {
EventKey string `json:"eventKey"`
Date string `json:"date"`
Actor *user `json:"actor"`
PullRequest *pullRequest `json:"pullRequest"`
PreviousFromHash string `json:"previousFromHash"`
PreviousTitle string `json:"previousTitle"`
PreviousDescription string `json:"previousDescription"`
PreviousTarget interface{} `json:"previousTarget"`
}
type pullRequestCommentHook struct {
EventKey string `json:"eventKey"`
Date string `json:"date"`
Author *user `json:"author"`
PullRequest *pullRequest `json:"pullRequest"`
Comment *prComment `json:"comment"`
}
type prComment struct {
ID int `json:"id"`
Version int `json:"version"`
Text string `json:"text"`
Author *user `json:"author"`
CreatedAt int64 `json:"createdDate"`
UpdatedAt int64 `json:"updatedDate"`
}
type change struct {
Ref struct {
ID string `json:"id"`
DisplayID string `json:"displayId"`
Type string `json:"type"`
} `json:"ref"`
RefID string `json:"refId"`
FromHash string `json:"fromHash"`
ToHash string `json:"toHash"`
Type string `json:"type"`
}
type pullRequestApprovalHook struct {
EventKey string `json:"eventKey"`
Date string `json:"date"`
Actor *user `json:"actor"`
PullRequest *pullRequest `json:"pullRequest"`
Participant *prUser `json:"participant"`
PreviousStatus string `json:"previousParticipant"`
}
//
// push hooks
//
func convertPushHook(src *pushHook) *scm.PushHook {
change := src.Changes[0]
repo := convertRepository(src.Repository)
sender := convertUser(src.Actor)
signer := convertSignature(src.Actor)
signer.Date, _ = time.Parse("2006-01-02T15:04:05+0000", src.Date)
return &scm.PushHook{
Ref: change.RefID,
Commit: scm.Commit{
Sha: change.ToHash,
Message: "",
Link: "",
Author: signer,
Committer: signer,
},
Repo: *repo,
Sender: *sender,
}
}
func convertTagHook(src *pushHook) *scm.TagHook {
change := src.Changes[0]
sender := convertUser(src.Actor)
repo := convertRepository(src.Repository)
dst := &scm.TagHook{
Ref: scm.Reference{
Name: change.Ref.DisplayID,
Sha: change.ToHash,
},
Action: scm.ActionCreate,
Repo: *repo,
Sender: *sender,
}
if change.Type == "DELETE" {
dst.Action = scm.ActionDelete
dst.Ref.Sha = change.FromHash
}
return dst
}
func convertBranchHook(src *pushHook) *scm.BranchHook {
change := src.Changes[0]
sender := convertUser(src.Actor)
repo := convertRepository(src.Repository)
dst := &scm.BranchHook{
Ref: scm.Reference{
Name: change.Ref.DisplayID,
Sha: change.ToHash,
},
Action: scm.ActionCreate,
Repo: *repo,
Sender: *sender,
}
if change.Type == "DELETE" {
dst.Action = scm.ActionDelete
dst.Ref.Sha = change.FromHash
}
return dst
}
func convertSignature(actor *user) scm.Signature {
return scm.Signature{
Name: actor.DisplayName,
Email: actor.EmailAddress,
Login: actor.Slug,
Avatar: avatarLink(actor.EmailAddress),
}
}
func convertPullRequestHook(src *pullRequestHook) *scm.PullRequestHook {
toRepo := convertRepository(&src.PullRequest.ToRef.Repository)
fromRepo := convertRepository(&src.PullRequest.FromRef.Repository)
pr := convertPullRequest(src.PullRequest)
sender := convertUser(src.Actor)
pr.Base.Repo = *toRepo
pr.Head.Repo = *fromRepo
if pr.Base.Ref == "" {
pr.Base.Ref = toRepo.Branch
}
if pr.Head.Ref == "" {
pr.Head.Ref = fromRepo.Branch
}
return &scm.PullRequestHook{
Action: scm.ActionOpen,
Repo: *toRepo,
PullRequest: *pr,
Sender: *sender,
}
}
func convertPullRequestCommentHook(src *pullRequestCommentHook) *scm.PullRequestCommentHook {
toRepo := convertRepository(&src.PullRequest.ToRef.Repository)
fromRepo := convertRepository(&src.PullRequest.FromRef.Repository)
pr := convertPullRequest(src.PullRequest)
author := src.Comment.Author
if author == nil {
author = src.Author
}
sender := convertUser(author)
pr.Base.Repo = *toRepo
pr.Head.Repo = *fromRepo
return &scm.PullRequestCommentHook{
Action: scm.ActionCreate,
Repo: *toRepo,
PullRequest: *pr,
Sender: *sender,
Comment: convertComment(src.Comment),
}
}
func convertComment(src *prComment) scm.Comment {
dst := scm.Comment{}
if src != nil {
dst.ID = src.ID
dst.Body = src.Text
author := convertUser(src.Author)
if author != nil {
dst.Author = *author
}
dst.Created = time.Unix(src.CreatedAt/1000, 0)
dst.Updated = time.Unix(src.UpdatedAt/1000, 0)
}
return dst
}
func convertPullRequestApprovalHook(src *pullRequestApprovalHook) *scm.ReviewHook {
toRepo := convertRepository(&src.PullRequest.ToRef.Repository)
fromRepo := convertRepository(&src.PullRequest.FromRef.Repository)
pr := convertPullRequest(src.PullRequest)
pr.Base.Repo = *toRepo
pr.Head.Repo = *fromRepo
if pr.Base.Ref == "" {
pr.Base.Ref = toRepo.Branch
}
if pr.Head.Ref == "" {
pr.Head.Ref = fromRepo.Branch
}
review := scm.Review{
State: convertReviewStateFromEvent(src.EventKey),
Author: *convertUser(&src.Participant.User),
}
return &scm.ReviewHook{
PullRequest: *pr,
Repo: *toRepo,
Review: review,
}
}
func convertReviewStateFromEvent(src string) string {
switch src {
case "pr:reviewer:approved":
return scm.ReviewStateApproved
case "pr:reviewer:unapproved":
return scm.ReviewStateDismissed
case "pr:reviewer:needs_work":
return scm.ReviewStateChangesRequested
default:
return scm.ReviewStatePending
}
}