From dce754cde180903d349fbca611ec1b8ab90ea5e2 Mon Sep 17 00:00:00 2001 From: oliverpool Date: Thu, 21 Mar 2024 14:04:37 +0100 Subject: [PATCH] [REFACTOR] webhook packagist endpoint --- routers/web/repo/setting/webhook.go | 26 -------------------------- routers/web/web.go | 2 -- services/forms/repo_form.go | 14 -------------- services/webhook/packagist.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 a1021c1967..ea36da649f 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 { } } -// PackagistHooksNewPost response for creating Packagist webhook -func PackagistHooksNewPost(ctx *context.Context) { - createWebhook(ctx, packagistHookParams(ctx)) -} - -// PackagistHooksEditPost response for editing Packagist webhook -func PackagistHooksEditPost(ctx *context.Context) { - editWebhook(ctx, packagistHookParams(ctx)) -} - -func packagistHookParams(ctx *context.Context) webhookParams { - form := web.GetForm(ctx).(*forms.NewPackagistHookForm) - - return webhookParams{ - Type: webhook_module.PACKAGIST, - URL: fmt.Sprintf("https://packagist.org/api/update-package?username=%s&apiToken=%s", url.QueryEscape(form.Username), url.QueryEscape(form.APIToken)), - ContentType: webhook.ContentTypeJSON, - WebhookForm: form.WebhookForm, - Meta: &webhook_service.PackagistMeta{ - Username: form.Username, - APIToken: form.APIToken, - PackageURL: form.PackageURL, - }, - } -} - 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 81ff59499d..4b44a8eac5 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -403,13 +403,11 @@ 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("/packagist/new", web.Bind(forms.NewPackagistHookForm{}), repo_setting.PackagistHooksNewPost) m.Post("/{type}/new", repo_setting.WebhookCreate) } addWebhookEditRoutes := func() { m.Post("/gogs/{id}", web.Bind(forms.NewGogshookForm{}), repo_setting.GogsHooksEditPost) - m.Post("/packagist/{id}", web.Bind(forms.NewPackagistHookForm{}), repo_setting.PackagistHooksEditPost) m.Post("/{type}/{id:[0-9]+}", repo_setting.WebhookUpdate) } diff --git a/services/forms/repo_form.go b/services/forms/repo_form.go index e1d29943a0..7613feabda 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) } -// NewPackagistHookForm form for creating packagist hook -type NewPackagistHookForm struct { - Username string `binding:"Required"` - APIToken string `binding:"Required"` - PackageURL string `binding:"Required;ValidUrl"` - WebhookForm -} - -// Validate validates the fields -func (f *NewPackagistHookForm) 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/packagist.go b/services/webhook/packagist.go index 9b07fe025b..d8bf9ea23a 100644 --- a/services/webhook/packagist.go +++ b/services/webhook/packagist.go @@ -7,11 +7,13 @@ import ( "context" "fmt" "net/http" + "net/url" webhook_model "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" webhook_module "code.gitea.io/gitea/modules/webhook" + "code.gitea.io/gitea/services/forms" ) type packagistHandler struct{} @@ -19,7 +21,26 @@ type packagistHandler struct{} func (packagistHandler) Type() webhook_module.HookType { return webhook_module.PACKAGIST } func (packagistHandler) FormFields(bind func(any)) FormFields { - panic("TODO") + var form struct { + forms.WebhookForm + Username string `binding:"Required"` + APIToken string `binding:"Required"` + PackageURL string `binding:"Required;ValidUrl"` + } + bind(&form) + + return FormFields{ + WebhookForm: form.WebhookForm, + URL: fmt.Sprintf("https://packagist.org/api/update-package?username=%s&apiToken=%s", url.QueryEscape(form.Username), url.QueryEscape(form.APIToken)), + ContentType: webhook_model.ContentTypeJSON, + Secret: "", + HTTPMethod: http.MethodPost, + Metadata: &PackagistMeta{ + Username: form.Username, + APIToken: form.APIToken, + PackageURL: form.PackageURL, + }, + } } type (