From c3f8e6ed604e3b1a88b8d8d03440e2f5b7d96288 Mon Sep 17 00:00:00 2001 From: oliverpool Date: Thu, 21 Mar 2024 13:54:27 +0100 Subject: [PATCH] [REFACTOR] webhook discord endpoint --- routers/web/repo/setting/webhook.go | 25 ------------------------- routers/web/web.go | 2 -- services/forms/repo_form.go | 14 -------------- services/webhook/discord.go | 21 ++++++++++++++++++++- 4 files changed, 20 insertions(+), 42 deletions(-) diff --git a/routers/web/repo/setting/webhook.go b/routers/web/repo/setting/webhook.go index ca686cac63..50ff6698f2 100644 --- a/routers/web/repo/setting/webhook.go +++ b/routers/web/repo/setting/webhook.go @@ -388,31 +388,6 @@ func gogsHookParams(ctx *context.Context) webhookParams { } } -// DiscordHooksNewPost response for creating Discord webhook -func DiscordHooksNewPost(ctx *context.Context) { - createWebhook(ctx, discordHookParams(ctx)) -} - -// DiscordHooksEditPost response for editing Discord webhook -func DiscordHooksEditPost(ctx *context.Context) { - editWebhook(ctx, discordHookParams(ctx)) -} - -func discordHookParams(ctx *context.Context) webhookParams { - form := web.GetForm(ctx).(*forms.NewDiscordHookForm) - - return webhookParams{ - Type: webhook_module.DISCORD, - URL: form.PayloadURL, - ContentType: webhook.ContentTypeJSON, - WebhookForm: form.WebhookForm, - Meta: &webhook_service.DiscordMeta{ - Username: form.Username, - IconURL: form.IconURL, - }, - } -} - // DingtalkHooksNewPost response for creating Dingtalk webhook func DingtalkHooksNewPost(ctx *context.Context) { createWebhook(ctx, dingtalkHookParams(ctx)) diff --git a/routers/web/web.go b/routers/web/web.go index 92f2680657..9d4e07df24 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("/discord/new", web.Bind(forms.NewDiscordHookForm{}), repo_setting.DiscordHooksNewPost) 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) @@ -415,7 +414,6 @@ func registerRoutes(m *web.Route) { addWebhookEditRoutes := func() { m.Post("/gogs/{id}", web.Bind(forms.NewGogshookForm{}), repo_setting.GogsHooksEditPost) - m.Post("/discord/{id}", web.Bind(forms.NewDiscordHookForm{}), repo_setting.DiscordHooksEditPost) 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) diff --git a/services/forms/repo_form.go b/services/forms/repo_form.go index d251faf225..0614ea4c28 100644 --- a/services/forms/repo_form.go +++ b/services/forms/repo_form.go @@ -292,20 +292,6 @@ func (f *NewGogshookForm) Validate(req *http.Request, errs binding.Errors) bindi return middleware.Validate(errs, ctx.Data, f, ctx.Locale) } -// NewDiscordHookForm form for creating discord hook -type NewDiscordHookForm struct { - PayloadURL string `binding:"Required;ValidUrl"` - Username string - IconURL string - WebhookForm -} - -// Validate validates the fields -func (f *NewDiscordHookForm) Validate(req *http.Request, errs binding.Errors) binding.Errors { - ctx := context.GetValidateContext(req) - return middleware.Validate(errs, ctx.Data, f, ctx.Locale) -} - // NewDingtalkHookForm form for creating dingtalk hook type NewDingtalkHookForm struct { PayloadURL string `binding:"Required;ValidUrl"` diff --git a/services/webhook/discord.go b/services/webhook/discord.go index 8ddbb13738..3a0a973860 100644 --- a/services/webhook/discord.go +++ b/services/webhook/discord.go @@ -20,6 +20,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" ) type discordHandler struct{} @@ -27,7 +28,25 @@ type discordHandler struct{} func (discordHandler) Type() webhook_module.HookType { return webhook_module.DISCORD } func (discordHandler) FormFields(bind func(any)) FormFields { - panic("TODO") + var form struct { + forms.WebhookForm + PayloadURL string `binding:"Required;ValidUrl"` + Username string + IconURL string + } + bind(&form) + + return FormFields{ + WebhookForm: form.WebhookForm, + URL: form.PayloadURL, + ContentType: webhook_model.ContentTypeJSON, + Secret: "", + HTTPMethod: http.MethodPost, + Metadata: &DiscordMeta{ + Username: form.Username, + IconURL: form.IconURL, + }, + } } type (