From 7e0965b02c8cf9ecb14cdaeaeec7b1612010a603 Mon Sep 17 00:00:00 2001 From: oliverpool Date: Thu, 21 Mar 2024 13:42:40 +0100 Subject: [PATCH] [REFACTOR] webhook forgejo/gitea endpoints --- routers/web/repo/setting/webhook.go | 56 ----------------------------- routers/web/web.go | 4 --- services/forms/repo_form.go | 15 -------- services/webhook/default.go | 23 +++++++++++- 4 files changed, 22 insertions(+), 76 deletions(-) diff --git a/routers/web/repo/setting/webhook.go b/routers/web/repo/setting/webhook.go index f41752e47..890094219 100644 --- a/routers/web/repo/setting/webhook.go +++ b/routers/web/repo/setting/webhook.go @@ -361,62 +361,6 @@ func editWebhook(ctx *context.Context, params webhookParams) { ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID)) } -// ForgejoHooksNewPost response for creating Forgejo webhook -func ForgejoHooksNewPost(ctx *context.Context) { - createWebhook(ctx, forgejoHookParams(ctx)) -} - -// ForgejoHooksEditPost response for editing Forgejo webhook -func ForgejoHooksEditPost(ctx *context.Context) { - editWebhook(ctx, forgejoHookParams(ctx)) -} - -func forgejoHookParams(ctx *context.Context) webhookParams { - form := web.GetForm(ctx).(*forms.NewWebhookForm) - - contentType := webhook.ContentTypeJSON - if webhook.HookContentType(form.ContentType) == webhook.ContentTypeForm { - contentType = webhook.ContentTypeForm - } - - return webhookParams{ - Type: webhook_module.FORGEJO, - URL: form.PayloadURL, - ContentType: contentType, - Secret: form.Secret, - HTTPMethod: form.HTTPMethod, - WebhookForm: form.WebhookForm, - } -} - -// GiteaHooksNewPost response for creating Gitea webhook -func GiteaHooksNewPost(ctx *context.Context) { - createWebhook(ctx, giteaHookParams(ctx)) -} - -// GiteaHooksEditPost response for editing Gitea webhook -func GiteaHooksEditPost(ctx *context.Context) { - editWebhook(ctx, giteaHookParams(ctx)) -} - -func giteaHookParams(ctx *context.Context) webhookParams { - form := web.GetForm(ctx).(*forms.NewWebhookForm) - - contentType := webhook.ContentTypeJSON - if webhook.HookContentType(form.ContentType) == webhook.ContentTypeForm { - contentType = webhook.ContentTypeForm - } - - return webhookParams{ - Type: webhook_module.GITEA, - URL: form.PayloadURL, - ContentType: contentType, - Secret: form.Secret, - HTTPMethod: form.HTTPMethod, - WebhookForm: form.WebhookForm, - } -} - // GogsHooksNewPost response for creating Gogs webhook func GogsHooksNewPost(ctx *context.Context) { createWebhook(ctx, gogsHookParams(ctx)) diff --git a/routers/web/web.go b/routers/web/web.go index b23068a29..06ef48542 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -402,8 +402,6 @@ func registerRoutes(m *web.Route) { addWebhookAddRoutes := func() { m.Get("/{type}/new", repo_setting.WebhooksNew) - m.Post("/forgejo/new", web.Bind(forms.NewWebhookForm{}), repo_setting.ForgejoHooksNewPost) - m.Post("/gitea/new", web.Bind(forms.NewWebhookForm{}), repo_setting.GiteaHooksNewPost) m.Post("/gogs/new", web.Bind(forms.NewGogshookForm{}), repo_setting.GogsHooksNewPost) m.Post("/slack/new", web.Bind(forms.NewSlackHookForm{}), repo_setting.SlackHooksNewPost) m.Post("/discord/new", web.Bind(forms.NewDiscordHookForm{}), repo_setting.DiscordHooksNewPost) @@ -417,8 +415,6 @@ func registerRoutes(m *web.Route) { } addWebhookEditRoutes := func() { - m.Post("/forgejo/{id}", web.Bind(forms.NewWebhookForm{}), repo_setting.ForgejoHooksEditPost) - m.Post("/gitea/{id}", web.Bind(forms.NewWebhookForm{}), repo_setting.GiteaHooksEditPost) m.Post("/gogs/{id}", web.Bind(forms.NewGogshookForm{}), repo_setting.GogsHooksEditPost) m.Post("/slack/{id}", web.Bind(forms.NewSlackHookForm{}), repo_setting.SlackHooksEditPost) m.Post("/discord/{id}", web.Bind(forms.NewDiscordHookForm{}), repo_setting.DiscordHooksEditPost) diff --git a/services/forms/repo_form.go b/services/forms/repo_form.go index 35d465bdd..5a372ab67 100644 --- a/services/forms/repo_form.go +++ b/services/forms/repo_form.go @@ -279,21 +279,6 @@ func (f WebhookForm) ChooseEvents() bool { return f.Events == "choose_events" } -// NewWebhookForm form for creating web hook -type NewWebhookForm struct { - PayloadURL string `binding:"Required;ValidUrl"` - HTTPMethod string `binding:"Required;In(POST,GET)"` - ContentType int `binding:"Required"` - Secret string - WebhookForm -} - -// Validate validates the fields -func (f *NewWebhookForm) Validate(req *http.Request, errs binding.Errors) binding.Errors { - ctx := context.GetValidateContext(req) - return middleware.Validate(errs, ctx.Data, f, ctx.Locale) -} - // NewGogshookForm form for creating gogs hook type NewGogshookForm struct { PayloadURL string `binding:"Required;ValidUrl"` diff --git a/services/webhook/default.go b/services/webhook/default.go index d61e316a6..f725f8a78 100644 --- a/services/webhook/default.go +++ b/services/webhook/default.go @@ -18,6 +18,7 @@ import ( webhook_model "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/log" webhook_module "code.gitea.io/gitea/modules/webhook" + "code.gitea.io/gitea/services/forms" ) var _ Handler = defaultHandler{} @@ -36,7 +37,27 @@ func (dh defaultHandler) Type() webhook_module.HookType { func (defaultHandler) Metadata(*webhook_model.Webhook) any { return nil } func (defaultHandler) FormFields(bind func(any)) FormFields { - panic("TODO") + var form struct { + forms.WebhookForm + PayloadURL string `binding:"Required;ValidUrl"` + HTTPMethod string `binding:"Required;In(POST,GET)"` + ContentType int `binding:"Required"` + Secret string + } + bind(&form) + + contentType := webhook_model.ContentTypeJSON + if webhook_model.HookContentType(form.ContentType) == webhook_model.ContentTypeForm { + contentType = webhook_model.ContentTypeForm + } + return FormFields{ + WebhookForm: form.WebhookForm, + URL: form.PayloadURL, + ContentType: contentType, + Secret: form.Secret, + HTTPMethod: form.HTTPMethod, + Metadata: nil, + } } func (defaultHandler) NewRequest(ctx context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (req *http.Request, body []byte, err error) {