@@ -23,12 +23,16 @@ import (
23
23
"code.gitea.io/gitea/modules/sync"
24
24
)
25
25
26
+ // HookQueue is a global queue of web hooks
26
27
var HookQueue = sync .NewUniqueQueue (setting .Webhook .QueueLength )
27
28
29
+ // HookContentType is the content type of a web hook
28
30
type HookContentType int
29
31
30
32
const (
33
+ // ContentTypeJSON is a JSON payload for web hooks
31
34
ContentTypeJSON HookContentType = iota + 1
35
+ // ContentTypeForm is an url-encoded form payload for web hook
32
36
ContentTypeForm
33
37
)
34
38
@@ -42,6 +46,7 @@ func ToHookContentType(name string) HookContentType {
42
46
return hookContentTypes [name ]
43
47
}
44
48
49
+ // Name returns the name of a given web hook's content type
45
50
func (t HookContentType ) Name () string {
46
51
switch t {
47
52
case ContentTypeJSON :
@@ -58,6 +63,7 @@ func IsValidHookContentType(name string) bool {
58
63
return ok
59
64
}
60
65
66
+ // HookEvents is a set of web hook events
61
67
type HookEvents struct {
62
68
Create bool `json:"create"`
63
69
Push bool `json:"push"`
@@ -73,8 +79,10 @@ type HookEvent struct {
73
79
HookEvents `json:"events"`
74
80
}
75
81
82
+ // HookStatus is the status of a web hook
76
83
type HookStatus int
77
84
85
+ // Possible statuses of a web hook
78
86
const (
79
87
HookStatusNone = iota
80
88
HookStatusSucceed
@@ -103,15 +111,20 @@ type Webhook struct {
103
111
UpdatedUnix int64
104
112
}
105
113
114
+ // BeforeInsert will be invoked by XORM before inserting a record
115
+ // representing this object
106
116
func (w * Webhook ) BeforeInsert () {
107
117
w .CreatedUnix = time .Now ().Unix ()
108
118
w .UpdatedUnix = w .CreatedUnix
109
119
}
110
120
121
+ // BeforeUpdate will be invoked by XORM before updating a record
122
+ // representing this object
111
123
func (w * Webhook ) BeforeUpdate () {
112
124
w .UpdatedUnix = time .Now ().Unix ()
113
125
}
114
126
127
+ // AfterSet updates the webhook object upon setting a column
115
128
func (w * Webhook ) AfterSet (colName string , _ xorm.Cell ) {
116
129
var err error
117
130
switch colName {
@@ -127,6 +140,7 @@ func (w *Webhook) AfterSet(colName string, _ xorm.Cell) {
127
140
}
128
141
}
129
142
143
+ // GetSlackHook returns slack metadata
130
144
func (w * Webhook ) GetSlackHook () * SlackMeta {
131
145
s := & SlackMeta {}
132
146
if err := json .Unmarshal ([]byte (w .Meta ), s ); err != nil {
@@ -165,6 +179,7 @@ func (w *Webhook) HasPullRequestEvent() bool {
165
179
(w .ChooseEvents && w .HookEvents .PullRequest )
166
180
}
167
181
182
+ // EventsArray returns an array of hook events
168
183
func (w * Webhook ) EventsArray () []string {
169
184
events := make ([]string , 0 , 3 )
170
185
if w .HasCreateEvent () {
@@ -290,8 +305,10 @@ func GetActiveWebhooksByOrgID(orgID int64) (ws []*Webhook, err error) {
290
305
// \___|_ / \____/ \____/|__|_ \ |____| (____ /____ >__|_ \
291
306
// \/ \/ \/ \/ \/
292
307
308
+ // HookTaskType is the type of an hook task
293
309
type HookTaskType int
294
310
311
+ // Types of hook tasks
295
312
const (
296
313
GOGS HookTaskType = iota + 1
297
314
SLACK
@@ -307,6 +324,7 @@ func ToHookTaskType(name string) HookTaskType {
307
324
return hookTaskTypes [name ]
308
325
}
309
326
327
+ // Name returns the name of an hook task type
310
328
func (t HookTaskType ) Name () string {
311
329
switch t {
312
330
case GOGS :
@@ -323,8 +341,10 @@ func IsValidHookTaskType(name string) bool {
323
341
return ok
324
342
}
325
343
344
+ // HookEventType is the type of an hook event
326
345
type HookEventType string
327
346
347
+ // Types of hook events
328
348
const (
329
349
HookEventCreate HookEventType = "create"
330
350
HookEventPush HookEventType = "push"
@@ -368,15 +388,18 @@ type HookTask struct {
368
388
ResponseInfo * HookResponse `xorm:"-"`
369
389
}
370
390
391
+ // BeforeUpdate will be invoked by XORM before updating a record
392
+ // representing this object
371
393
func (t * HookTask ) BeforeUpdate () {
372
394
if t .RequestInfo != nil {
373
- t .RequestContent = t .SimpleMarshalJSON (t .RequestInfo )
395
+ t .RequestContent = t .simpleMarshalJSON (t .RequestInfo )
374
396
}
375
397
if t .ResponseInfo != nil {
376
- t .ResponseContent = t .SimpleMarshalJSON (t .ResponseInfo )
398
+ t .ResponseContent = t .simpleMarshalJSON (t .ResponseInfo )
377
399
}
378
400
}
379
401
402
+ // AfterSet updates the webhook object upon setting a column
380
403
func (t * HookTask ) AfterSet (colName string , _ xorm.Cell ) {
381
404
var err error
382
405
switch colName {
@@ -405,7 +428,7 @@ func (t *HookTask) AfterSet(colName string, _ xorm.Cell) {
405
428
}
406
429
}
407
430
408
- func (t * HookTask ) SimpleMarshalJSON (v interface {}) string {
431
+ func (t * HookTask ) simpleMarshalJSON (v interface {}) string {
409
432
p , err := json .Marshal (v )
410
433
if err != nil {
411
434
log .Error (3 , "Marshal [%d]: %v" , t .ID , err )
@@ -624,6 +647,7 @@ func DeliverHooks() {
624
647
}
625
648
}
626
649
650
+ // InitDeliverHooks starts the hooks delivery thread
627
651
func InitDeliverHooks () {
628
652
go DeliverHooks ()
629
653
}
0 commit comments