summaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
authoroliverpool <git@olivier.pfad.fr>2024-03-21 14:15:56 +0100
committeroliverpool <git@olivier.pfad.fr>2024-03-27 22:26:54 +0100
commit1e050d01c0ca44b9c1a34d00a3cc28faef22aa7a (patch)
tree64518649fdfb367a4afa6e3b2530d04d5b80ddb4 /routers
parentMerge pull request '[REFACTOR] webhook settings: merge Create/Update endpoint... (diff)
downloadforgejo-1e050d01c0ca44b9c1a34d00a3cc28faef22aa7a.tar.xz
forgejo-1e050d01c0ca44b9c1a34d00a3cc28faef22aa7a.zip
[REFACTOR] merge once-called functions
Diffstat (limited to 'routers')
-rw-r--r--routers/web/repo/setting/webhook.go99
-rw-r--r--routers/web/web.go29
2 files changed, 44 insertions, 84 deletions
diff --git a/routers/web/repo/setting/webhook.go b/routers/web/repo/setting/webhook.go
index e89fcef39a..e6e869d8f6 100644
--- a/routers/web/repo/setting/webhook.go
+++ b/routers/web/repo/setting/webhook.go
@@ -191,18 +191,6 @@ func ParseHookEvent(form forms.WebhookForm) *webhook_module.HookEvent {
}
}
-type webhookParams struct {
- // Type should be imported from webhook package (webhook.XXX)
- Type string
-
- URL string
- ContentType webhook.HookContentType
- Secret string
- HTTPMethod string
- WebhookForm forms.WebhookForm
- Meta any
-}
-
func WebhookCreate(ctx *context.Context) {
typ := ctx.Params(":type")
handler := webhook_service.GetWebhookHandler(typ)
@@ -213,25 +201,14 @@ func WebhookCreate(ctx *context.Context) {
fields := handler.FormFields(func(form any) {
errs := binding.Bind(ctx.Req, form)
- middleware.Validate(errs, ctx.Data, form, ctx.Locale) // error will be checked later in ctx.HasError
+ middleware.Validate(errs, ctx.Data, form, ctx.Locale) // error checked below in ctx.HasError
})
- createWebhook(ctx, webhookParams{
- Type: typ,
- URL: fields.URL,
- ContentType: fields.ContentType,
- Secret: fields.Secret,
- HTTPMethod: fields.HTTPMethod,
- WebhookForm: fields.WebhookForm,
- Meta: fields.Metadata,
- })
-}
-func createWebhook(ctx *context.Context, params webhookParams) {
ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
ctx.Data["PageIsSettingsHooks"] = true
ctx.Data["PageIsSettingsHooksNew"] = true
ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook_module.HookEvent{}}
- ctx.Data["HookType"] = params.Type
+ ctx.Data["HookType"] = typ
orCtx, err := getOwnerRepoCtx(ctx)
if err != nil {
@@ -247,8 +224,8 @@ func createWebhook(ctx *context.Context, params webhookParams) {
}
var meta []byte
- if params.Meta != nil {
- meta, err = json.Marshal(params.Meta)
+ if fields.Metadata != nil {
+ meta, err = json.Marshal(fields.Metadata)
if err != nil {
ctx.ServerError("Marshal", err)
return
@@ -257,18 +234,18 @@ func createWebhook(ctx *context.Context, params webhookParams) {
w := &webhook.Webhook{
RepoID: orCtx.RepoID,
- URL: params.URL,
- HTTPMethod: params.HTTPMethod,
- ContentType: params.ContentType,
- Secret: params.Secret,
- HookEvent: ParseHookEvent(params.WebhookForm),
- IsActive: params.WebhookForm.Active,
- Type: params.Type,
+ URL: fields.URL,
+ HTTPMethod: fields.HTTPMethod,
+ ContentType: fields.ContentType,
+ Secret: fields.Secret,
+ HookEvent: ParseHookEvent(fields.WebhookForm),
+ IsActive: fields.WebhookForm.Active,
+ Type: typ,
Meta: string(meta),
OwnerID: orCtx.OwnerID,
IsSystemWebhook: orCtx.IsSystemWebhook,
}
- err = w.SetHeaderAuthorization(params.WebhookForm.AuthorizationHeader)
+ err = w.SetHeaderAuthorization(fields.WebhookForm.AuthorizationHeader)
if err != nil {
ctx.ServerError("SetHeaderAuthorization", err)
return
@@ -286,29 +263,6 @@ func createWebhook(ctx *context.Context, params webhookParams) {
}
func WebhookUpdate(ctx *context.Context) {
- typ := ctx.Params(":type")
- handler := webhook_service.GetWebhookHandler(typ)
- if handler == nil {
- ctx.NotFound("GetWebhookHandler", nil)
- return
- }
-
- fields := handler.FormFields(func(form any) {
- errs := binding.Bind(ctx.Req, form)
- middleware.Validate(errs, ctx.Data, form, ctx.Locale) // error will be checked later in ctx.HasError
- })
- editWebhook(ctx, webhookParams{
- Type: typ,
- URL: fields.URL,
- ContentType: fields.ContentType,
- Secret: fields.Secret,
- HTTPMethod: fields.HTTPMethod,
- WebhookForm: fields.WebhookForm,
- Meta: fields.Metadata,
- })
-}
-
-func editWebhook(ctx *context.Context, params webhookParams) {
ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook")
ctx.Data["PageIsSettingsHooks"] = true
ctx.Data["PageIsSettingsHooksEdit"] = true
@@ -319,6 +273,17 @@ func editWebhook(ctx *context.Context, params webhookParams) {
}
ctx.Data["Webhook"] = w
+ handler := webhook_service.GetWebhookHandler(w.Type)
+ if handler == nil {
+ ctx.NotFound("GetWebhookHandler", nil)
+ return
+ }
+
+ fields := handler.FormFields(func(form any) {
+ errs := binding.Bind(ctx.Req, form)
+ middleware.Validate(errs, ctx.Data, form, ctx.Locale) // error checked below in ctx.HasError
+ })
+
if ctx.HasError() {
ctx.HTML(http.StatusUnprocessableEntity, orCtx.NewTemplate)
return
@@ -326,23 +291,23 @@ func editWebhook(ctx *context.Context, params webhookParams) {
var meta []byte
var err error
- if params.Meta != nil {
- meta, err = json.Marshal(params.Meta)
+ if fields.Metadata != nil {
+ meta, err = json.Marshal(fields.Metadata)
if err != nil {
ctx.ServerError("Marshal", err)
return
}
}
- w.URL = params.URL
- w.ContentType = params.ContentType
- w.Secret = params.Secret
- w.HookEvent = ParseHookEvent(params.WebhookForm)
- w.IsActive = params.WebhookForm.Active
- w.HTTPMethod = params.HTTPMethod
+ w.URL = fields.URL
+ w.ContentType = fields.ContentType
+ w.Secret = fields.Secret
+ w.HookEvent = ParseHookEvent(fields.WebhookForm)
+ w.IsActive = fields.WebhookForm.Active
+ w.HTTPMethod = fields.HTTPMethod
w.Meta = string(meta)
- err = w.SetHeaderAuthorization(params.WebhookForm.AuthorizationHeader)
+ err = w.SetHeaderAuthorization(fields.WebhookForm.AuthorizationHeader)
if err != nil {
ctx.ServerError("SetHeaderAuthorization", err)
return
diff --git a/routers/web/web.go b/routers/web/web.go
index 348b9546c7..b2677f1a65 100644
--- a/routers/web/web.go
+++ b/routers/web/web.go
@@ -400,15 +400,6 @@ func registerRoutes(m *web.Route) {
}
}
- addWebhookAddRoutes := func() {
- m.Get("/{type}/new", repo_setting.WebhooksNew)
- m.Post("/{type}/new", repo_setting.WebhookCreate)
- }
-
- addWebhookEditRoutes := func() {
- m.Post("/{type}/{id:[0-9]+}", repo_setting.WebhookUpdate)
- }
-
addSettingsVariablesRoutes := func() {
m.Group("/variables", func() {
m.Get("", repo_setting.Variables)
@@ -618,12 +609,13 @@ func registerRoutes(m *web.Route) {
m.Group("/hooks", func() {
m.Get("", user_setting.Webhooks)
m.Post("/delete", user_setting.DeleteWebhook)
- addWebhookAddRoutes()
+ m.Get("/{type}/new", repo_setting.WebhooksNew)
+ m.Post("/{type}/new", repo_setting.WebhookCreate)
m.Group("/{id}", func() {
m.Get("", repo_setting.WebHooksEdit)
m.Post("/replay/{uuid}", repo_setting.ReplayWebhook)
})
- addWebhookEditRoutes()
+ m.Post("/{type}/{id:[0-9]+}", repo_setting.WebhookUpdate)
}, webhooksEnabled)
m.Group("/blocked_users", func() {
@@ -725,11 +717,12 @@ func registerRoutes(m *web.Route) {
m.Get("", repo_setting.WebHooksEdit)
m.Post("/replay/{uuid}", repo_setting.ReplayWebhook)
})
- addWebhookEditRoutes()
+ m.Post("/{type}/{id:[0-9]+}", repo_setting.WebhookUpdate)
}, webhooksEnabled)
m.Group("/{configType:default-hooks|system-hooks}", func() {
- addWebhookAddRoutes()
+ m.Get("/{type}/new", repo_setting.WebhooksNew)
+ m.Post("/{type}/new", repo_setting.WebhookCreate)
})
m.Group("/auths", func() {
@@ -887,12 +880,13 @@ func registerRoutes(m *web.Route) {
m.Group("/hooks", func() {
m.Get("", org.Webhooks)
m.Post("/delete", org.DeleteWebhook)
- addWebhookAddRoutes()
+ m.Get("/{type}/new", repo_setting.WebhooksNew)
+ m.Post("/{type}/new", repo_setting.WebhookCreate)
m.Group("/{id}", func() {
m.Get("", repo_setting.WebHooksEdit)
m.Post("/replay/{uuid}", repo_setting.ReplayWebhook)
})
- addWebhookEditRoutes()
+ m.Post("/{type}/{id:[0-9]+}", repo_setting.WebhookUpdate)
}, webhooksEnabled)
m.Group("/labels", func() {
@@ -1061,13 +1055,14 @@ func registerRoutes(m *web.Route) {
m.Group("/hooks", func() {
m.Get("", repo_setting.Webhooks)
m.Post("/delete", repo_setting.DeleteWebhook)
- addWebhookAddRoutes()
+ m.Get("/{type}/new", repo_setting.WebhooksNew)
+ m.Post("/{type}/new", repo_setting.WebhookCreate)
m.Group("/{id}", func() {
m.Get("", repo_setting.WebHooksEdit)
m.Post("/test", repo_setting.TestWebhook)
m.Post("/replay/{uuid}", repo_setting.ReplayWebhook)
})
- addWebhookEditRoutes()
+ m.Post("/{type}/{id:[0-9]+}", repo_setting.WebhookUpdate)
}, webhooksEnabled)
m.Group("/keys", func() {