From 4ab341e9714b59fa009af44fb7de61642b6e6e86 Mon Sep 17 00:00:00 2001 From: oliverpool Date: Thu, 21 Mar 2024 14:09:49 +0100 Subject: [PATCH] [REFACTOR] webhook gogs endpoint --- routers/web/repo/setting/webhook.go | 28 ---------------------------- routers/web/web.go | 2 -- services/forms/repo_form.go | 14 -------------- services/webhook/gogs.go | 27 +++++++++++++++++++++++++++ 4 files changed, 27 insertions(+), 44 deletions(-) diff --git a/routers/web/repo/setting/webhook.go b/routers/web/repo/setting/webhook.go index ea36da649f..e89fcef39a 100644 --- a/routers/web/repo/setting/webhook.go +++ b/routers/web/repo/setting/webhook.go @@ -23,7 +23,6 @@ import ( "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" - "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/modules/web/middleware" webhook_module "code.gitea.io/gitea/modules/webhook" "code.gitea.io/gitea/services/context" @@ -361,33 +360,6 @@ func editWebhook(ctx *context.Context, params webhookParams) { ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID)) } -// GogsHooksNewPost response for creating Gogs webhook -func GogsHooksNewPost(ctx *context.Context) { - createWebhook(ctx, gogsHookParams(ctx)) -} - -// GogsHooksEditPost response for editing Gogs webhook -func GogsHooksEditPost(ctx *context.Context) { - editWebhook(ctx, gogsHookParams(ctx)) -} - -func gogsHookParams(ctx *context.Context) webhookParams { - form := web.GetForm(ctx).(*forms.NewGogshookForm) - - contentType := webhook.ContentTypeJSON - if webhook.HookContentType(form.ContentType) == webhook.ContentTypeForm { - contentType = webhook.ContentTypeForm - } - - return webhookParams{ - Type: webhook_module.GOGS, - URL: form.PayloadURL, - ContentType: contentType, - Secret: form.Secret, - WebhookForm: form.WebhookForm, - } -} - func checkWebhook(ctx *context.Context) (*ownerRepoCtx, *webhook.Webhook) { orCtx, err := getOwnerRepoCtx(ctx) if err != nil { diff --git a/routers/web/web.go b/routers/web/web.go index 4b44a8eac5..348b9546c7 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -402,12 +402,10 @@ 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("/{type}/new", repo_setting.WebhookCreate) } addWebhookEditRoutes := func() { - m.Post("/gogs/{id}", web.Bind(forms.NewGogshookForm{}), repo_setting.GogsHooksEditPost) m.Post("/{type}/{id:[0-9]+}", repo_setting.WebhookUpdate) } diff --git a/services/forms/repo_form.go b/services/forms/repo_form.go index 7613feabda..0f7665804d 100644 --- a/services/forms/repo_form.go +++ b/services/forms/repo_form.go @@ -278,20 +278,6 @@ func (f WebhookForm) ChooseEvents() bool { return f.Events == "choose_events" } -// NewGogshookForm form for creating gogs hook -type NewGogshookForm struct { - PayloadURL string `binding:"Required;ValidUrl"` - ContentType int `binding:"Required"` - Secret string - WebhookForm -} - -// Validate validates the fields -func (f *NewGogshookForm) Validate(req *http.Request, errs binding.Errors) binding.Errors { - ctx := context.GetValidateContext(req) - return middleware.Validate(errs, ctx.Data, f, ctx.Locale) -} - // .___ // | | ______ ________ __ ____ // | |/ ___// ___/ | \_/ __ \ diff --git a/services/webhook/gogs.go b/services/webhook/gogs.go index 2ecd9e6d09..e23673ed35 100644 --- a/services/webhook/gogs.go +++ b/services/webhook/gogs.go @@ -4,9 +4,36 @@ package webhook import ( + "net/http" + + webhook_model "code.gitea.io/gitea/models/webhook" webhook_module "code.gitea.io/gitea/modules/webhook" + "code.gitea.io/gitea/services/forms" ) type gogsHandler struct{ defaultHandler } func (gogsHandler) Type() webhook_module.HookType { return webhook_module.GOGS } + +func (gogsHandler) FormFields(bind func(any)) FormFields { + var form struct { + forms.WebhookForm + PayloadURL string `binding:"Required;ValidUrl"` + 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: http.MethodPost, + Metadata: nil, + } +}