@@ -23,12 +23,16 @@ import (
2323 "code.gitea.io/gitea/modules/sync"
2424)
2525
26+ // HookQueue is a global queue of web hooks
2627var HookQueue = sync .NewUniqueQueue (setting .Webhook .QueueLength )
2728
29+ // HookContentType is the content type of a web hook
2830type HookContentType int
2931
3032const (
33+ // ContentTypeJSON is a JSON payload for web hooks
3134 ContentTypeJSON HookContentType = iota + 1
35+ // ContentTypeForm is an url-encoded form payload for web hook
3236 ContentTypeForm
3337)
3438
@@ -42,6 +46,7 @@ func ToHookContentType(name string) HookContentType {
4246 return hookContentTypes [name ]
4347}
4448
49+ // Name returns the name of a given web hook's content type
4550func (t HookContentType ) Name () string {
4651 switch t {
4752 case ContentTypeJSON :
@@ -58,6 +63,7 @@ func IsValidHookContentType(name string) bool {
5863 return ok
5964}
6065
66+ // HookEvents is a set of web hook events
6167type HookEvents struct {
6268 Create bool `json:"create"`
6369 Push bool `json:"push"`
@@ -73,8 +79,10 @@ type HookEvent struct {
7379 HookEvents `json:"events"`
7480}
7581
82+ // HookStatus is the status of a web hook
7683type HookStatus int
7784
85+ // Possible statuses of a web hook
7886const (
7987 HookStatusNone = iota
8088 HookStatusSucceed
@@ -103,15 +111,20 @@ type Webhook struct {
103111 UpdatedUnix int64
104112}
105113
114+ // BeforeInsert will be invoked by XORM before inserting a record
115+ // representing this object
106116func (w * Webhook ) BeforeInsert () {
107117 w .CreatedUnix = time .Now ().Unix ()
108118 w .UpdatedUnix = w .CreatedUnix
109119}
110120
121+ // BeforeUpdate will be invoked by XORM before updating a record
122+ // representing this object
111123func (w * Webhook ) BeforeUpdate () {
112124 w .UpdatedUnix = time .Now ().Unix ()
113125}
114126
127+ // AfterSet updates the webhook object upon setting a column
115128func (w * Webhook ) AfterSet (colName string , _ xorm.Cell ) {
116129 var err error
117130 switch colName {
@@ -127,6 +140,7 @@ func (w *Webhook) AfterSet(colName string, _ xorm.Cell) {
127140 }
128141}
129142
143+ // GetSlackHook returns slack metadata
130144func (w * Webhook ) GetSlackHook () * SlackMeta {
131145 s := & SlackMeta {}
132146 if err := json .Unmarshal ([]byte (w .Meta ), s ); err != nil {
@@ -165,6 +179,7 @@ func (w *Webhook) HasPullRequestEvent() bool {
165179 (w .ChooseEvents && w .HookEvents .PullRequest )
166180}
167181
182+ // EventsArray returns an array of hook events
168183func (w * Webhook ) EventsArray () []string {
169184 events := make ([]string , 0 , 3 )
170185 if w .HasCreateEvent () {
@@ -290,8 +305,10 @@ func GetActiveWebhooksByOrgID(orgID int64) (ws []*Webhook, err error) {
290305// \___|_ / \____/ \____/|__|_ \ |____| (____ /____ >__|_ \
291306// \/ \/ \/ \/ \/
292307
308+ // HookTaskType is the type of an hook task
293309type HookTaskType int
294310
311+ // Types of hook tasks
295312const (
296313 GOGS HookTaskType = iota + 1
297314 SLACK
@@ -307,6 +324,7 @@ func ToHookTaskType(name string) HookTaskType {
307324 return hookTaskTypes [name ]
308325}
309326
327+ // Name returns the name of an hook task type
310328func (t HookTaskType ) Name () string {
311329 switch t {
312330 case GOGS :
@@ -323,8 +341,10 @@ func IsValidHookTaskType(name string) bool {
323341 return ok
324342}
325343
344+ // HookEventType is the type of an hook event
326345type HookEventType string
327346
347+ // Types of hook events
328348const (
329349 HookEventCreate HookEventType = "create"
330350 HookEventPush HookEventType = "push"
@@ -368,15 +388,18 @@ type HookTask struct {
368388 ResponseInfo * HookResponse `xorm:"-"`
369389}
370390
391+ // BeforeUpdate will be invoked by XORM before updating a record
392+ // representing this object
371393func (t * HookTask ) BeforeUpdate () {
372394 if t .RequestInfo != nil {
373- t .RequestContent = t .SimpleMarshalJSON (t .RequestInfo )
395+ t .RequestContent = t .simpleMarshalJSON (t .RequestInfo )
374396 }
375397 if t .ResponseInfo != nil {
376- t .ResponseContent = t .SimpleMarshalJSON (t .ResponseInfo )
398+ t .ResponseContent = t .simpleMarshalJSON (t .ResponseInfo )
377399 }
378400}
379401
402+ // AfterSet updates the webhook object upon setting a column
380403func (t * HookTask ) AfterSet (colName string , _ xorm.Cell ) {
381404 var err error
382405 switch colName {
@@ -405,7 +428,7 @@ func (t *HookTask) AfterSet(colName string, _ xorm.Cell) {
405428 }
406429}
407430
408- func (t * HookTask ) SimpleMarshalJSON (v interface {}) string {
431+ func (t * HookTask ) simpleMarshalJSON (v interface {}) string {
409432 p , err := json .Marshal (v )
410433 if err != nil {
411434 log .Error (3 , "Marshal [%d]: %v" , t .ID , err )
@@ -624,6 +647,7 @@ func DeliverHooks() {
624647 }
625648}
626649
650+ // InitDeliverHooks starts the hooks delivery thread
627651func InitDeliverHooks () {
628652 go DeliverHooks ()
629653}
0 commit comments