-
Notifications
You must be signed in to change notification settings - Fork 238
/
Copy pathwebhook.go
362 lines (329 loc) · 9.2 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
// 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 gitea
import (
"crypto/sha256"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"github.com/drone/go-scm/scm"
"github.com/drone/go-scm/scm/driver/internal/hmac"
)
type webhookService struct {
client *wrapper
}
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
switch req.Header.Get("X-Gitea-Event") {
case "push":
hook, err = s.parsePushHook(data)
case "create":
hook, err = s.parseCreateHook(data)
case "delete":
hook, err = s.parseDeleteHook(data)
case "issues":
hook, err = s.parseIssueHook(data)
case "issue_comment":
hook, err = s.parseIssueCommentHook(data)
case "pull_request":
hook, err = s.parsePullRequestHook(data)
default:
return nil, scm.ErrUnknownEvent
}
if err != nil {
return nil, err
}
// get the gitea 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
}
secret := req.FormValue("secret")
signature := req.Header.Get("X-Gitea-Signature")
// fail if no signature passed
if signature == "" && secret == "" {
return hook, scm.ErrSignatureInvalid
}
// test signature if header not set and secret is in payload
if signature == "" && secret != "" && secret != key {
return hook, scm.ErrSignatureInvalid
}
// test signature using header
if signature != "" && !hmac.Validate(sha256.New, data, []byte(key), signature) {
return hook, scm.ErrSignatureInvalid
}
return hook, nil
}
func (s *webhookService) parsePushHook(data []byte) (scm.Webhook, error) {
dst := new(pushHook)
err := json.Unmarshal(data, dst)
return convertPushHook(dst), err
}
func (s *webhookService) parseCreateHook(data []byte) (scm.Webhook, error) {
dst := new(createHook)
err := json.Unmarshal(data, dst)
switch dst.RefType {
case "tag":
return convertTagHook(dst, scm.ActionCreate), err
case "branch":
return convertBranchHook(dst, scm.ActionCreate), err
default:
return nil, scm.ErrUnknownEvent
}
}
func (s *webhookService) parseDeleteHook(data []byte) (scm.Webhook, error) {
dst := new(createHook)
err := json.Unmarshal(data, dst)
switch dst.RefType {
case "tag":
return convertTagHook(dst, scm.ActionDelete), err
case "branch":
return convertBranchHook(dst, scm.ActionDelete), err
default:
return nil, scm.ErrUnknownEvent
}
}
func (s *webhookService) parseIssueHook(data []byte) (scm.Webhook, error) {
dst := new(issueHook)
err := json.Unmarshal(data, dst)
return convertIssueHook(dst), err
}
func (s *webhookService) parseIssueCommentHook(data []byte) (scm.Webhook, error) {
dst := new(issueHook)
err := json.Unmarshal(data, dst)
if dst.Issue.PullRequest != nil {
return convertPullRequestCommentHook(dst), err
}
return convertIssueCommentHook(dst), err
}
func (s *webhookService) parsePullRequestHook(data []byte) (scm.Webhook, error) {
dst := new(pullRequestHook)
err := json.Unmarshal(data, dst)
return convertPullRequestHook(dst), err
}
//
// native data structures
//
type (
// gitea push webhook payload
pushHook struct {
Ref string `json:"ref"`
Before string `json:"before"`
After string `json:"after"`
Compare string `json:"compare_url"`
Commits []commit `json:"commits"`
Repository repository `json:"repository"`
Pusher user `json:"pusher"`
Sender user `json:"sender"`
}
// gitea create webhook payload
createHook struct {
Ref string `json:"ref"`
RefType string `json:"ref_type"`
Sha string `json:"sha"`
DefaultBranch string `json:"default_branch"`
Repository repository `json:"repository"`
Sender user `json:"sender"`
}
// gitea issue webhook payload
issueHook struct {
Action string `json:"action"`
Issue issue `json:"issue"`
Comment issueComment `json:"comment"`
Repository repository `json:"repository"`
Sender user `json:"sender"`
}
// gitea pull request webhook payload
pullRequestHook struct {
Action string `json:"action"`
Number int `json:"number"`
PullRequest pr `json:"pull_request"`
Repository repository `json:"repository"`
Sender user `json:"sender"`
}
)
//
// native data structure conversion
//
func convertTagHook(dst *createHook, action scm.Action) *scm.TagHook {
return &scm.TagHook{
Action: action,
Ref: scm.Reference{
Name: dst.Ref,
Sha: dst.Sha,
},
Repo: *convertRepository(&dst.Repository),
Sender: *convertUser(&dst.Sender),
}
}
func convertBranchHook(dst *createHook, action scm.Action) *scm.BranchHook {
return &scm.BranchHook{
Action: action,
Ref: scm.Reference{
Name: dst.Ref,
},
Repo: *convertRepository(&dst.Repository),
Sender: *convertUser(&dst.Sender),
}
}
func convertPushHook(dst *pushHook) *scm.PushHook {
if len(dst.Commits) > 0 {
var commits []scm.Commit
for _, c := range dst.Commits {
commits = append(commits,
scm.Commit{
Sha: c.ID,
Message: c.Message,
Link: c.URL,
Author: scm.Signature{
Login: c.Author.Username,
Email: c.Author.Email,
Name: c.Author.Name,
Date: c.Timestamp,
},
Committer: scm.Signature{
Login: c.Committer.Username,
Email: c.Committer.Email,
Name: c.Committer.Name,
Date: c.Timestamp,
},
})
}
return &scm.PushHook{
Ref: dst.Ref,
Before: dst.Before,
Commit: scm.Commit{
Sha: dst.After,
Message: dst.Commits[0].Message,
Link: dst.Compare,
Author: scm.Signature{
Login: dst.Commits[0].Author.Username,
Email: dst.Commits[0].Author.Email,
Name: dst.Commits[0].Author.Name,
Date: dst.Commits[0].Timestamp,
},
Committer: scm.Signature{
Login: dst.Commits[0].Committer.Username,
Email: dst.Commits[0].Committer.Email,
Name: dst.Commits[0].Committer.Name,
Date: dst.Commits[0].Timestamp,
},
},
Commits: commits,
Repo: *convertRepository(&dst.Repository),
Sender: *convertUser(&dst.Sender),
}
} else {
return &scm.PushHook{
Ref: dst.Ref,
Commit: scm.Commit{
Sha: dst.After,
Link: dst.Compare,
Author: scm.Signature{
Login: dst.Pusher.Login,
Email: dst.Pusher.Email,
Name: dst.Pusher.Fullname,
},
Committer: scm.Signature{
Login: dst.Pusher.Login,
Email: dst.Pusher.Email,
Name: dst.Pusher.Fullname,
},
},
Repo: *convertRepository(&dst.Repository),
Sender: *convertUser(&dst.Sender),
}
}
}
func convertPullRequestHook(dst *pullRequestHook) *scm.PullRequestHook {
return &scm.PullRequestHook{
Action: convertAction(dst.Action),
PullRequest: scm.PullRequest{
Number: dst.PullRequest.Number,
Title: dst.PullRequest.Title,
Body: dst.PullRequest.Body,
Closed: dst.PullRequest.State == "closed",
Author: scm.User{
Login: dst.PullRequest.User.Login,
Email: dst.PullRequest.User.Email,
Avatar: dst.PullRequest.User.Avatar,
},
Merged: dst.PullRequest.Merged,
// Created: nil,
// Updated: nil,
Source: dst.PullRequest.Head.Name,
Target: dst.PullRequest.Base.Name,
Fork: dst.PullRequest.Head.Repo.FullName,
Link: dst.PullRequest.HTMLURL,
Ref: fmt.Sprintf("refs/pull/%d/head", dst.PullRequest.Number),
Sha: dst.PullRequest.Head.Sha,
},
Repo: *convertRepository(&dst.Repository),
Sender: *convertUser(&dst.Sender),
}
}
func convertPullRequestCommentHook(dst *issueHook) *scm.PullRequestCommentHook {
return &scm.PullRequestCommentHook{
Action: convertAction(dst.Action),
PullRequest: *convertPullRequestFromIssue(&dst.Issue),
Comment: *convertIssueComment(&dst.Comment),
Repo: *convertRepository(&dst.Repository),
Sender: *convertUser(&dst.Sender),
}
}
func convertIssueHook(dst *issueHook) *scm.IssueHook {
return &scm.IssueHook{
Action: convertAction(dst.Action),
Issue: *convertIssue(&dst.Issue),
Repo: *convertRepository(&dst.Repository),
Sender: *convertUser(&dst.Sender),
}
}
func convertIssueCommentHook(dst *issueHook) *scm.IssueCommentHook {
return &scm.IssueCommentHook{
Action: convertAction(dst.Action),
Issue: *convertIssue(&dst.Issue),
Comment: *convertIssueComment(&dst.Comment),
Repo: *convertRepository(&dst.Repository),
Sender: *convertUser(&dst.Sender),
}
}
func convertAction(src string) (action scm.Action) {
switch src {
case "create", "created":
return scm.ActionCreate
case "delete", "deleted":
return scm.ActionDelete
case "update", "updated", "edit", "edited":
return scm.ActionUpdate
case "open", "opened":
return scm.ActionOpen
case "reopen", "reopened":
return scm.ActionReopen
case "close", "closed":
return scm.ActionClose
case "label", "labeled":
return scm.ActionLabel
case "unlabel", "unlabeled":
return scm.ActionUnlabel
case "merge", "merged":
return scm.ActionMerge
case "synchronize", "synchronized":
return scm.ActionSync
default:
return
}
}