From 9dff719523d0db7599262760a099a69194e187a4 Mon Sep 17 00:00:00 2001 From: oliverpool Date: Thu, 21 Mar 2024 13:57:14 +0100 Subject: [PATCH] [REFACTOR] webhook dingtalk endpoint --- routers/web/repo/setting/webhook.go | 21 --------------------- routers/web/web.go | 2 -- services/forms/repo_form.go | 12 ------------ services/webhook/dingtalk.go | 16 +++++++++++++++- 4 files changed, 15 insertions(+), 36 deletions(-) diff --git a/routers/web/repo/setting/webhook.go b/routers/web/repo/setting/webhook.go index 50ff6698f2..29b28cfd30 100644 --- a/routers/web/repo/setting/webhook.go +++ b/routers/web/repo/setting/webhook.go @@ -388,27 +388,6 @@ func gogsHookParams(ctx *context.Context) webhookParams { } } -// DingtalkHooksNewPost response for creating Dingtalk webhook -func DingtalkHooksNewPost(ctx *context.Context) { - createWebhook(ctx, dingtalkHookParams(ctx)) -} - -// DingtalkHooksEditPost response for editing Dingtalk webhook -func DingtalkHooksEditPost(ctx *context.Context) { - editWebhook(ctx, dingtalkHookParams(ctx)) -} - -func dingtalkHookParams(ctx *context.Context) webhookParams { - form := web.GetForm(ctx).(*forms.NewDingtalkHookForm) - - return webhookParams{ - Type: webhook_module.DINGTALK, - URL: form.PayloadURL, - ContentType: webhook.ContentTypeJSON, - WebhookForm: form.WebhookForm, - } -} - // TelegramHooksNewPost response for creating Telegram webhook func TelegramHooksNewPost(ctx *context.Context) { createWebhook(ctx, telegramHookParams(ctx)) diff --git a/routers/web/web.go b/routers/web/web.go index 9d4e07df24..116ffccad5 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -403,7 +403,6 @@ func registerRoutes(m *web.Route) { addWebhookAddRoutes := func() { m.Get("/{type}/new", repo_setting.WebhooksNew) m.Post("/gogs/new", web.Bind(forms.NewGogshookForm{}), repo_setting.GogsHooksNewPost) - m.Post("/dingtalk/new", web.Bind(forms.NewDingtalkHookForm{}), repo_setting.DingtalkHooksNewPost) m.Post("/telegram/new", web.Bind(forms.NewTelegramHookForm{}), repo_setting.TelegramHooksNewPost) m.Post("/msteams/new", web.Bind(forms.NewMSTeamsHookForm{}), repo_setting.MSTeamsHooksNewPost) m.Post("/feishu/new", web.Bind(forms.NewFeishuHookForm{}), repo_setting.FeishuHooksNewPost) @@ -414,7 +413,6 @@ func registerRoutes(m *web.Route) { addWebhookEditRoutes := func() { m.Post("/gogs/{id}", web.Bind(forms.NewGogshookForm{}), repo_setting.GogsHooksEditPost) - m.Post("/dingtalk/{id}", web.Bind(forms.NewDingtalkHookForm{}), repo_setting.DingtalkHooksEditPost) m.Post("/telegram/{id}", web.Bind(forms.NewTelegramHookForm{}), repo_setting.TelegramHooksEditPost) m.Post("/msteams/{id}", web.Bind(forms.NewMSTeamsHookForm{}), repo_setting.MSTeamsHooksEditPost) m.Post("/feishu/{id}", web.Bind(forms.NewFeishuHookForm{}), repo_setting.FeishuHooksEditPost) diff --git a/services/forms/repo_form.go b/services/forms/repo_form.go index 0614ea4c28..891d52493c 100644 --- a/services/forms/repo_form.go +++ b/services/forms/repo_form.go @@ -292,18 +292,6 @@ func (f *NewGogshookForm) Validate(req *http.Request, errs binding.Errors) bindi return middleware.Validate(errs, ctx.Data, f, ctx.Locale) } -// NewDingtalkHookForm form for creating dingtalk hook -type NewDingtalkHookForm struct { - PayloadURL string `binding:"Required;ValidUrl"` - WebhookForm -} - -// Validate validates the fields -func (f *NewDingtalkHookForm) Validate(req *http.Request, errs binding.Errors) binding.Errors { - ctx := context.GetValidateContext(req) - return middleware.Validate(errs, ctx.Data, f, ctx.Locale) -} - // NewTelegramHookForm form for creating telegram hook type NewTelegramHookForm struct { BotToken string `binding:"Required"` diff --git a/services/webhook/dingtalk.go b/services/webhook/dingtalk.go index 12c04d2251..562e3e86eb 100644 --- a/services/webhook/dingtalk.go +++ b/services/webhook/dingtalk.go @@ -15,6 +15,7 @@ import ( api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" webhook_module "code.gitea.io/gitea/modules/webhook" + "code.gitea.io/gitea/services/forms" dingtalk "gitea.com/lunny/dingtalk_webhook" ) @@ -24,7 +25,20 @@ type dingtalkHandler struct{} func (dingtalkHandler) Type() webhook_module.HookType { return webhook_module.DINGTALK } func (dingtalkHandler) Metadata(*webhook_model.Webhook) any { return nil } func (dingtalkHandler) FormFields(bind func(any)) FormFields { - panic("TODO") + var form struct { + forms.WebhookForm + PayloadURL string `binding:"Required;ValidUrl"` + } + bind(&form) + + return FormFields{ + WebhookForm: form.WebhookForm, + URL: form.PayloadURL, + ContentType: webhook_model.ContentTypeJSON, + Secret: "", + HTTPMethod: http.MethodPost, + Metadata: nil, + } } type (