Skip to content

Commit 0f4e1b9

Browse files
delvh6543lunny
authored
Restructure webhook module (#22256)
Previously, there was an `import services/webhooks` inside `modules/notification/webhook`. This import was removed (after fighting against many import cycles). Additionally, `modules/notification/webhook` was moved to `modules/webhook`, and a few structs/constants were extracted from `models/webhooks` to `modules/webhook`. Co-authored-by: 6543 <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent f8e93ce commit 0f4e1b9

37 files changed

+444
-362
lines changed

models/migrations/v1_19/v233.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package v1_19 //nolint
66
import (
77
"fmt"
88

9-
"code.gitea.io/gitea/models/webhook"
109
"code.gitea.io/gitea/modules/json"
1110
"code.gitea.io/gitea/modules/secret"
1211
"code.gitea.io/gitea/modules/setting"
@@ -56,9 +55,9 @@ func batchProcess[T any](x *xorm.Engine, buf []T, query func(limit, start int) *
5655
func AddHeaderAuthorizationEncryptedColWebhook(x *xorm.Engine) error {
5756
// Add the column to the table
5857
type Webhook struct {
59-
ID int64 `xorm:"pk autoincr"`
60-
Type webhook.HookType `xorm:"VARCHAR(16) 'type'"`
61-
Meta string `xorm:"TEXT"` // store hook-specific attributes
58+
ID int64 `xorm:"pk autoincr"`
59+
Type string `xorm:"VARCHAR(16) 'type'"`
60+
Meta string `xorm:"TEXT"` // store hook-specific attributes
6261

6362
// HeaderAuthorizationEncrypted should be accessed using HeaderAuthorization() and SetHeaderAuthorization()
6463
HeaderAuthorizationEncrypted string `xorm:"TEXT"`

models/migrations/v1_19/v233_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ import (
77
"testing"
88

99
"code.gitea.io/gitea/models/migrations/base"
10-
"code.gitea.io/gitea/models/webhook"
1110
"code.gitea.io/gitea/modules/json"
1211
"code.gitea.io/gitea/modules/secret"
1312
"code.gitea.io/gitea/modules/setting"
13+
webhook_module "code.gitea.io/gitea/modules/webhook"
1414

1515
"github.com/stretchr/testify/assert"
1616
)
1717

1818
func Test_AddHeaderAuthorizationEncryptedColWebhook(t *testing.T) {
1919
// Create Webhook table
2020
type Webhook struct {
21-
ID int64 `xorm:"pk autoincr"`
22-
Type webhook.HookType `xorm:"VARCHAR(16) 'type'"`
23-
Meta string `xorm:"TEXT"` // store hook-specific attributes
21+
ID int64 `xorm:"pk autoincr"`
22+
Type webhook_module.HookType `xorm:"VARCHAR(16) 'type'"`
23+
Meta string `xorm:"TEXT"` // store hook-specific attributes
2424

2525
// HeaderAuthorizationEncrypted should be accessed using HeaderAuthorization() and SetHeaderAuthorization()
2626
HeaderAuthorizationEncrypted string `xorm:"TEXT"`

models/webhook/hooktask.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"code.gitea.io/gitea/modules/log"
1313
"code.gitea.io/gitea/modules/setting"
1414
api "code.gitea.io/gitea/modules/structs"
15+
webhook_module "code.gitea.io/gitea/modules/webhook"
1516

1617
gouuid "github.com/google/uuid"
1718
)
@@ -107,7 +108,7 @@ type HookTask struct {
107108
UUID string `xorm:"unique"`
108109
api.Payloader `xorm:"-"`
109110
PayloadContent string `xorm:"LONGTEXT"`
110-
EventType HookEventType
111+
EventType webhook_module.HookEventType
111112
IsDelivered bool
112113
Delivered int64
113114
DeliveredString string `xorm:"-"`

models/webhook/webhook.go

Lines changed: 43 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"code.gitea.io/gitea/modules/setting"
1717
"code.gitea.io/gitea/modules/timeutil"
1818
"code.gitea.io/gitea/modules/util"
19+
webhook_module "code.gitea.io/gitea/modules/webhook"
1920

2021
"xorm.io/builder"
2122
)
@@ -46,7 +47,7 @@ type ErrHookTaskNotExist struct {
4647
UUID string
4748
}
4849

49-
// IsErrWebhookNotExist checks if an error is a ErrWebhookNotExist.
50+
// IsErrHookTaskNotExist checks if an error is a ErrHookTaskNotExist.
5051
func IsErrHookTaskNotExist(err error) bool {
5152
_, ok := err.(ErrHookTaskNotExist)
5253
return ok
@@ -117,84 +118,22 @@ func IsValidHookContentType(name string) bool {
117118
return ok
118119
}
119120

120-
// HookEvents is a set of web hook events
121-
type HookEvents struct {
122-
Create bool `json:"create"`
123-
Delete bool `json:"delete"`
124-
Fork bool `json:"fork"`
125-
Issues bool `json:"issues"`
126-
IssueAssign bool `json:"issue_assign"`
127-
IssueLabel bool `json:"issue_label"`
128-
IssueMilestone bool `json:"issue_milestone"`
129-
IssueComment bool `json:"issue_comment"`
130-
Push bool `json:"push"`
131-
PullRequest bool `json:"pull_request"`
132-
PullRequestAssign bool `json:"pull_request_assign"`
133-
PullRequestLabel bool `json:"pull_request_label"`
134-
PullRequestMilestone bool `json:"pull_request_milestone"`
135-
PullRequestComment bool `json:"pull_request_comment"`
136-
PullRequestReview bool `json:"pull_request_review"`
137-
PullRequestSync bool `json:"pull_request_sync"`
138-
Wiki bool `json:"wiki"`
139-
Repository bool `json:"repository"`
140-
Release bool `json:"release"`
141-
Package bool `json:"package"`
142-
}
143-
144-
// HookEvent represents events that will delivery hook.
145-
type HookEvent struct {
146-
PushOnly bool `json:"push_only"`
147-
SendEverything bool `json:"send_everything"`
148-
ChooseEvents bool `json:"choose_events"`
149-
BranchFilter string `json:"branch_filter"`
150-
151-
HookEvents `json:"events"`
152-
}
153-
154-
// HookType is the type of a webhook
155-
type HookType = string
156-
157-
// Types of webhooks
158-
const (
159-
GITEA HookType = "gitea"
160-
GOGS HookType = "gogs"
161-
SLACK HookType = "slack"
162-
DISCORD HookType = "discord"
163-
DINGTALK HookType = "dingtalk"
164-
TELEGRAM HookType = "telegram"
165-
MSTEAMS HookType = "msteams"
166-
FEISHU HookType = "feishu"
167-
MATRIX HookType = "matrix"
168-
WECHATWORK HookType = "wechatwork"
169-
PACKAGIST HookType = "packagist"
170-
)
171-
172-
// HookStatus is the status of a web hook
173-
type HookStatus int
174-
175-
// Possible statuses of a web hook
176-
const (
177-
HookStatusNone = iota
178-
HookStatusSucceed
179-
HookStatusFail
180-
)
181-
182121
// Webhook represents a web hook object.
183122
type Webhook struct {
184-
ID int64 `xorm:"pk autoincr"`
185-
RepoID int64 `xorm:"INDEX"` // An ID of 0 indicates either a default or system webhook
186-
OrgID int64 `xorm:"INDEX"`
187-
IsSystemWebhook bool
188-
URL string `xorm:"url TEXT"`
189-
HTTPMethod string `xorm:"http_method"`
190-
ContentType HookContentType
191-
Secret string `xorm:"TEXT"`
192-
Events string `xorm:"TEXT"`
193-
*HookEvent `xorm:"-"`
194-
IsActive bool `xorm:"INDEX"`
195-
Type HookType `xorm:"VARCHAR(16) 'type'"`
196-
Meta string `xorm:"TEXT"` // store hook-specific attributes
197-
LastStatus HookStatus // Last delivery status
123+
ID int64 `xorm:"pk autoincr"`
124+
RepoID int64 `xorm:"INDEX"` // An ID of 0 indicates either a default or system webhook
125+
OrgID int64 `xorm:"INDEX"`
126+
IsSystemWebhook bool
127+
URL string `xorm:"url TEXT"`
128+
HTTPMethod string `xorm:"http_method"`
129+
ContentType HookContentType
130+
Secret string `xorm:"TEXT"`
131+
Events string `xorm:"TEXT"`
132+
*webhook_module.HookEvent `xorm:"-"`
133+
IsActive bool `xorm:"INDEX"`
134+
Type webhook_module.HookType `xorm:"VARCHAR(16) 'type'"`
135+
Meta string `xorm:"TEXT"` // store hook-specific attributes
136+
LastStatus webhook_module.HookStatus // Last delivery status
198137

199138
// HeaderAuthorizationEncrypted should be accessed using HeaderAuthorization() and SetHeaderAuthorization()
200139
HeaderAuthorizationEncrypted string `xorm:"TEXT"`
@@ -209,7 +148,7 @@ func init() {
209148

210149
// AfterLoad updates the webhook object upon setting a column
211150
func (w *Webhook) AfterLoad() {
212-
w.HookEvent = &HookEvent{}
151+
w.HookEvent = &webhook_module.HookEvent{}
213152
if err := json.Unmarshal([]byte(w.Events), w.HookEvent); err != nil {
214153
log.Error("Unmarshal[%d]: %v", w.ID, err)
215154
}
@@ -362,34 +301,34 @@ func (w *Webhook) HasPackageEvent() bool {
362301
// EventCheckers returns event checkers
363302
func (w *Webhook) EventCheckers() []struct {
364303
Has func() bool
365-
Type HookEventType
304+
Type webhook_module.HookEventType
366305
} {
367306
return []struct {
368307
Has func() bool
369-
Type HookEventType
308+
Type webhook_module.HookEventType
370309
}{
371-
{w.HasCreateEvent, HookEventCreate},
372-
{w.HasDeleteEvent, HookEventDelete},
373-
{w.HasForkEvent, HookEventFork},
374-
{w.HasPushEvent, HookEventPush},
375-
{w.HasIssuesEvent, HookEventIssues},
376-
{w.HasIssuesAssignEvent, HookEventIssueAssign},
377-
{w.HasIssuesLabelEvent, HookEventIssueLabel},
378-
{w.HasIssuesMilestoneEvent, HookEventIssueMilestone},
379-
{w.HasIssueCommentEvent, HookEventIssueComment},
380-
{w.HasPullRequestEvent, HookEventPullRequest},
381-
{w.HasPullRequestAssignEvent, HookEventPullRequestAssign},
382-
{w.HasPullRequestLabelEvent, HookEventPullRequestLabel},
383-
{w.HasPullRequestMilestoneEvent, HookEventPullRequestMilestone},
384-
{w.HasPullRequestCommentEvent, HookEventPullRequestComment},
385-
{w.HasPullRequestApprovedEvent, HookEventPullRequestReviewApproved},
386-
{w.HasPullRequestRejectedEvent, HookEventPullRequestReviewRejected},
387-
{w.HasPullRequestCommentEvent, HookEventPullRequestReviewComment},
388-
{w.HasPullRequestSyncEvent, HookEventPullRequestSync},
389-
{w.HasWikiEvent, HookEventWiki},
390-
{w.HasRepositoryEvent, HookEventRepository},
391-
{w.HasReleaseEvent, HookEventRelease},
392-
{w.HasPackageEvent, HookEventPackage},
310+
{w.HasCreateEvent, webhook_module.HookEventCreate},
311+
{w.HasDeleteEvent, webhook_module.HookEventDelete},
312+
{w.HasForkEvent, webhook_module.HookEventFork},
313+
{w.HasPushEvent, webhook_module.HookEventPush},
314+
{w.HasIssuesEvent, webhook_module.HookEventIssues},
315+
{w.HasIssuesAssignEvent, webhook_module.HookEventIssueAssign},
316+
{w.HasIssuesLabelEvent, webhook_module.HookEventIssueLabel},
317+
{w.HasIssuesMilestoneEvent, webhook_module.HookEventIssueMilestone},
318+
{w.HasIssueCommentEvent, webhook_module.HookEventIssueComment},
319+
{w.HasPullRequestEvent, webhook_module.HookEventPullRequest},
320+
{w.HasPullRequestAssignEvent, webhook_module.HookEventPullRequestAssign},
321+
{w.HasPullRequestLabelEvent, webhook_module.HookEventPullRequestLabel},
322+
{w.HasPullRequestMilestoneEvent, webhook_module.HookEventPullRequestMilestone},
323+
{w.HasPullRequestCommentEvent, webhook_module.HookEventPullRequestComment},
324+
{w.HasPullRequestApprovedEvent, webhook_module.HookEventPullRequestReviewApproved},
325+
{w.HasPullRequestRejectedEvent, webhook_module.HookEventPullRequestReviewRejected},
326+
{w.HasPullRequestCommentEvent, webhook_module.HookEventPullRequestReviewComment},
327+
{w.HasPullRequestSyncEvent, webhook_module.HookEventPullRequestSync},
328+
{w.HasWikiEvent, webhook_module.HookEventWiki},
329+
{w.HasRepositoryEvent, webhook_module.HookEventRepository},
330+
{w.HasReleaseEvent, webhook_module.HookEventRelease},
331+
{w.HasPackageEvent, webhook_module.HookEventPackage},
393332
}
394333
}
395334

@@ -453,7 +392,7 @@ func getWebhook(bean *Webhook) (*Webhook, error) {
453392
if err != nil {
454393
return nil, err
455394
} else if !has {
456-
return nil, ErrWebhookNotExist{bean.ID}
395+
return nil, ErrWebhookNotExist{ID: bean.ID}
457396
}
458397
return bean, nil
459398
}
@@ -541,7 +480,7 @@ func GetSystemOrDefaultWebhook(id int64) (*Webhook, error) {
541480
if err != nil {
542481
return nil, err
543482
} else if !has {
544-
return nil, ErrWebhookNotExist{id}
483+
return nil, ErrWebhookNotExist{ID: id}
545484
}
546485
return webhook, nil
547486
}

models/webhook/webhook_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"code.gitea.io/gitea/modules/json"
1414
api "code.gitea.io/gitea/modules/structs"
1515
"code.gitea.io/gitea/modules/util"
16+
webhook_module "code.gitea.io/gitea/modules/webhook"
1617

1718
"github.com/stretchr/testify/assert"
1819
)
@@ -46,11 +47,11 @@ func TestWebhook_History(t *testing.T) {
4647
func TestWebhook_UpdateEvent(t *testing.T) {
4748
assert.NoError(t, unittest.PrepareTestDatabase())
4849
webhook := unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 1})
49-
hookEvent := &HookEvent{
50+
hookEvent := &webhook_module.HookEvent{
5051
PushOnly: true,
5152
SendEverything: false,
5253
ChooseEvents: false,
53-
HookEvents: HookEvents{
54+
HookEvents: webhook_module.HookEvents{
5455
Create: false,
5556
Push: true,
5657
PullRequest: false,
@@ -59,7 +60,7 @@ func TestWebhook_UpdateEvent(t *testing.T) {
5960
webhook.HookEvent = hookEvent
6061
assert.NoError(t, webhook.UpdateEvent())
6162
assert.NotEmpty(t, webhook.Events)
62-
actualHookEvent := &HookEvent{}
63+
actualHookEvent := &webhook_module.HookEvent{}
6364
assert.NoError(t, json.Unmarshal([]byte(webhook.Events), actualHookEvent))
6465
assert.Equal(t, *hookEvent, *actualHookEvent)
6566
}
@@ -74,13 +75,13 @@ func TestWebhook_EventsArray(t *testing.T) {
7475
"package",
7576
},
7677
(&Webhook{
77-
HookEvent: &HookEvent{SendEverything: true},
78+
HookEvent: &webhook_module.HookEvent{SendEverything: true},
7879
}).EventsArray(),
7980
)
8081

8182
assert.Equal(t, []string{"push"},
8283
(&Webhook{
83-
HookEvent: &HookEvent{PushOnly: true},
84+
HookEvent: &webhook_module.HookEvent{PushOnly: true},
8485
}).EventsArray(),
8586
)
8687
}

modules/notification/notification.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"code.gitea.io/gitea/modules/notification/mail"
1717
"code.gitea.io/gitea/modules/notification/mirror"
1818
"code.gitea.io/gitea/modules/notification/ui"
19-
"code.gitea.io/gitea/modules/notification/webhook"
2019
"code.gitea.io/gitea/modules/repository"
2120
"code.gitea.io/gitea/modules/setting"
2221
)
@@ -36,7 +35,6 @@ func NewContext() {
3635
RegisterNotifier(mail.NewNotifier())
3736
}
3837
RegisterNotifier(indexer.NewNotifier())
39-
RegisterNotifier(webhook.NewNotifier())
4038
RegisterNotifier(action.NewNotifier())
4139
RegisterNotifier(mirror.NewNotifier())
4240
}

modules/webhook/structs.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2022 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package webhook
5+
6+
// HookEvents is a set of web hook events
7+
type HookEvents struct {
8+
Create bool `json:"create"`
9+
Delete bool `json:"delete"`
10+
Fork bool `json:"fork"`
11+
Issues bool `json:"issues"`
12+
IssueAssign bool `json:"issue_assign"`
13+
IssueLabel bool `json:"issue_label"`
14+
IssueMilestone bool `json:"issue_milestone"`
15+
IssueComment bool `json:"issue_comment"`
16+
Push bool `json:"push"`
17+
PullRequest bool `json:"pull_request"`
18+
PullRequestAssign bool `json:"pull_request_assign"`
19+
PullRequestLabel bool `json:"pull_request_label"`
20+
PullRequestMilestone bool `json:"pull_request_milestone"`
21+
PullRequestComment bool `json:"pull_request_comment"`
22+
PullRequestReview bool `json:"pull_request_review"`
23+
PullRequestSync bool `json:"pull_request_sync"`
24+
Wiki bool `json:"wiki"`
25+
Repository bool `json:"repository"`
26+
Release bool `json:"release"`
27+
Package bool `json:"package"`
28+
}
29+
30+
// HookEvent represents events that will delivery hook.
31+
type HookEvent struct {
32+
PushOnly bool `json:"push_only"`
33+
SendEverything bool `json:"send_everything"`
34+
ChooseEvents bool `json:"choose_events"`
35+
BranchFilter string `json:"branch_filter"`
36+
37+
HookEvents `json:"events"`
38+
}

0 commit comments

Comments
 (0)