diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-11-10 13:13:16 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-10 13:13:16 +0800 |
commit | 33fca2b537d36cf998dd27425b2bb8ed5b0965f3 (patch) | |
tree | 817f392502e1c176a5cd7e80290520cb940a8416 /services | |
parent | edbaa5d3f05b5ca397524587ba9db15edd61bc29 (diff) | |
download | gitea-33fca2b537d36cf998dd27425b2bb8ed5b0965f3.tar.gz gitea-33fca2b537d36cf998dd27425b2bb8ed5b0965f3.zip |
Move webhook into models/webhook/ (#17579)
Diffstat (limited to 'services')
-rw-r--r-- | services/auth/source/ldap/source_sync.go | 7 | ||||
-rw-r--r-- | services/auth/sync.go | 4 | ||||
-rw-r--r-- | services/webhook/deliver.go | 29 | ||||
-rw-r--r-- | services/webhook/dingtalk.go | 6 | ||||
-rw-r--r-- | services/webhook/dingtalk_test.go | 4 | ||||
-rw-r--r-- | services/webhook/discord.go | 22 | ||||
-rw-r--r-- | services/webhook/discord_test.go | 4 | ||||
-rw-r--r-- | services/webhook/feishu.go | 6 | ||||
-rw-r--r-- | services/webhook/feishu_test.go | 4 | ||||
-rw-r--r-- | services/webhook/matrix.go | 10 | ||||
-rw-r--r-- | services/webhook/matrix_test.go | 8 | ||||
-rw-r--r-- | services/webhook/msteams.go | 12 | ||||
-rw-r--r-- | services/webhook/msteams_test.go | 4 | ||||
-rw-r--r-- | services/webhook/payloader.go | 28 | ||||
-rw-r--r-- | services/webhook/slack.go | 8 | ||||
-rw-r--r-- | services/webhook/slack_test.go | 4 | ||||
-rw-r--r-- | services/webhook/telegram.go | 8 | ||||
-rw-r--r-- | services/webhook/telegram_test.go | 4 | ||||
-rw-r--r-- | services/webhook/webhook.go | 65 | ||||
-rw-r--r-- | services/webhook/webhook_test.go | 21 | ||||
-rw-r--r-- | services/webhook/wechatwork.go | 6 |
21 files changed, 134 insertions, 130 deletions
diff --git a/services/auth/source/ldap/source_sync.go b/services/auth/source/ldap/source_sync.go index 2df97aabd3..4508d1514a 100644 --- a/services/auth/source/ldap/source_sync.go +++ b/services/auth/source/ldap/source_sync.go @@ -11,6 +11,7 @@ import ( "strings" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/log" ) @@ -31,7 +32,7 @@ func (source *Source) Sync(ctx context.Context, updateExisting bool) error { select { case <-ctx.Done(): log.Warn("SyncExternalUsers: Cancelled before update of %s", source.loginSource.Name) - return models.ErrCancelledf("Before update of %s", source.loginSource.Name) + return db.ErrCancelledf("Before update of %s", source.loginSource.Name) default: } @@ -70,7 +71,7 @@ func (source *Source) Sync(ctx context.Context, updateExisting bool) error { log.Error("RewriteAllPublicKeys: %v", err) } } - return models.ErrCancelledf("During update of %s before completed update of users", source.loginSource.Name) + return db.ErrCancelledf("During update of %s before completed update of users", source.loginSource.Name) default: } if len(su.Username) == 0 { @@ -177,7 +178,7 @@ func (source *Source) Sync(ctx context.Context, updateExisting bool) error { select { case <-ctx.Done(): log.Warn("SyncExternalUsers: Cancelled during update of %s before delete users", source.loginSource.Name) - return models.ErrCancelledf("During update of %s before delete users", source.loginSource.Name) + return db.ErrCancelledf("During update of %s before delete users", source.loginSource.Name) default: } diff --git a/services/auth/sync.go b/services/auth/sync.go index 6d69650e5b..494dfc3a38 100644 --- a/services/auth/sync.go +++ b/services/auth/sync.go @@ -7,7 +7,7 @@ package auth import ( "context" - "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/login" "code.gitea.io/gitea/modules/log" ) @@ -29,7 +29,7 @@ func SyncExternalUsers(ctx context.Context, updateExisting bool) error { select { case <-ctx.Done(): log.Warn("SyncExternalUsers: Cancelled before update of %s", s.Name) - return models.ErrCancelledf("Before update of %s", s.Name) + return db.ErrCancelledf("Before update of %s", s.Name) default: } diff --git a/services/webhook/deliver.go b/services/webhook/deliver.go index 04cec4c1c4..da7052fd9b 100644 --- a/services/webhook/deliver.go +++ b/services/webhook/deliver.go @@ -22,19 +22,20 @@ import ( "syscall" "time" - "code.gitea.io/gitea/models" + webhook_model "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/proxy" "code.gitea.io/gitea/modules/setting" + "github.com/gobwas/glob" ) var contextKeyWebhookRequest interface{} = "contextKeyWebhookRequest" // Deliver deliver hook task -func Deliver(t *models.HookTask) error { - w, err := models.GetWebhookByID(t.HookID) +func Deliver(t *webhook_model.HookTask) error { + w, err := webhook_model.GetWebhookByID(t.HookID) if err != nil { return err } @@ -58,14 +59,14 @@ func Deliver(t *models.HookTask) error { fallthrough case http.MethodPost: switch w.ContentType { - case models.ContentTypeJSON: + case webhook_model.ContentTypeJSON: req, err = http.NewRequest("POST", w.URL, strings.NewReader(t.PayloadContent)) if err != nil { return err } req.Header.Set("Content-Type", "application/json") - case models.ContentTypeForm: + case webhook_model.ContentTypeForm: var forms = url.Values{ "payload": []string{t.PayloadContent}, } @@ -91,7 +92,7 @@ func Deliver(t *models.HookTask) error { } case http.MethodPut: switch w.Type { - case models.MATRIX: + case webhook_model.MATRIX: req, err = getMatrixHookRequest(w, t) if err != nil { return err @@ -133,7 +134,7 @@ func Deliver(t *models.HookTask) error { req.Header["X-GitHub-Event-Type"] = []string{eventType} // Record delivery information. - t.RequestInfo = &models.HookRequest{ + t.RequestInfo = &webhook_model.HookRequest{ URL: req.URL.String(), HTTPMethod: req.Method, Headers: map[string]string{}, @@ -142,7 +143,7 @@ func Deliver(t *models.HookTask) error { t.RequestInfo.Headers[k] = strings.Join(vals, ",") } - t.ResponseInfo = &models.HookResponse{ + t.ResponseInfo = &webhook_model.HookResponse{ Headers: map[string]string{}, } @@ -154,17 +155,17 @@ func Deliver(t *models.HookTask) error { log.Trace("Hook delivery failed: %s", t.UUID) } - if err := models.UpdateHookTask(t); err != nil { + if err := webhook_model.UpdateHookTask(t); err != nil { log.Error("UpdateHookTask [%d]: %v", t.ID, err) } // Update webhook last delivery status. if t.IsSucceed { - w.LastStatus = models.HookStatusSucceed + w.LastStatus = webhook_model.HookStatusSucceed } else { - w.LastStatus = models.HookStatusFail + w.LastStatus = webhook_model.HookStatusFail } - if err = models.UpdateWebhookLastStatus(w); err != nil { + if err = webhook_model.UpdateWebhookLastStatus(w); err != nil { log.Error("UpdateWebhookLastStatus: %v", err) return } @@ -206,7 +207,7 @@ func DeliverHooks(ctx context.Context) { return default: } - tasks, err := models.FindUndeliveredHookTasks() + tasks, err := webhook_model.FindUndeliveredHookTasks() if err != nil { log.Error("DeliverHooks: %v", err) return @@ -240,7 +241,7 @@ func DeliverHooks(ctx context.Context) { continue } - tasks, err := models.FindRepoUndeliveredHookTasks(repoID) + tasks, err := webhook_model.FindRepoUndeliveredHookTasks(repoID) if err != nil { log.Error("Get repository [%d] hook tasks: %v", repoID, err) continue diff --git a/services/webhook/dingtalk.go b/services/webhook/dingtalk.go index 7d352db18c..88e4078922 100644 --- a/services/webhook/dingtalk.go +++ b/services/webhook/dingtalk.go @@ -9,7 +9,7 @@ import ( "net/url" "strings" - "code.gitea.io/gitea/models" + webhook_model "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" api "code.gitea.io/gitea/modules/structs" @@ -123,7 +123,7 @@ func (d *DingtalkPayload) PullRequest(p *api.PullRequestPayload) (api.Payloader, } // Review implements PayloadConvertor Review method -func (d *DingtalkPayload) Review(p *api.PullRequestPayload, event models.HookEventType) (api.Payloader, error) { +func (d *DingtalkPayload) Review(p *api.PullRequestPayload, event webhook_model.HookEventType) (api.Payloader, error) { var text, title string switch p.Action { case api.HookIssueReviewed: @@ -185,6 +185,6 @@ func createDingtalkPayload(title, text, singleTitle, singleURL string) *Dingtalk } // GetDingtalkPayload converts a ding talk webhook into a DingtalkPayload -func GetDingtalkPayload(p api.Payloader, event models.HookEventType, meta string) (api.Payloader, error) { +func GetDingtalkPayload(p api.Payloader, event webhook_model.HookEventType, meta string) (api.Payloader, error) { return convertPayloader(new(DingtalkPayload), p, event) } diff --git a/services/webhook/dingtalk_test.go b/services/webhook/dingtalk_test.go index 5c1d187523..b66b5e43a8 100644 --- a/services/webhook/dingtalk_test.go +++ b/services/webhook/dingtalk_test.go @@ -8,7 +8,7 @@ import ( "net/url" "testing" - "code.gitea.io/gitea/models" + webhook_model "code.gitea.io/gitea/models/webhook" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -163,7 +163,7 @@ func TestDingTalkPayload(t *testing.T) { p.Action = api.HookIssueReviewed d := new(DingtalkPayload) - pl, err := d.Review(p, models.HookEventPullRequestReviewApproved) + pl, err := d.Review(p, webhook_model.HookEventPullRequestReviewApproved) require.NoError(t, err) require.NotNil(t, pl) require.IsType(t, &DingtalkPayload{}, pl) diff --git a/services/webhook/discord.go b/services/webhook/discord.go index 9a8f8a27a5..3de50a8a2f 100644 --- a/services/webhook/discord.go +++ b/services/webhook/discord.go @@ -10,7 +10,7 @@ import ( "strconv" "strings" - "code.gitea.io/gitea/models" + webhook_model "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" @@ -66,7 +66,7 @@ type ( ) // GetDiscordHook returns discord metadata -func GetDiscordHook(w *models.Webhook) *DiscordMeta { +func GetDiscordHook(w *webhook_model.Webhook) *DiscordMeta { s := &DiscordMeta{} if err := json.Unmarshal([]byte(w.Meta), s); err != nil { log.Error("webhook.GetDiscordHook(%d): %v", w.ID, err) @@ -191,7 +191,7 @@ func (d *DiscordPayload) PullRequest(p *api.PullRequestPayload) (api.Payloader, } // Review implements PayloadConvertor Review method -func (d *DiscordPayload) Review(p *api.PullRequestPayload, event models.HookEventType) (api.Payloader, error) { +func (d *DiscordPayload) Review(p *api.PullRequestPayload, event webhook_model.HookEventType) (api.Payloader, error) { var text, title string var color int switch p.Action { @@ -205,11 +205,11 @@ func (d *DiscordPayload) Review(p *api.PullRequestPayload, event models.HookEven text = p.Review.Content switch event { - case models.HookEventPullRequestReviewApproved: + case webhook_model.HookEventPullRequestReviewApproved: color = greenColor - case models.HookEventPullRequestReviewRejected: + case webhook_model.HookEventPullRequestReviewRejected: color = redColor - case models.HookEventPullRequestComment: + case webhook_model.HookEventPullRequestComment: color = greyColor default: color = yellowColor @@ -244,7 +244,7 @@ func (d *DiscordPayload) Release(p *api.ReleasePayload) (api.Payloader, error) { } // GetDiscordPayload converts a discord webhook into a DiscordPayload -func GetDiscordPayload(p api.Payloader, event models.HookEventType, meta string) (api.Payloader, error) { +func GetDiscordPayload(p api.Payloader, event webhook_model.HookEventType, meta string) (api.Payloader, error) { s := new(DiscordPayload) discord := &DiscordMeta{} @@ -257,14 +257,14 @@ func GetDiscordPayload(p api.Payloader, event models.HookEventType, meta string) return convertPayloader(s, p, event) } -func parseHookPullRequestEventType(event models.HookEventType) (string, error) { +func parseHookPullRequestEventType(event webhook_model.HookEventType) (string, error) { switch event { - case models.HookEventPullRequestReviewApproved: + case webhook_model.HookEventPullRequestReviewApproved: return "approved", nil - case models.HookEventPullRequestReviewRejected: + case webhook_model.HookEventPullRequestReviewRejected: return "rejected", nil - case models.HookEventPullRequestComment: + case webhook_model.HookEventPullRequestComment: return "comment", nil default: diff --git a/services/webhook/discord_test.go b/services/webhook/discord_test.go index fd7d2856c7..8fe20c0102 100644 --- a/services/webhook/discord_test.go +++ b/services/webhook/discord_test.go @@ -7,7 +7,7 @@ package webhook import ( "testing" - "code.gitea.io/gitea/models" + webhook_model "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" @@ -180,7 +180,7 @@ func TestDiscordPayload(t *testing.T) { p.Action = api.HookIssueReviewed d := new(DiscordPayload) - pl, err := d.Review(p, models.HookEventPullRequestReviewApproved) + pl, err := d.Review(p, webhook_model.HookEventPullRequestReviewApproved) require.NoError(t, err) require.NotNil(t, pl) require.IsType(t, &DiscordPayload{}, pl) diff --git a/services/webhook/feishu.go b/services/webhook/feishu.go index 4428d54f92..18858c0994 100644 --- a/services/webhook/feishu.go +++ b/services/webhook/feishu.go @@ -8,7 +8,7 @@ import ( "fmt" "strings" - "code.gitea.io/gitea/models" + webhook_model "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" api "code.gitea.io/gitea/modules/structs" @@ -120,7 +120,7 @@ func (f *FeishuPayload) PullRequest(p *api.PullRequestPayload) (api.Payloader, e } // Review implements PayloadConvertor Review method -func (f *FeishuPayload) Review(p *api.PullRequestPayload, event models.HookEventType) (api.Payloader, error) { +func (f *FeishuPayload) Review(p *api.PullRequestPayload, event webhook_model.HookEventType) (api.Payloader, error) { action, err := parseHookPullRequestEventType(event) if err != nil { return nil, err @@ -155,6 +155,6 @@ func (f *FeishuPayload) Release(p *api.ReleasePayload) (api.Payloader, error) { } // GetFeishuPayload converts a ding talk webhook into a FeishuPayload -func GetFeishuPayload(p api.Payloader, event models.HookEventType, meta string) (api.Payloader, error) { +func GetFeishuPayload(p api.Payloader, event webhook_model.HookEventType, meta string) (api.Payloader, error) { return convertPayloader(new(FeishuPayload), p, event) } diff --git a/services/webhook/feishu_test.go b/services/webhook/feishu_test.go index 7f3508c145..d862b015e7 100644 --- a/services/webhook/feishu_test.go +++ b/services/webhook/feishu_test.go @@ -7,7 +7,7 @@ package webhook import ( "testing" - "code.gitea.io/gitea/models" + webhook_model "code.gitea.io/gitea/models/webhook" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -125,7 +125,7 @@ func TestFeishuPayload(t *testing.T) { p.Action = api.HookIssueReviewed d := new(FeishuPayload) - pl, err := d.Review(p, models.HookEventPullRequestReviewApproved) + pl, err := d.Review(p, webhook_model.HookEventPullRequestReviewApproved) require.NoError(t, err) require.NotNil(t, pl) require.IsType(t, &FeishuPayload{}, pl) diff --git a/services/webhook/matrix.go b/services/webhook/matrix.go index 10e0c8fd15..08adaef6fd 100644 --- a/services/webhook/matrix.go +++ b/services/webhook/matrix.go @@ -13,7 +13,7 @@ import ( "regexp" "strings" - "code.gitea.io/gitea/models" + webhook_model "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" @@ -37,7 +37,7 @@ var messageTypeText = map[int]string{ } // GetMatrixHook returns Matrix metadata -func GetMatrixHook(w *models.Webhook) *MatrixMeta { +func GetMatrixHook(w *webhook_model.Webhook) *MatrixMeta { s := &MatrixMeta{} if err := json.Unmarshal([]byte(w.Meta), s); err != nil { log.Error("webhook.GetMatrixHook(%d): %v", w.ID, err) @@ -185,7 +185,7 @@ func (m *MatrixPayloadUnsafe) PullRequest(p *api.PullRequestPayload) (api.Payloa } // Review implements PayloadConvertor Review method -func (m *MatrixPayloadUnsafe) Review(p *api.PullRequestPayload, event models.HookEventType) (api.Payloader, error) { +func (m *MatrixPayloadUnsafe) Review(p *api.PullRequestPayload, event webhook_model.HookEventType) (api.Payloader, error) { senderLink := MatrixLinkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName) title := fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title) titleLink := fmt.Sprintf("%s/pulls/%d", p.Repository.HTMLURL, p.Index) @@ -222,7 +222,7 @@ func (m *MatrixPayloadUnsafe) Repository(p *api.RepositoryPayload) (api.Payloade } // GetMatrixPayload converts a Matrix webhook into a MatrixPayloadUnsafe -func GetMatrixPayload(p api.Payloader, event models.HookEventType, meta string) (api.Payloader, error) { +func GetMatrixPayload(p api.Payloader, event webhook_model.HookEventType, meta string) (api.Payloader, error) { s := new(MatrixPayloadUnsafe) matrix := &MatrixMeta{} @@ -257,7 +257,7 @@ func getMessageBody(htmlText string) string { // getMatrixHookRequest creates a new request which contains an Authorization header. // The access_token is removed from t.PayloadContent -func getMatrixHookRequest(w *models.Webhook, t *models.HookTask) (*http.Request, error) { +func getMatrixHookRequest(w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, error) { payloadunsafe := MatrixPayloadUnsafe{} if err := json.Unmarshal([]byte(t.PayloadContent), &payloadunsafe); err != nil { log.Error("Matrix Hook delivery failed: %v", err) diff --git a/services/webhook/matrix_test.go b/services/webhook/matrix_test.go index 451dff6949..3cc7c7518f 100644 --- a/services/webhook/matrix_test.go +++ b/services/webhook/matrix_test.go @@ -7,7 +7,7 @@ package webhook import ( "testing" - "code.gitea.io/gitea/models" + webhook_model "code.gitea.io/gitea/models/webhook" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -134,7 +134,7 @@ func TestMatrixPayload(t *testing.T) { p.Action = api.HookIssueReviewed d := new(MatrixPayloadUnsafe) - pl, err := d.Review(p, models.HookEventPullRequestReviewApproved) + pl, err := d.Review(p, webhook_model.HookEventPullRequestReviewApproved) require.NoError(t, err) require.NotNil(t, pl) require.IsType(t, &MatrixPayloadUnsafe{}, pl) @@ -184,9 +184,9 @@ func TestMatrixJSONPayload(t *testing.T) { } func TestMatrixHookRequest(t *testing.T) { - w := &models.Webhook{} + w := &webhook_model.Webhook{} - h := &models.HookTask{ + h := &webhook_model.HookTask{ PayloadContent: `{ "body": "[[user1/test](http://localhost:3000/user1/test)] user1 pushed 1 commit to [master](http://localhost:3000/user1/test/src/branch/master):\n[5175ef2](http://localhost:3000/user1/test/commit/5175ef26201c58b035a3404b3fe02b4e8d436eee): Merge pull request 'Change readme.md' (#2) from add-matrix-webhook into master\n\nReviewed-on: http://localhost:3000/user1/test/pulls/2\n - user1", "msgtype": "m.notice", diff --git a/services/webhook/msteams.go b/services/webhook/msteams.go index ed5e659058..2b88bb23ff 100644 --- a/services/webhook/msteams.go +++ b/services/webhook/msteams.go @@ -8,7 +8,7 @@ import ( "fmt" "strings" - "code.gitea.io/gitea/models" + webhook_model "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" api "code.gitea.io/gitea/modules/structs" @@ -206,7 +206,7 @@ func (m *MSTeamsPayload) PullRequest(p *api.PullRequestPayload) (api.Payloader, } // Review implements PayloadConvertor Review method -func (m *MSTeamsPayload) Review(p *api.PullRequestPayload, event models.HookEventType) (api.Payloader, error) { +func (m *MSTeamsPayload) Review(p *api.PullRequestPayload, event webhook_model.HookEventType) (api.Payloader, error) { var text, title string var color int switch p.Action { @@ -220,11 +220,11 @@ func (m *MSTeamsPayload) Review(p *api.PullRequestPayload, event models.HookEven text = p.Review.Content switch event { - case models.HookEventPullRequestReviewApproved: + case webhook_model.HookEventPullRequestReviewApproved: color = greenColor - case models.HookEventPullRequestReviewRejected: + case webhook_model.HookEventPullRequestReviewRejected: color = redColor - case models.HookEventPullRequestComment: + case webhook_model.HookEventPullRequestComment: color = greyColor default: color = yellowColor @@ -283,7 +283,7 @@ func (m *MSTeamsPayload) Release(p *api.ReleasePayload) (api.Payloader, error) { } // GetMSTeamsPayload converts a MSTeams webhook into a MSTeamsPayload -func GetMSTeamsPayload(p api.Payloader, event models.HookEventType, meta string) (api.Payloader, error) { +func GetMSTeamsPayload(p api.Payloader, event webhook_model.HookEventType, meta string) (api.Payloader, error) { return convertPayloader(new(MSTeamsPayload), p, event) } diff --git a/services/webhook/msteams_test.go b/services/webhook/msteams_test.go index 2f54c39d39..3fdf47c1ae 100644 --- a/services/webhook/msteams_test.go +++ b/services/webhook/msteams_test.go @@ -7,7 +7,7 @@ package webhook import ( "testing" - "code.gitea.io/gitea/models" + webhook_model "code.gitea.io/gitea/models/webhook" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -278,7 +278,7 @@ func TestMSTeamsPayload(t *testing.T) { p.Action = api.HookIssueReviewed d := new(MSTeamsPayload) - pl, err := d.Review(p, models.HookEventPullRequestReviewApproved) + pl, err := d.Review(p, webhook_model.HookEventPullRequestReviewApproved) require.NoError(t, err) require.NotNil(t, pl) require.IsType(t, &MSTeamsPayload{}, pl) diff --git a/services/webhook/payloader.go b/services/webhook/payloader.go index f1cdaf6595..0e09dd1b1e 100644 --- a/services/webhook/payloader.go +++ b/services/webhook/payloader.go @@ -5,7 +5,7 @@ package webhook import ( - "code.gitea.io/gitea/models" + webhook_model "code.gitea.io/gitea/models/webhook" api "code.gitea.io/gitea/modules/structs" ) @@ -19,37 +19,37 @@ type PayloadConvertor interface { IssueComment(*api.IssueCommentPayload) (api.Payloader, error) Push(*api.PushPayload) (api.Payloader, error) PullRequest(*api.PullRequestPayload) (api.Payloader, error) - Review(*api.PullRequestPayload, models.HookEventType) (api.Payloader, error) + Review(*api.PullRequestPayload, webhook_model.HookEventType) (api.Payloader, error) Repository(*api.RepositoryPayload) (api.Payloader, error) Release(*api.ReleasePayload) (api.Payloader, error) } -func convertPayloader(s PayloadConvertor, p api.Payloader, event models.HookEventType) (api.Payloader, error) { +func convertPayloader(s PayloadConvertor, p api.Payloader, event webhook_model.HookEventType) (api.Payloader, error) { switch event { - case models.HookEventCreate: + case webhook_model.HookEventCreate: return s.Create(p.(*api.CreatePayload)) - case models.HookEventDelete: + case webhook_model.HookEventDelete: return s.Delete(p.(*api.DeletePayload)) - case models.HookEventFork: + case webhook_model.HookEventFork: return s.Fork(p.(*api.ForkPayload)) - case models.HookEventIssues, models.HookEventIssueAssign, models.HookEventIssueLabel, models.HookEventIssueMilestone: + case webhook_model.HookEventIssues, webhook_model.HookEventIssueAssign, webhook_model.HookEventIssueLabel, webhook_model.HookEventIssueMilestone: return s.Issue(p.(*api.IssuePayload)) - case models.HookEventIssueComment, models.HookEventPullRequestComment: + case webhook_model.HookEventIssueComment, webhook_model.HookEventPullRequestComment: pl, ok := p.(*api.IssueCommentPayload) if ok { return s.IssueComment(pl) } return s.PullRequest(p.(*api.PullRequestPayload)) - case models.HookEventPush: + case webhook_model.HookEventPush: return s.Push(p.(*api.PushPayload)) - case models.HookEventPullRequest, models.HookEventPullRequestAssign, models.HookEventPullRequestLabel, - models.HookEventPullRequestMilestone, models.HookEventPullRequestSync: + case webhook_model.HookEventPullRequest, webhook_model.HookEventPullRequestAssign, webhook_model.HookEventPullRequestLabel, + webhook_model.HookEventPullRequestMilestone, webhook_model.HookEventPullRequestSync: return s.PullRequest(p.(*api.PullRequestPayload)) - case models.HookEventPullRequestReviewApproved, models.HookEventPullRequestReviewRejected, models.HookEventPullRequestReviewComment: + case webhook_model.HookEventPullRequestReviewApproved, webhook_model.HookEventPullRequestReviewRejected, webhook_model.HookEventPullRequestReviewComment: return s.Review(p.(*api.PullRequestPayload), event) - case models.HookEventRepository: + case webhook_model.HookEventRepository: return s.Repository(p.(*api.RepositoryPayload)) - case models.HookEventRelease: + case webhook_model.HookEventRelease: return s.Release(p.(*api.ReleasePayload)) } return s, nil diff --git a/services/webhook/slack.go b/services/webhook/slack.go index 803b5de0b2..575aedf0c0 100644 --- a/services/webhook/slack.go +++ b/services/webhook/slack.go @@ -9,7 +9,7 @@ import ( "fmt" "strings" - "code.gitea.io/gitea/models" + webhook_model "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" @@ -26,7 +26,7 @@ type SlackMeta struct { } // GetSlackHook returns slack metadata -func GetSlackHook(w *models.Webhook) *SlackMeta { +func GetSlackHook(w *webhook_model.Webhook) *SlackMeta { s := &SlackMeta{} if err := json.Unmarshal([]byte(w.Meta), s); err != nil { log.Error("webhook.GetSlackHook(%d): %v", w.ID, err) @@ -226,7 +226,7 @@ func (s *SlackPayload) PullRequest(p *api.PullRequestPayload) (api.Payloader, er } // Review implements PayloadConvertor Review method -func (s *SlackPayload) Review(p *api.PullRequestPayload, event models.HookEventType) (api.Payloader, error) { +func (s *SlackPayload) Review(p *api.PullRequestPayload, event webhook_model.HookEventType) (api.Payloader, error) { senderLink := SlackLinkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName) title := fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title) titleLink := fmt.Sprintf("%s/pulls/%d", p.Repository.HTMLURL, p.Index) @@ -273,7 +273,7 @@ func (s *SlackPayload) createPayload(text string, attachments []SlackAttachment) } // GetSlackPayload converts a slack webhook into a SlackPayload -func GetSlackPayload(p api.Payloader, event models.HookEventType, meta string) (api.Payloader, error) { +func GetSlackPayload(p api.Payloader, event webhook_model.HookEventType, meta string) (api.Payloader, error) { s := new(SlackPayload) slack := &SlackMeta{} diff --git a/services/webhook/slack_test.go b/services/webhook/slack_test.go index 3f279810c9..1fa7777328 100644 --- a/services/webhook/slack_test.go +++ b/services/webhook/slack_test.go @@ -7,7 +7,7 @@ package webhook import ( "testing" - "code.gitea.io/gitea/models" + webhook_model "code.gitea.io/gitea/models/webhook" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -125,7 +125,7 @@ func TestSlackPayload(t *testing.T) { p.Action = api.HookIssueReviewed d := new(SlackPayload) - pl, err := d.Review(p, models.HookEventPullRequestReviewApproved) + pl, err := d.Review(p, webhook_model.HookEventPullRequestReviewApproved) require.NoError(t, err) require.NotNil(t, pl) require.IsType(t, &SlackPayload{}, pl) diff --git a/services/webhook/telegram.go b/services/webhook/telegram.go index c5a26f687d..93c464fe73 100644 --- a/services/webhook/telegram.go +++ b/services/webhook/telegram.go @@ -8,7 +8,7 @@ import ( "fmt" "strings" - "code.gitea.io/gitea/models" + webhook_model "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" @@ -32,7 +32,7 @@ type ( ) // GetTelegramHook returns telegram metadata -func GetTelegramHook(w *models.Webhook) *TelegramMeta { +func GetTelegramHook(w *webhook_model.Webhook) *TelegramMeta { s := &TelegramMeta{} if err := json.Unmarshal([]byte(w.Meta), s); err != nil { log.Error("webhook.GetTelegramHook(%d): %v", w.ID, err) @@ -143,7 +143,7 @@ func (t *TelegramPayload) PullRequest(p *api.PullRequestPayload) (api.Payloader, } // Review implements PayloadConvertor Review method -func (t *TelegramPayload) Review(p *api.PullRequestPayload, event models.HookEventType) (api.Payloader, error) { +func (t *TelegramPayload) Review(p *api.PullRequestPayload, event webhook_model.HookEventType) (api.Payloader, error) { var text, attachmentText string switch p.Action { case api.HookIssueReviewed: @@ -181,7 +181,7 @@ func (t *TelegramPayload) Release(p *api.ReleasePayload) (api.Payloader, error) } // GetTelegramPayload converts a telegram webhook into a TelegramPayload -func GetTelegramPayload(p api.Payloader, event models.HookEventType, meta string) (api.Payloader, error) { +func GetTelegramPayload(p api.Payloader, event webhook_model.HookEventType, meta string) (api.Payloader, error) { return convertPayloader(new(TelegramPayload), p, event) } diff --git a/services/webhook/telegram_test.go b/services/webhook/telegram_test.go index 037a2481d6..6a3682847c 100644 --- a/services/webhook/telegram_test.go +++ b/services/webhook/telegram_test.go @@ -7,7 +7,7 @@ package webhook import ( "testing" - "code.gitea.io/gitea/models" + webhook_model "code.gitea.io/gitea/models/webhook" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -125,7 +125,7 @@ func TestTelegramPayload(t *testing.T) { p.Action = api.HookIssueReviewed d := new(TelegramPayload) - pl, err := d.Review(p, models.HookEventPullRequestReviewApproved) + pl, err := d.Review(p, webhook_model.HookEventPullRequestReviewApproved) require.NoError(t, err) require.NotNil(t, pl) require.IsType(t, &TelegramPayload{}, pl) diff --git a/services/webhook/webhook.go b/services/webhook/webhook.go index 00b2ef05b8..d4fe4e3bcc 100644 --- a/services/webhook/webhook.go +++ b/services/webhook/webhook.go @@ -9,6 +9,7 @@ import ( "strings" "code.gitea.io/gitea/models" + webhook_model "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" @@ -20,42 +21,42 @@ import ( ) type webhook struct { - name models.HookType - payloadCreator func(p api.Payloader, event models.HookEventType, meta string) (api.Payloader, error) + name webhook_model.HookType + payloadCreator func(p api.Payloader, event webhook_model.HookEventType, meta string) (api.Payloader, error) } var ( - webhooks = map[models.HookType]*webhook{ - models.SLACK: { - name: models.SLACK, + webhooks = map[webhook_model.HookType]*webhook{ + webhook_model.SLACK: { + name: webhook_model.SLACK, payloadCreator: GetSlackPayload, }, - models.DISCORD: { - name: models.DISCORD, + webhook_model.DISCORD: { + name: webhook_model.DISCORD, payloadCreator: GetDiscordPayload, }, - models.DINGTALK: { - name: models.DINGTALK, + webhook_model.DINGTALK: { + name: webhook_model.DINGTALK, payloadCreator: GetDingtalkPayload, }, - models.TELEGRAM: { - name: models.TELEGRAM, + webhook_model.TELEGRAM: { + name: webhook_model.TELEGRAM, payloadCreator: GetTelegramPayload, }, - models.MSTEAMS: { - name: models.MSTEAMS, + webhook_model.MSTEAMS: { + name: webhook_model.MSTEAMS, payloadCreator: GetMSTeamsPayload, }, - models.FEISHU: { - name: models.FEISHU, + webhook_model.FEISHU: { + name: webhook_model.FEISHU, payloadCreator: GetFeishuPayload, }, - models.MATRIX: { - name: models.MATRIX, + webhook_model.MATRIX: { + name: webhook_model.MATRIX, payloadCreator: GetMatrixPayload, }, - models.WECHATWORK: { - name: models.WECHATWORK, + webhook_model.WECHATWORK: { + name: webhook_model.WECHATWORK, payloadCreator: GetWechatworkPayload, }, } @@ -63,15 +64,15 @@ var ( // RegisterWebhook registers a webhook func RegisterWebhook(name string, webhook *webhook) { - webhooks[models.HookType(name)] = webhook + webhooks[webhook_model.HookType(name)] = webhook } // IsValidHookTaskType returns true if a webhook registered func IsValidHookTaskType(name string) bool { - if name == models.GITEA || name == models.GOGS { + if name == webhook_model.GITEA || name == webhook_model.GOGS { return true } - _, ok := webhooks[models.HookType(name)] + _, ok := webhooks[webhook_model.HookType(name)] return ok } @@ -98,7 +99,7 @@ func getPayloadBranch(p api.Payloader) string { } // PrepareWebhook adds special webhook to task queue for given payload. -func PrepareWebhook(w *models.Webhook, repo *models.Repository, event models.HookEventType, p api.Payloader) error { +func PrepareWebhook(w *webhook_model.Webhook, repo *models.Repository, event webhook_model.HookEventType, p api.Payloader) error { if err := prepareWebhook(w, repo, event, p); err != nil { return err } @@ -107,7 +108,7 @@ func PrepareWebhook(w *models.Webhook, repo *models.Repository, event models.Hoo return nil } -func checkBranch(w *models.Webhook, branch string) bool { +func checkBranch(w *webhook_model.Webhook, branch string) bool { if w.BranchFilter == "" || w.BranchFilter == "*" { return true } @@ -122,7 +123,7 @@ func checkBranch(w *models.Webhook, branch string) bool { return g.Match(branch) } -func prepareWebhook(w *models.Webhook, repo *models.Repository, event models.HookEventType, p api.Payloader) error { +func prepareWebhook(w *webhook_model.Webhook, repo *models.Repository, event webhook_model.HookEventType, p api.Payloader) error { // Skip sending if webhooks are disabled. if setting.DisableWebhooks { return nil @@ -141,7 +142,7 @@ func prepareWebhook(w *models.Webhook, repo *models.Repository, event models.Hoo // Avoid sending "0 new commits" to non-integration relevant webhooks (e.g. slack, discord, etc.). // Integration webhooks (e.g. drone) still receive the required data. if pushEvent, ok := p.(*api.PushPayload); ok && - w.Type != models.GITEA && w.Type != models.GOGS && + w.Type != webhook_model.GITEA && w.Type != webhook_model.GOGS && len(pushEvent.Commits) == 0 { return nil } @@ -167,7 +168,7 @@ func prepareWebhook(w *models.Webhook, repo *models.Repository, event models.Hoo payloader = p } - if err = models.CreateHookTask(&models.HookTask{ + if err = webhook_model.CreateHookTask(&webhook_model.HookTask{ RepoID: repo.ID, HookID: w.ID, Payloader: payloader, @@ -179,7 +180,7 @@ func prepareWebhook(w *models.Webhook, repo *models.Repository, event models.Hoo } // PrepareWebhooks adds new webhooks to task queue for given payload. -func PrepareWebhooks(repo *models.Repository, event models.HookEventType, p api.Payloader) error { +func PrepareWebhooks(repo *models.Repository, event webhook_model.HookEventType, p api.Payloader) error { if err := prepareWebhooks(repo, event, p); err != nil { return err } @@ -188,8 +189,8 @@ func PrepareWebhooks(repo *models.Repository, event models.HookEventType, p api. return nil } -func prepareWebhooks(repo *models.Repository, event models.HookEventType, p api.Payloader) error { - ws, err := models.ListWebhooksByOpts(&models.ListWebhookOptions{ +func prepareWebhooks(repo *models.Repository, event webhook_model.HookEventType, p api.Payloader) error { + ws, err := webhook_model.ListWebhooksByOpts(&webhook_model.ListWebhookOptions{ RepoID: repo.ID, IsActive: util.OptionalBoolTrue, }) @@ -200,7 +201,7 @@ func prepareWebhooks(repo *models.Repository, event models.HookEventType, p api. // check if repo belongs to org and append additional webhooks if repo.MustOwner().IsOrganization() { // get hooks for org - orgHooks, err := models.ListWebhooksByOpts(&models.ListWebhookOptions{ + orgHooks, err := webhook_model.ListWebhooksByOpts(&webhook_model.ListWebhookOptions{ OrgID: repo.OwnerID, IsActive: util.OptionalBoolTrue, }) @@ -211,7 +212,7 @@ func prepareWebhooks(repo *models.Repository, event models.HookEventType, p api. } // Add any admin-defined system webhooks - systemHooks, err := models.GetSystemWebhooks() + systemHooks, err := webhook_model.GetSystemWebhooks() if err != nil { return fmt.Errorf("GetSystemWebhooks: %v", err) } diff --git a/services/webhook/webhook_test.go b/services/webhook/webhook_test.go index 095b30713c..75f19e50ce 100644 --- a/services/webhook/webhook_test.go +++ b/services/webhook/webhook_test.go @@ -9,12 +9,13 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + webhook_model "code.gitea.io/gitea/models/webhook" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" ) func TestWebhook_GetSlackHook(t *testing.T) { - w := &models.Webhook{ + w := &webhook_model.Webhook{ Meta: `{"channel": "foo", "username": "username", "color": "blue"}`, } slackHook := GetSlackHook(w) @@ -29,13 +30,13 @@ func TestPrepareWebhooks(t *testing.T) { assert.NoError(t, db.PrepareTestDatabase()) repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - hookTasks := []*models.HookTask{ - {RepoID: repo.ID, HookID: 1, EventType: models.HookEventPush}, + hookTasks := []*webhook_model.HookTask{ + {RepoID: repo.ID, HookID: 1, EventType: webhook_model.HookEventPush}, } for _, hookTask := range hookTasks { db.AssertNotExistsBean(t, hookTask) } - assert.NoError(t, PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{Commits: []*api.PayloadCommit{{}}})) + assert.NoError(t, PrepareWebhooks(repo, webhook_model.HookEventPush, &api.PushPayload{Commits: []*api.PayloadCommit{{}}})) for _, hookTask := range hookTasks { db.AssertExistsAndLoadBean(t, hookTask) } @@ -45,14 +46,14 @@ func TestPrepareWebhooksBranchFilterMatch(t *testing.T) { assert.NoError(t, db.PrepareTestDatabase()) repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) - hookTasks := []*models.HookTask{ - {RepoID: repo.ID, HookID: 4, EventType: models.HookEventPush}, + hookTasks := []*webhook_model.HookTask{ + {RepoID: repo.ID, HookID: 4, EventType: webhook_model.HookEventPush}, } for _, hookTask := range hookTasks { db.AssertNotExistsBean(t, hookTask) } // this test also ensures that * doesn't handle / in any special way (like shell would) - assert.NoError(t, PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{Ref: "refs/heads/feature/7791", Commits: []*api.PayloadCommit{{}}})) + assert.NoError(t, PrepareWebhooks(repo, webhook_model.HookEventPush, &api.PushPayload{Ref: "refs/heads/feature/7791", Commits: []*api.PayloadCommit{{}}})) for _, hookTask := range hookTasks { db.AssertExistsAndLoadBean(t, hookTask) } @@ -62,13 +63,13 @@ func TestPrepareWebhooksBranchFilterNoMatch(t *testing.T) { assert.NoError(t, db.PrepareTestDatabase()) repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) - hookTasks := []*models.HookTask{ - {RepoID: repo.ID, HookID: 4, EventType: models.HookEventPush}, + hookTasks := []*webhook_model.HookTask{ + {RepoID: repo.ID, HookID: 4, EventType: webhook_model.HookEventPush}, } for _, hookTask := range hookTasks { db.AssertNotExistsBean(t, hookTask) } - assert.NoError(t, PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{Ref: "refs/heads/fix_weird_bug"})) + assert.NoError(t, PrepareWebhooks(repo, webhook_model.HookEventPush, &api.PushPayload{Ref: "refs/heads/fix_weird_bug"})) for _, hookTask := range hookTasks { db.AssertNotExistsBean(t, hookTask) diff --git a/services/webhook/wechatwork.go b/services/webhook/wechatwork.go index 08c77ff2a5..d28e70aded 100644 --- a/services/webhook/wechatwork.go +++ b/services/webhook/wechatwork.go @@ -8,7 +8,7 @@ import ( "fmt" "strings" - "code.gitea.io/gitea/models" + webhook_model "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" api "code.gitea.io/gitea/modules/structs" @@ -144,7 +144,7 @@ func (f *WechatworkPayload) PullRequest(p *api.PullRequestPayload) (api.Payloade } // Review implements PayloadConvertor Review method -func (f *WechatworkPayload) Review(p *api.PullRequestPayload, event models.HookEventType) (api.Payloader, error) { +func (f *WechatworkPayload) Review(p *api.PullRequestPayload, event webhook_model.HookEventType) (api.Payloader, error) { var text, title string switch p.Action { case api.HookIssueSynchronized: @@ -183,6 +183,6 @@ func (f *WechatworkPayload) Release(p *api.ReleasePayload) (api.Payloader, error } // GetWechatworkPayload GetWechatworkPayload converts a ding talk webhook into a WechatworkPayload -func GetWechatworkPayload(p api.Payloader, event models.HookEventType, meta string) (api.Payloader, error) { +func GetWechatworkPayload(p api.Payloader, event webhook_model.HookEventType, meta string) (api.Payloader, error) { return convertPayloader(new(WechatworkPayload), p, event) } |