-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
parse.go
313 lines (294 loc) · 9.77 KB
/
parse.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
// Copyright 2019 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by the Drone Non-Commercial License
// that can be found in the LICENSE file.
package parser
import (
"errors"
"fmt"
"net/http"
"net/http/httputil"
"os"
"strconv"
"strings"
"github.com/drone/drone/core"
"github.com/drone/go-scm/scm"
)
// TODO(bradrydzewski): stash, push hook missing link
// TODO(bradrydzewski): stash, tag hook missing timestamp
// TODO(bradrydzewski): stash, tag hook missing commit message
// TODO(bradrydzewski): stash, tag hook missing link
// TODO(bradrydzewski): stash, pull request hook missing link
// TODO(bradrydzewski): stash, hooks missing repository clone http url
// TODO(bradrydzewski): stash, hooks missing repository clone ssh url
// TODO(bradrydzewski): stash, hooks missing repository html link
// TODO(bradrydzewski): gogs, push hook missing author avatar, using sender instead.
// TODO(bradrydzewski): gogs, pull request hook missing commit sha.
// TODO(bradrydzewski): gogs, tag hook missing commit sha.
// TODO(bradrydzewski): gogs, sender missing Name field.
// TODO(bradrydzewski): gogs, push hook missing repository html url
// TODO(bradrydzewski): gitea, push hook missing author avatar, using sender instead.
// TODO(bradrydzewski): gitea, tag hook missing commit sha.
// TODO(bradrydzewski): gitea, sender missing Name field.
// TODO(bradrydzewski): gitea, push hook missing repository html url
// TODO(bradrydzewski): bitbucket, pull request hook missing author email.
// TODO(bradrydzewski): bitbucket, hooks missing defualt repository branch.
// TODO(bradrydzewski): github, push hook timestamp is negative value.
// TODO(bradrydzewski): github, pull request message is empty
// represents a deleted ref in the github webhook.
const emptyCommit = "0000000000000000000000000000000000000000"
// this is intended for local testing and instructs the handler
// to print the contents of the hook to stdout.
var debugPrintHook = false
func init() {
debugPrintHook, _ = strconv.ParseBool(
os.Getenv("DRONE_DEBUG_DUMP_HOOK"),
)
}
// New returns a new HookParser.
func New(client *scm.Client) core.HookParser {
return &parser{client}
}
type parser struct {
client *scm.Client
}
func (p *parser) Parse(req *http.Request, secretFunc func(string) string) (*core.Hook, *core.Repository, error) {
if debugPrintHook {
// if DRONE_DEBUG_DUMP_HOOK=true print the http.Request
// headers and body to stdout.
out, _ := httputil.DumpRequest(req, true)
os.Stderr.Write(out)
}
// callback function provides the webhook parser with
// a per-repository secret key used to verify the webhook
// payload signature for authenticity.
fn := func(webhook scm.Webhook) (string, error) {
if webhook == nil {
return "", errors.New("Invalid or malformed webhook")
}
repo := webhook.Repository()
slug := scm.Join(repo.Namespace, repo.Name)
secret := secretFunc(slug)
if secret == "" {
return secret, errors.New("Cannot find repository")
}
return secret, nil
}
payload, err := p.client.Webhooks.Parse(req, fn)
if err == scm.ErrUnknownEvent {
return nil, nil, nil
}
if err != nil {
return nil, nil, err
}
var repo *core.Repository
var hook *core.Hook
switch v := payload.(type) {
case *scm.PushHook:
// github sends push hooks when tags and branches are
// deleted. These hooks should be ignored.
if v.Commit.Sha == emptyCommit {
return nil, nil, nil
}
// github sends push hooks when tags are created. The
// push hook contains more information than the tag hook,
// so we choose to use the push hook for tags.
if strings.HasPrefix(v.Ref, "refs/tags/") {
hook = &core.Hook{
Trigger: core.TriggerHook, // core.TriggerHook
Event: core.EventTag,
Action: core.ActionCreate,
Link: v.Commit.Link,
Timestamp: v.Commit.Author.Date.Unix(),
Message: v.Commit.Message,
Before: v.Before,
After: v.Commit.Sha,
Ref: v.Ref,
Author: v.Commit.Author.Login,
AuthorName: v.Commit.Author.Name,
AuthorEmail: v.Commit.Author.Email,
AuthorAvatar: v.Commit.Author.Avatar,
Sender: v.Sender.Login,
}
} else {
hook = &core.Hook{
Trigger: core.TriggerHook, //core.TriggerHook,
Event: core.EventPush,
Link: v.Commit.Link,
Timestamp: v.Commit.Author.Date.Unix(),
Message: v.Commit.Message,
Before: v.Before,
After: v.Commit.Sha,
Ref: v.Ref,
Source: strings.TrimPrefix(v.Ref, "refs/heads/"),
Target: strings.TrimPrefix(v.Ref, "refs/heads/"),
Author: v.Commit.Author.Login,
AuthorName: v.Commit.Author.Name,
AuthorEmail: v.Commit.Author.Email,
AuthorAvatar: v.Commit.Author.Avatar,
Sender: v.Sender.Login,
}
}
repo = &core.Repository{
UID: v.Repo.ID,
Namespace: v.Repo.Namespace,
Name: v.Repo.Name,
Slug: scm.Join(v.Repo.Namespace, v.Repo.Name),
Link: v.Repo.Link,
Branch: v.Repo.Branch,
Private: v.Repo.Private,
HTTPURL: v.Repo.Clone,
SSHURL: v.Repo.CloneSSH,
}
// gogs and gitea do not include the author avatar in
// the webhook, but they do include the sender avatar.
// use the sender avatar when necessary.
if hook.AuthorAvatar == "" {
hook.AuthorAvatar = v.Sender.Avatar
}
return hook, repo, nil
case *scm.TagHook:
if v.Action != scm.ActionCreate {
return nil, nil, nil
}
// when a tag is created github sends both a push hook
// and a tag create hook. The push hook contains more
// information, so we choose to use the push hook and
// ignore the native tag hook.
if p.client.Driver == scm.DriverGithub ||
p.client.Driver == scm.DriverGitea ||
p.client.Driver == scm.DriverGitlab {
return nil, nil, nil
}
// the tag hook does not include the commit link, message
// or timestamp. In some cases it does not event include
// the sha (gogs). Note that we may need to fetch additional
// details to augment the webhook.
hook = &core.Hook{
Trigger: core.TriggerHook, // core.TriggerHook,
Event: core.EventTag,
Action: core.ActionCreate,
Link: "",
Timestamp: 0,
Message: "",
After: v.Ref.Sha,
Ref: v.Ref.Name,
Source: v.Ref.Name,
Target: v.Ref.Name,
Author: v.Sender.Login,
AuthorName: v.Sender.Name,
AuthorEmail: v.Sender.Email,
AuthorAvatar: v.Sender.Avatar,
Sender: v.Sender.Login,
}
repo = &core.Repository{
UID: v.Repo.ID,
Namespace: v.Repo.Namespace,
Name: v.Repo.Name,
Slug: scm.Join(v.Repo.Namespace, v.Repo.Name),
Link: v.Repo.Link,
Branch: v.Repo.Branch,
Private: v.Repo.Private,
HTTPURL: v.Repo.Clone,
SSHURL: v.Repo.CloneSSH,
}
// TODO(bradrydzewski) can we use scm.ExpandRef here?
if !strings.HasPrefix(hook.Ref, "refs/tags/") {
hook.Ref = fmt.Sprintf("refs/tags/%s", hook.Ref)
}
if hook.AuthorAvatar == "" {
hook.AuthorAvatar = v.Sender.Avatar
}
return hook, repo, nil
case *scm.PullRequestHook:
if v.Action != scm.ActionOpen && v.Action != scm.ActionSync {
return nil, nil, nil
}
// Pull Requests are not supported for Bitbucket due
// to lack of refs (e.g. refs/pull-requests/42/from).
// Please contact Bitbucket Support if you would like to
// see this feature enabled:
// https://bitbucket.org/site/master/issues/5814/repository-refs-for-pull-requests
if p.client.Driver == scm.DriverBitbucket {
return nil, nil, nil
}
hook = &core.Hook{
Trigger: core.TriggerHook, // core.TriggerHook,
Event: core.EventPullRequest,
Action: core.ActionCreate,
Link: v.PullRequest.Link,
Timestamp: v.PullRequest.Created.Unix(),
Title: v.PullRequest.Title,
Message: v.PullRequest.Body,
After: v.PullRequest.Sha,
Ref: v.PullRequest.Ref,
Fork: v.PullRequest.Fork,
Source: v.PullRequest.Source,
Target: v.PullRequest.Target,
Author: v.PullRequest.Author.Login,
AuthorName: v.PullRequest.Author.Name,
AuthorEmail: v.PullRequest.Author.Email,
AuthorAvatar: v.PullRequest.Author.Avatar,
Sender: v.Sender.Login,
}
if v.Action != scm.ActionSync {
hook.Action = core.ActionSync
}
// HACK this is a workaround for github. The pull
// request title is populated, but not the message.
if hook.Message == "" {
hook.Message = hook.Title
}
repo = &core.Repository{
UID: v.Repo.ID,
Namespace: v.Repo.Namespace,
Name: v.Repo.Name,
Slug: scm.Join(v.Repo.Namespace, v.Repo.Name),
Link: v.Repo.Link,
Branch: v.Repo.Branch,
Private: v.Repo.Private,
HTTPURL: v.Repo.Clone,
SSHURL: v.Repo.CloneSSH,
}
if hook.AuthorAvatar == "" {
hook.AuthorAvatar = v.Sender.Avatar
}
return hook, repo, nil
case *scm.BranchHook:
if v.Action != scm.ActionCreate {
return nil, nil, nil
}
if p.client.Driver != scm.DriverStash {
return nil, nil, nil
}
hook = &core.Hook{
Trigger: core.TriggerHook, // core.TriggerHook,
Event: core.EventPush,
Link: "",
Timestamp: 0,
Message: "",
After: v.Ref.Sha,
Ref: v.Ref.Name,
Source: v.Ref.Name,
Target: v.Ref.Name,
Author: v.Sender.Login,
AuthorName: v.Sender.Name,
AuthorEmail: v.Sender.Email,
AuthorAvatar: v.Sender.Avatar,
Sender: v.Sender.Login,
}
repo = &core.Repository{
UID: v.Repo.ID,
Namespace: v.Repo.Namespace,
Name: v.Repo.Name,
Slug: scm.Join(v.Repo.Namespace, v.Repo.Name),
Link: v.Repo.Link,
Branch: v.Repo.Branch,
Private: v.Repo.Private,
HTTPURL: v.Repo.Clone,
SSHURL: v.Repo.CloneSSH,
}
return hook, repo, nil
default:
return nil, nil, nil
}
}