-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathevents.go
338 lines (281 loc) · 10.4 KB
/
events.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
// SPDX-License-Identifier: Apache-2.0
package types
import (
"fmt"
"github.com/go-vela/server/api/types/actions"
"github.com/go-vela/server/constants"
)
// Events is the API representation of the various events that generate a
// webhook from the SCM.
type Events struct {
Push *actions.Push `json:"push"`
PullRequest *actions.Pull `json:"pull_request"`
Deployment *actions.Deploy `json:"deployment"`
Comment *actions.Comment `json:"comment"`
Schedule *actions.Schedule `json:"schedule"`
}
// NewEventsFromMask is an instatiation function for the Events type that
// takes in an event mask integer value and populates the nested Events struct.
func NewEventsFromMask(mask int64) *Events {
pushActions := new(actions.Push).FromMask(mask)
pullActions := new(actions.Pull).FromMask(mask)
deployActions := new(actions.Deploy).FromMask(mask)
commentActions := new(actions.Comment).FromMask(mask)
scheduleActions := new(actions.Schedule).FromMask(mask)
e := new(Events)
e.SetPush(pushActions)
e.SetPullRequest(pullActions)
e.SetDeployment(deployActions)
e.SetComment(commentActions)
e.SetSchedule(scheduleActions)
return e
}
// NewEventsFromSlice is an instantiation function for the Events type that
// takes in a slice of event strings and populates the nested Events struct.
func NewEventsFromSlice(events []string) (*Events, error) {
mask := int64(0)
// iterate through all events provided
for _, event := range events {
switch event {
// push actions
case constants.EventPush, constants.EventPush + ":branch":
mask = mask | constants.AllowPushBranch
case constants.EventTag, constants.EventPush + ":" + constants.EventTag:
mask = mask | constants.AllowPushTag
case constants.EventDelete + ":" + constants.ActionBranch:
mask = mask | constants.AllowPushDeleteBranch
case constants.EventDelete + ":" + constants.ActionTag:
mask = mask | constants.AllowPushDeleteTag
case constants.EventDelete:
mask = mask | constants.AllowPushDeleteBranch | constants.AllowPushDeleteTag
// pull_request actions
case constants.EventPull, constants.EventPullAlternate:
mask = mask | constants.AllowPullOpen | constants.AllowPullSync | constants.AllowPullReopen
case constants.EventPull + ":" + constants.ActionOpened:
mask = mask | constants.AllowPullOpen
case constants.EventPull + ":" + constants.ActionEdited:
mask = mask | constants.AllowPullEdit
case constants.EventPull + ":" + constants.ActionSynchronize:
mask = mask | constants.AllowPullSync
case constants.EventPull + ":" + constants.ActionReopened:
mask = mask | constants.AllowPullReopen
case constants.EventPull + ":" + constants.ActionLabeled:
mask = mask | constants.AllowPullLabel
case constants.EventPull + ":" + constants.ActionUnlabeled:
mask = mask | constants.AllowPullUnlabel
// deployment actions
case constants.EventDeploy, constants.EventDeployAlternate, constants.EventDeploy + ":" + constants.ActionCreated:
mask = mask | constants.AllowDeployCreate
// comment actions
case constants.EventComment:
mask = mask | constants.AllowCommentCreate | constants.AllowCommentEdit
case constants.EventComment + ":" + constants.ActionCreated:
mask = mask | constants.AllowCommentCreate
case constants.EventComment + ":" + constants.ActionEdited:
mask = mask | constants.AllowCommentEdit
// schedule actions
case constants.EventSchedule, constants.EventSchedule + ":" + constants.ActionRun:
mask = mask | constants.AllowSchedule
default:
return nil, fmt.Errorf("invalid event provided: %s", event)
}
}
return NewEventsFromMask(mask), nil
}
// Allowed determines whether or not an event + action is allowed based on whether
// its event:action is set to true in the Events struct.
func (e *Events) Allowed(event, action string) bool {
allowed := false
// if there is an action, create `event:action` comparator string
if len(action) > 0 {
event = event + ":" + action
}
switch event {
case constants.EventPush:
allowed = e.GetPush().GetBranch()
case constants.EventPull + ":" + constants.ActionOpened:
allowed = e.GetPullRequest().GetOpened()
case constants.EventPull + ":" + constants.ActionSynchronize:
allowed = e.GetPullRequest().GetSynchronize()
case constants.EventPull + ":" + constants.ActionEdited:
allowed = e.GetPullRequest().GetEdited()
case constants.EventPull + ":" + constants.ActionReopened:
allowed = e.GetPullRequest().GetReopened()
case constants.EventPull + ":" + constants.ActionLabeled:
allowed = e.GetPullRequest().GetLabeled()
case constants.EventPull + ":" + constants.ActionUnlabeled:
allowed = e.GetPullRequest().GetUnlabeled()
case constants.EventTag:
allowed = e.GetPush().GetTag()
case constants.EventComment + ":" + constants.ActionCreated:
allowed = e.GetComment().GetCreated()
case constants.EventComment + ":" + constants.ActionEdited:
allowed = e.GetComment().GetEdited()
case constants.EventDeploy + ":" + constants.ActionCreated:
allowed = e.GetDeployment().GetCreated()
case constants.EventSchedule:
allowed = e.GetSchedule().GetRun()
case constants.EventDelete + ":" + constants.ActionBranch:
allowed = e.GetPush().GetDeleteBranch()
case constants.EventDelete + ":" + constants.ActionTag:
allowed = e.GetPush().GetDeleteTag()
}
return allowed
}
// List is an Events method that generates a comma-separated list of event:action
// combinations that are allowed for the repo.
func (e *Events) List() []string {
eventSlice := []string{}
if e.GetPush().GetBranch() {
eventSlice = append(eventSlice, constants.EventPush)
}
if e.GetPullRequest().GetOpened() {
eventSlice = append(eventSlice, constants.EventPull+":"+constants.ActionOpened)
}
if e.GetPullRequest().GetSynchronize() {
eventSlice = append(eventSlice, constants.EventPull+":"+constants.ActionSynchronize)
}
if e.GetPullRequest().GetEdited() {
eventSlice = append(eventSlice, constants.EventPull+":"+constants.ActionEdited)
}
if e.GetPullRequest().GetReopened() {
eventSlice = append(eventSlice, constants.EventPull+":"+constants.ActionReopened)
}
if e.GetPullRequest().GetLabeled() {
eventSlice = append(eventSlice, constants.EventPull+":"+constants.ActionLabeled)
}
if e.GetPullRequest().GetUnlabeled() {
eventSlice = append(eventSlice, constants.EventPull+":"+constants.ActionUnlabeled)
}
if e.GetPush().GetTag() {
eventSlice = append(eventSlice, constants.EventTag)
}
if e.GetDeployment().GetCreated() {
eventSlice = append(eventSlice, constants.EventDeploy)
}
if e.GetComment().GetCreated() {
eventSlice = append(eventSlice, constants.EventComment+":"+constants.ActionCreated)
}
if e.GetComment().GetEdited() {
eventSlice = append(eventSlice, constants.EventComment+":"+constants.ActionEdited)
}
if e.GetSchedule().GetRun() {
eventSlice = append(eventSlice, constants.EventSchedule)
}
if e.GetPush().GetDeleteBranch() {
eventSlice = append(eventSlice, constants.EventDelete+":"+constants.ActionBranch)
}
if e.GetPush().GetDeleteTag() {
eventSlice = append(eventSlice, constants.EventDelete+":"+constants.ActionTag)
}
return eventSlice
}
// ToDatabase is an Events method that converts a nested Events struct into an integer event mask.
func (e *Events) ToDatabase() int64 {
return 0 |
e.GetPush().ToMask() |
e.GetPullRequest().ToMask() |
e.GetComment().ToMask() |
e.GetDeployment().ToMask() |
e.GetSchedule().ToMask()
}
// GetPush returns the Push field from the provided Events. If the object is nil,
// or the field within the object is nil, it returns the zero value instead.
func (e *Events) GetPush() *actions.Push {
// return zero value if Events type or Push field is nil
if e == nil || e.Push == nil {
return new(actions.Push)
}
return e.Push
}
// GetPullRequest returns the PullRequest field from the provided Events. If the object is nil,
// or the field within the object is nil, it returns the zero value instead.
func (e *Events) GetPullRequest() *actions.Pull {
// return zero value if Events type or PullRequest field is nil
if e == nil || e.PullRequest == nil {
return new(actions.Pull)
}
return e.PullRequest
}
// GetDeployment returns the Deployment field from the provided Events. If the object is nil,
// or the field within the object is nil, it returns the zero value instead.
func (e *Events) GetDeployment() *actions.Deploy {
// return zero value if Events type or Deployment field is nil
if e == nil || e.Deployment == nil {
return new(actions.Deploy)
}
return e.Deployment
}
// GetComment returns the Comment field from the provided Events. If the object is nil,
// or the field within the object is nil, it returns the zero value instead.
func (e *Events) GetComment() *actions.Comment {
// return zero value if Events type or Comment field is nil
if e == nil || e.Comment == nil {
return new(actions.Comment)
}
return e.Comment
}
// GetSchedule returns the Schedule field from the provided Events. If the object is nil,
// or the field within the object is nil, it returns the zero value instead.
func (e *Events) GetSchedule() *actions.Schedule {
// return zero value if Events type or Schedule field is nil
if e == nil || e.Schedule == nil {
return new(actions.Schedule)
}
return e.Schedule
}
// SetPush sets the Events Push field.
//
// When the provided Events type is nil, it
// will set nothing and immediately return.
func (e *Events) SetPush(v *actions.Push) {
// return if Events type is nil
if e == nil {
return
}
e.Push = v
}
// SetPullRequest sets the Events PullRequest field.
//
// When the provided Events type is nil, it
// will set nothing and immediately return.
func (e *Events) SetPullRequest(v *actions.Pull) {
// return if Events type is nil
if e == nil {
return
}
e.PullRequest = v
}
// SetDeployment sets the Events Deployment field.
//
// When the provided Events type is nil, it
// will set nothing and immediately return.
func (e *Events) SetDeployment(v *actions.Deploy) {
// return if Events type is nil
if e == nil {
return
}
e.Deployment = v
}
// SetComment sets the Events Comment field.
//
// When the provided Events type is nil, it
// will set nothing and immediately return.
func (e *Events) SetComment(v *actions.Comment) {
// return if Events type is nil
if e == nil {
return
}
e.Comment = v
}
// SetSchedule sets the Events Schedule field.
//
// When the provided Events type is nil, it
// will set nothing and immediately return.
func (e *Events) SetSchedule(v *actions.Schedule) {
// return if Events type is nil
if e == nil {
return
}
e.Schedule = v
}