summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authordelvh <leon@kske.dev>2023-01-01 16:23:15 +0100
committerGitHub <noreply@github.com>2023-01-01 23:23:15 +0800
commit0f4e1b9ac66b8ffa0083a5a2516e4710393bb0da (patch)
tree4514951316ebfb10dabf1ffbc856142839817a80 /models
parentf8e93ce4238253c01e3ca36c88eb59979be99d12 (diff)
downloadgitea-0f4e1b9ac66b8ffa0083a5a2516e4710393bb0da.tar.gz
gitea-0f4e1b9ac66b8ffa0083a5a2516e4710393bb0da.zip
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 <6543@obermui.de> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'models')
-rw-r--r--models/migrations/v1_19/v233.go7
-rw-r--r--models/migrations/v1_19/v233_test.go8
-rw-r--r--models/webhook/hooktask.go3
-rw-r--r--models/webhook/webhook.go147
-rw-r--r--models/webhook/webhook_test.go11
5 files changed, 58 insertions, 118 deletions
diff --git a/models/migrations/v1_19/v233.go b/models/migrations/v1_19/v233.go
index fe568b64eb..ba4cd8e20b 100644
--- a/models/migrations/v1_19/v233.go
+++ b/models/migrations/v1_19/v233.go
@@ -6,7 +6,6 @@ package v1_19 //nolint
import (
"fmt"
- "code.gitea.io/gitea/models/webhook"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/secret"
"code.gitea.io/gitea/modules/setting"
@@ -56,9 +55,9 @@ func batchProcess[T any](x *xorm.Engine, buf []T, query func(limit, start int) *
func AddHeaderAuthorizationEncryptedColWebhook(x *xorm.Engine) error {
// Add the column to the table
type Webhook struct {
- ID int64 `xorm:"pk autoincr"`
- Type webhook.HookType `xorm:"VARCHAR(16) 'type'"`
- Meta string `xorm:"TEXT"` // store hook-specific attributes
+ ID int64 `xorm:"pk autoincr"`
+ Type string `xorm:"VARCHAR(16) 'type'"`
+ Meta string `xorm:"TEXT"` // store hook-specific attributes
// HeaderAuthorizationEncrypted should be accessed using HeaderAuthorization() and SetHeaderAuthorization()
HeaderAuthorizationEncrypted string `xorm:"TEXT"`
diff --git a/models/migrations/v1_19/v233_test.go b/models/migrations/v1_19/v233_test.go
index 9902b7e4ae..83558da334 100644
--- a/models/migrations/v1_19/v233_test.go
+++ b/models/migrations/v1_19/v233_test.go
@@ -7,10 +7,10 @@ import (
"testing"
"code.gitea.io/gitea/models/migrations/base"
- "code.gitea.io/gitea/models/webhook"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/secret"
"code.gitea.io/gitea/modules/setting"
+ webhook_module "code.gitea.io/gitea/modules/webhook"
"github.com/stretchr/testify/assert"
)
@@ -18,9 +18,9 @@ import (
func Test_AddHeaderAuthorizationEncryptedColWebhook(t *testing.T) {
// Create Webhook table
type Webhook struct {
- ID int64 `xorm:"pk autoincr"`
- Type webhook.HookType `xorm:"VARCHAR(16) 'type'"`
- Meta string `xorm:"TEXT"` // store hook-specific attributes
+ ID int64 `xorm:"pk autoincr"`
+ Type webhook_module.HookType `xorm:"VARCHAR(16) 'type'"`
+ Meta string `xorm:"TEXT"` // store hook-specific attributes
// HeaderAuthorizationEncrypted should be accessed using HeaderAuthorization() and SetHeaderAuthorization()
HeaderAuthorizationEncrypted string `xorm:"TEXT"`
diff --git a/models/webhook/hooktask.go b/models/webhook/hooktask.go
index 2a37ff31d8..ccce1447bc 100644
--- a/models/webhook/hooktask.go
+++ b/models/webhook/hooktask.go
@@ -12,6 +12,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
+ webhook_module "code.gitea.io/gitea/modules/webhook"
gouuid "github.com/google/uuid"
)
@@ -107,7 +108,7 @@ type HookTask struct {
UUID string `xorm:"unique"`
api.Payloader `xorm:"-"`
PayloadContent string `xorm:"LONGTEXT"`
- EventType HookEventType
+ EventType webhook_module.HookEventType
IsDelivered bool
Delivered int64
DeliveredString string `xorm:"-"`
diff --git a/models/webhook/webhook.go b/models/webhook/webhook.go
index 5defc67fd7..c24404c42c 100644
--- a/models/webhook/webhook.go
+++ b/models/webhook/webhook.go
@@ -16,6 +16,7 @@ import (
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"
+ webhook_module "code.gitea.io/gitea/modules/webhook"
"xorm.io/builder"
)
@@ -46,7 +47,7 @@ type ErrHookTaskNotExist struct {
UUID string
}
-// IsErrWebhookNotExist checks if an error is a ErrWebhookNotExist.
+// IsErrHookTaskNotExist checks if an error is a ErrHookTaskNotExist.
func IsErrHookTaskNotExist(err error) bool {
_, ok := err.(ErrHookTaskNotExist)
return ok
@@ -117,84 +118,22 @@ func IsValidHookContentType(name string) bool {
return ok
}
-// HookEvents is a set of web hook events
-type HookEvents struct {
- Create bool `json:"create"`
- Delete bool `json:"delete"`
- Fork bool `json:"fork"`
- Issues bool `json:"issues"`
- IssueAssign bool `json:"issue_assign"`
- IssueLabel bool `json:"issue_label"`
- IssueMilestone bool `json:"issue_milestone"`
- IssueComment bool `json:"issue_comment"`
- Push bool `json:"push"`
- PullRequest bool `json:"pull_request"`
- PullRequestAssign bool `json:"pull_request_assign"`
- PullRequestLabel bool `json:"pull_request_label"`
- PullRequestMilestone bool `json:"pull_request_milestone"`
- PullRequestComment bool `json:"pull_request_comment"`
- PullRequestReview bool `json:"pull_request_review"`
- PullRequestSync bool `json:"pull_request_sync"`
- Wiki bool `json:"wiki"`
- Repository bool `json:"repository"`
- Release bool `json:"release"`
- Package bool `json:"package"`
-}
-
-// HookEvent represents events that will delivery hook.
-type HookEvent struct {
- PushOnly bool `json:"push_only"`
- SendEverything bool `json:"send_everything"`
- ChooseEvents bool `json:"choose_events"`
- BranchFilter string `json:"branch_filter"`
-
- HookEvents `json:"events"`
-}
-
-// HookType is the type of a webhook
-type HookType = string
-
-// Types of webhooks
-const (
- GITEA HookType = "gitea"
- GOGS HookType = "gogs"
- SLACK HookType = "slack"
- DISCORD HookType = "discord"
- DINGTALK HookType = "dingtalk"
- TELEGRAM HookType = "telegram"
- MSTEAMS HookType = "msteams"
- FEISHU HookType = "feishu"
- MATRIX HookType = "matrix"
- WECHATWORK HookType = "wechatwork"
- PACKAGIST HookType = "packagist"
-)
-
-// HookStatus is the status of a web hook
-type HookStatus int
-
-// Possible statuses of a web hook
-const (
- HookStatusNone = iota
- HookStatusSucceed
- HookStatusFail
-)
-
// Webhook represents a web hook object.
type Webhook struct {
- ID int64 `xorm:"pk autoincr"`
- RepoID int64 `xorm:"INDEX"` // An ID of 0 indicates either a default or system webhook
- OrgID int64 `xorm:"INDEX"`
- IsSystemWebhook bool
- URL string `xorm:"url TEXT"`
- HTTPMethod string `xorm:"http_method"`
- ContentType HookContentType
- Secret string `xorm:"TEXT"`
- Events string `xorm:"TEXT"`
- *HookEvent `xorm:"-"`
- IsActive bool `xorm:"INDEX"`
- Type HookType `xorm:"VARCHAR(16) 'type'"`
- Meta string `xorm:"TEXT"` // store hook-specific attributes
- LastStatus HookStatus // Last delivery status
+ ID int64 `xorm:"pk autoincr"`
+ RepoID int64 `xorm:"INDEX"` // An ID of 0 indicates either a default or system webhook
+ OrgID int64 `xorm:"INDEX"`
+ IsSystemWebhook bool
+ URL string `xorm:"url TEXT"`
+ HTTPMethod string `xorm:"http_method"`
+ ContentType HookContentType
+ Secret string `xorm:"TEXT"`
+ Events string `xorm:"TEXT"`
+ *webhook_module.HookEvent `xorm:"-"`
+ IsActive bool `xorm:"INDEX"`
+ Type webhook_module.HookType `xorm:"VARCHAR(16) 'type'"`
+ Meta string `xorm:"TEXT"` // store hook-specific attributes
+ LastStatus webhook_module.HookStatus // Last delivery status
// HeaderAuthorizationEncrypted should be accessed using HeaderAuthorization() and SetHeaderAuthorization()
HeaderAuthorizationEncrypted string `xorm:"TEXT"`
@@ -209,7 +148,7 @@ func init() {
// AfterLoad updates the webhook object upon setting a column
func (w *Webhook) AfterLoad() {
- w.HookEvent = &HookEvent{}
+ w.HookEvent = &webhook_module.HookEvent{}
if err := json.Unmarshal([]byte(w.Events), w.HookEvent); err != nil {
log.Error("Unmarshal[%d]: %v", w.ID, err)
}
@@ -362,34 +301,34 @@ func (w *Webhook) HasPackageEvent() bool {
// EventCheckers returns event checkers
func (w *Webhook) EventCheckers() []struct {
Has func() bool
- Type HookEventType
+ Type webhook_module.HookEventType
} {
return []struct {
Has func() bool
- Type HookEventType
+ Type webhook_module.HookEventType
}{
- {w.HasCreateEvent, HookEventCreate},
- {w.HasDeleteEvent, HookEventDelete},
- {w.HasForkEvent, HookEventFork},
- {w.HasPushEvent, HookEventPush},
- {w.HasIssuesEvent, HookEventIssues},
- {w.HasIssuesAssignEvent, HookEventIssueAssign},
- {w.HasIssuesLabelEvent, HookEventIssueLabel},
- {w.HasIssuesMilestoneEvent, HookEventIssueMilestone},
- {w.HasIssueCommentEvent, HookEventIssueComment},
- {w.HasPullRequestEvent, HookEventPullRequest},
- {w.HasPullRequestAssignEvent, HookEventPullRequestAssign},
- {w.HasPullRequestLabelEvent, HookEventPullRequestLabel},
- {w.HasPullRequestMilestoneEvent, HookEventPullRequestMilestone},
- {w.HasPullRequestCommentEvent, HookEventPullRequestComment},
- {w.HasPullRequestApprovedEvent, HookEventPullRequestReviewApproved},
- {w.HasPullRequestRejectedEvent, HookEventPullRequestReviewRejected},
- {w.HasPullRequestCommentEvent, HookEventPullRequestReviewComment},
- {w.HasPullRequestSyncEvent, HookEventPullRequestSync},
- {w.HasWikiEvent, HookEventWiki},
- {w.HasRepositoryEvent, HookEventRepository},
- {w.HasReleaseEvent, HookEventRelease},
- {w.HasPackageEvent, HookEventPackage},
+ {w.HasCreateEvent, webhook_module.HookEventCreate},
+ {w.HasDeleteEvent, webhook_module.HookEventDelete},
+ {w.HasForkEvent, webhook_module.HookEventFork},
+ {w.HasPushEvent, webhook_module.HookEventPush},
+ {w.HasIssuesEvent, webhook_module.HookEventIssues},
+ {w.HasIssuesAssignEvent, webhook_module.HookEventIssueAssign},
+ {w.HasIssuesLabelEvent, webhook_module.HookEventIssueLabel},
+ {w.HasIssuesMilestoneEvent, webhook_module.HookEventIssueMilestone},
+ {w.HasIssueCommentEvent, webhook_module.HookEventIssueComment},
+ {w.HasPullRequestEvent, webhook_module.HookEventPullRequest},
+ {w.HasPullRequestAssignEvent, webhook_module.HookEventPullRequestAssign},
+ {w.HasPullRequestLabelEvent, webhook_module.HookEventPullRequestLabel},
+ {w.HasPullRequestMilestoneEvent, webhook_module.HookEventPullRequestMilestone},
+ {w.HasPullRequestCommentEvent, webhook_module.HookEventPullRequestComment},
+ {w.HasPullRequestApprovedEvent, webhook_module.HookEventPullRequestReviewApproved},
+ {w.HasPullRequestRejectedEvent, webhook_module.HookEventPullRequestReviewRejected},
+ {w.HasPullRequestCommentEvent, webhook_module.HookEventPullRequestReviewComment},
+ {w.HasPullRequestSyncEvent, webhook_module.HookEventPullRequestSync},
+ {w.HasWikiEvent, webhook_module.HookEventWiki},
+ {w.HasRepositoryEvent, webhook_module.HookEventRepository},
+ {w.HasReleaseEvent, webhook_module.HookEventRelease},
+ {w.HasPackageEvent, webhook_module.HookEventPackage},
}
}
@@ -453,7 +392,7 @@ func getWebhook(bean *Webhook) (*Webhook, error) {
if err != nil {
return nil, err
} else if !has {
- return nil, ErrWebhookNotExist{bean.ID}
+ return nil, ErrWebhookNotExist{ID: bean.ID}
}
return bean, nil
}
@@ -541,7 +480,7 @@ func GetSystemOrDefaultWebhook(id int64) (*Webhook, error) {
if err != nil {
return nil, err
} else if !has {
- return nil, ErrWebhookNotExist{id}
+ return nil, ErrWebhookNotExist{ID: id}
}
return webhook, nil
}
diff --git a/models/webhook/webhook_test.go b/models/webhook/webhook_test.go
index 2bdafb61b6..c368fc620e 100644
--- a/models/webhook/webhook_test.go
+++ b/models/webhook/webhook_test.go
@@ -13,6 +13,7 @@ import (
"code.gitea.io/gitea/modules/json"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
+ webhook_module "code.gitea.io/gitea/modules/webhook"
"github.com/stretchr/testify/assert"
)
@@ -46,11 +47,11 @@ func TestWebhook_History(t *testing.T) {
func TestWebhook_UpdateEvent(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
webhook := unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 1})
- hookEvent := &HookEvent{
+ hookEvent := &webhook_module.HookEvent{
PushOnly: true,
SendEverything: false,
ChooseEvents: false,
- HookEvents: HookEvents{
+ HookEvents: webhook_module.HookEvents{
Create: false,
Push: true,
PullRequest: false,
@@ -59,7 +60,7 @@ func TestWebhook_UpdateEvent(t *testing.T) {
webhook.HookEvent = hookEvent
assert.NoError(t, webhook.UpdateEvent())
assert.NotEmpty(t, webhook.Events)
- actualHookEvent := &HookEvent{}
+ actualHookEvent := &webhook_module.HookEvent{}
assert.NoError(t, json.Unmarshal([]byte(webhook.Events), actualHookEvent))
assert.Equal(t, *hookEvent, *actualHookEvent)
}
@@ -74,13 +75,13 @@ func TestWebhook_EventsArray(t *testing.T) {
"package",
},
(&Webhook{
- HookEvent: &HookEvent{SendEverything: true},
+ HookEvent: &webhook_module.HookEvent{SendEverything: true},
}).EventsArray(),
)
assert.Equal(t, []string{"push"},
(&Webhook{
- HookEvent: &HookEvent{PushOnly: true},
+ HookEvent: &webhook_module.HookEvent{PushOnly: true},
}).EventsArray(),
)
}