From 46b71ec709de88e7dc2addcb8ca33b97535549f6 Mon Sep 17 00:00:00 2001 From: oliverpool Date: Thu, 21 Mar 2024 13:59:24 +0100 Subject: [PATCH] [REFACTOR] webhook telegram endpoint --- routers/web/repo/setting/webhook.go | 26 -------------------------- routers/web/web.go | 2 -- services/forms/repo_form.go | 14 -------------- services/webhook/telegram.go | 23 ++++++++++++++++++++++- 4 files changed, 22 insertions(+), 43 deletions(-) diff --git a/routers/web/repo/setting/webhook.go b/routers/web/repo/setting/webhook.go index 29b28cfd30..dc82475aae 100644 --- a/routers/web/repo/setting/webhook.go +++ b/routers/web/repo/setting/webhook.go @@ -388,32 +388,6 @@ func gogsHookParams(ctx *context.Context) webhookParams { } } -// TelegramHooksNewPost response for creating Telegram webhook -func TelegramHooksNewPost(ctx *context.Context) { - createWebhook(ctx, telegramHookParams(ctx)) -} - -// TelegramHooksEditPost response for editing Telegram webhook -func TelegramHooksEditPost(ctx *context.Context) { - editWebhook(ctx, telegramHookParams(ctx)) -} - -func telegramHookParams(ctx *context.Context) webhookParams { - form := web.GetForm(ctx).(*forms.NewTelegramHookForm) - - return webhookParams{ - Type: webhook_module.TELEGRAM, - URL: fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%s&message_thread_id=%s", url.PathEscape(form.BotToken), url.QueryEscape(form.ChatID), url.QueryEscape(form.ThreadID)), - ContentType: webhook.ContentTypeJSON, - WebhookForm: form.WebhookForm, - Meta: &webhook_service.TelegramMeta{ - BotToken: form.BotToken, - ChatID: form.ChatID, - ThreadID: form.ThreadID, - }, - } -} - // MSTeamsHooksNewPost response for creating MSTeams webhook func MSTeamsHooksNewPost(ctx *context.Context) { createWebhook(ctx, mSTeamsHookParams(ctx)) diff --git a/routers/web/web.go b/routers/web/web.go index 116ffccad5..7e41d7ed49 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("/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) m.Post("/wechatwork/new", web.Bind(forms.NewWechatWorkHookForm{}), repo_setting.WechatworkHooksNewPost) @@ -413,7 +412,6 @@ func registerRoutes(m *web.Route) { addWebhookEditRoutes := func() { m.Post("/gogs/{id}", web.Bind(forms.NewGogshookForm{}), repo_setting.GogsHooksEditPost) - 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) m.Post("/wechatwork/{id}", web.Bind(forms.NewWechatWorkHookForm{}), repo_setting.WechatworkHooksEditPost) diff --git a/services/forms/repo_form.go b/services/forms/repo_form.go index 891d52493c..3c800d86ee 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) } -// NewTelegramHookForm form for creating telegram hook -type NewTelegramHookForm struct { - BotToken string `binding:"Required"` - ChatID string `binding:"Required"` - ThreadID string - WebhookForm -} - -// Validate validates the fields -func (f *NewTelegramHookForm) Validate(req *http.Request, errs binding.Errors) binding.Errors { - ctx := context.GetValidateContext(req) - return middleware.Validate(errs, ctx.Data, f, ctx.Locale) -} - // NewMSTeamsHookForm form for creating MS Teams hook type NewMSTeamsHookForm struct { PayloadURL string `binding:"Required;ValidUrl"` diff --git a/services/webhook/telegram.go b/services/webhook/telegram.go index 4179437a80..2ede28dbd2 100644 --- a/services/webhook/telegram.go +++ b/services/webhook/telegram.go @@ -7,6 +7,7 @@ import ( "context" "fmt" "net/http" + "net/url" "strings" webhook_model "code.gitea.io/gitea/models/webhook" @@ -15,6 +16,7 @@ import ( "code.gitea.io/gitea/modules/log" api "code.gitea.io/gitea/modules/structs" webhook_module "code.gitea.io/gitea/modules/webhook" + "code.gitea.io/gitea/services/forms" ) type telegramHandler struct{} @@ -22,7 +24,26 @@ type telegramHandler struct{} func (telegramHandler) Type() webhook_module.HookType { return webhook_module.TELEGRAM } func (telegramHandler) FormFields(bind func(any)) FormFields { - panic("TODO") + var form struct { + forms.WebhookForm + BotToken string `binding:"Required"` + ChatID string `binding:"Required"` + ThreadID string + } + bind(&form) + + return FormFields{ + WebhookForm: form.WebhookForm, + URL: fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%s&message_thread_id=%s", url.PathEscape(form.BotToken), url.QueryEscape(form.ChatID), url.QueryEscape(form.ThreadID)), + ContentType: webhook_model.ContentTypeJSON, + Secret: "", + HTTPMethod: http.MethodPost, + Metadata: &TelegramMeta{ + BotToken: form.BotToken, + ChatID: form.ChatID, + ThreadID: form.ThreadID, + }, + } } type (