From 97f0ad49ff6245c026265d50da788b8437c7b52f Mon Sep 17 00:00:00 2001 From: oliverpool Date: Thu, 21 Mar 2024 14:01:02 +0100 Subject: [PATCH] [REFACTOR] webhook msteams endpoint --- routers/web/repo/setting/webhook.go | 21 --------------------- routers/web/web.go | 2 -- services/forms/repo_form.go | 12 ------------ services/webhook/msteams.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 dc82475aae..ead24614f1 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 { } } -// MSTeamsHooksNewPost response for creating MSTeams webhook -func MSTeamsHooksNewPost(ctx *context.Context) { - createWebhook(ctx, mSTeamsHookParams(ctx)) -} - -// MSTeamsHooksEditPost response for editing MSTeams webhook -func MSTeamsHooksEditPost(ctx *context.Context) { - editWebhook(ctx, mSTeamsHookParams(ctx)) -} - -func mSTeamsHookParams(ctx *context.Context) webhookParams { - form := web.GetForm(ctx).(*forms.NewMSTeamsHookForm) - - return webhookParams{ - Type: webhook_module.MSTEAMS, - URL: form.PayloadURL, - ContentType: webhook.ContentTypeJSON, - WebhookForm: form.WebhookForm, - } -} - // FeishuHooksNewPost response for creating Feishu webhook func FeishuHooksNewPost(ctx *context.Context) { createWebhook(ctx, feishuHookParams(ctx)) diff --git a/routers/web/web.go b/routers/web/web.go index 7e41d7ed49..6d2e380e06 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("/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) m.Post("/packagist/new", web.Bind(forms.NewPackagistHookForm{}), repo_setting.PackagistHooksNewPost) @@ -412,7 +411,6 @@ func registerRoutes(m *web.Route) { addWebhookEditRoutes := func() { m.Post("/gogs/{id}", web.Bind(forms.NewGogshookForm{}), repo_setting.GogsHooksEditPost) - 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) m.Post("/packagist/{id}", web.Bind(forms.NewPackagistHookForm{}), repo_setting.PackagistHooksEditPost) diff --git a/services/forms/repo_form.go b/services/forms/repo_form.go index 3c800d86ee..2e30106ea2 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) } -// NewMSTeamsHookForm form for creating MS Teams hook -type NewMSTeamsHookForm struct { - PayloadURL string `binding:"Required;ValidUrl"` - WebhookForm -} - -// Validate validates the fields -func (f *NewMSTeamsHookForm) Validate(req *http.Request, errs binding.Errors) binding.Errors { - ctx := context.GetValidateContext(req) - return middleware.Validate(errs, ctx.Data, f, ctx.Locale) -} - // NewFeishuHookForm form for creating feishu hook type NewFeishuHookForm struct { PayloadURL string `binding:"Required;ValidUrl"` diff --git a/services/webhook/msteams.go b/services/webhook/msteams.go index 5799884898..849093e9bf 100644 --- a/services/webhook/msteams.go +++ b/services/webhook/msteams.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" ) type msteamsHandler struct{} @@ -23,7 +24,20 @@ func (msteamsHandler) Type() webhook_module.HookType { return webhook_modu func (msteamsHandler) Metadata(*webhook_model.Webhook) any { return nil } func (msteamsHandler) 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 (