[REFACTOR] merge once-called functions

This commit is contained in:
oliverpool 2024-03-21 14:15:56 +01:00
parent 883cb2d542
commit 1e050d01c0
2 changed files with 44 additions and 84 deletions

View file

@ -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) { func WebhookCreate(ctx *context.Context) {
typ := ctx.Params(":type") typ := ctx.Params(":type")
handler := webhook_service.GetWebhookHandler(typ) handler := webhook_service.GetWebhookHandler(typ)
@ -213,25 +201,14 @@ func WebhookCreate(ctx *context.Context) {
fields := handler.FormFields(func(form any) { fields := handler.FormFields(func(form any) {
errs := binding.Bind(ctx.Req, form) 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["Title"] = ctx.Tr("repo.settings.add_webhook")
ctx.Data["PageIsSettingsHooks"] = true ctx.Data["PageIsSettingsHooks"] = true
ctx.Data["PageIsSettingsHooksNew"] = true ctx.Data["PageIsSettingsHooksNew"] = true
ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook_module.HookEvent{}} ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook_module.HookEvent{}}
ctx.Data["HookType"] = params.Type ctx.Data["HookType"] = typ
orCtx, err := getOwnerRepoCtx(ctx) orCtx, err := getOwnerRepoCtx(ctx)
if err != nil { if err != nil {
@ -247,8 +224,8 @@ func createWebhook(ctx *context.Context, params webhookParams) {
} }
var meta []byte var meta []byte
if params.Meta != nil { if fields.Metadata != nil {
meta, err = json.Marshal(params.Meta) meta, err = json.Marshal(fields.Metadata)
if err != nil { if err != nil {
ctx.ServerError("Marshal", err) ctx.ServerError("Marshal", err)
return return
@ -257,18 +234,18 @@ func createWebhook(ctx *context.Context, params webhookParams) {
w := &webhook.Webhook{ w := &webhook.Webhook{
RepoID: orCtx.RepoID, RepoID: orCtx.RepoID,
URL: params.URL, URL: fields.URL,
HTTPMethod: params.HTTPMethod, HTTPMethod: fields.HTTPMethod,
ContentType: params.ContentType, ContentType: fields.ContentType,
Secret: params.Secret, Secret: fields.Secret,
HookEvent: ParseHookEvent(params.WebhookForm), HookEvent: ParseHookEvent(fields.WebhookForm),
IsActive: params.WebhookForm.Active, IsActive: fields.WebhookForm.Active,
Type: params.Type, Type: typ,
Meta: string(meta), Meta: string(meta),
OwnerID: orCtx.OwnerID, OwnerID: orCtx.OwnerID,
IsSystemWebhook: orCtx.IsSystemWebhook, IsSystemWebhook: orCtx.IsSystemWebhook,
} }
err = w.SetHeaderAuthorization(params.WebhookForm.AuthorizationHeader) err = w.SetHeaderAuthorization(fields.WebhookForm.AuthorizationHeader)
if err != nil { if err != nil {
ctx.ServerError("SetHeaderAuthorization", err) ctx.ServerError("SetHeaderAuthorization", err)
return return
@ -286,29 +263,6 @@ func createWebhook(ctx *context.Context, params webhookParams) {
} }
func WebhookUpdate(ctx *context.Context) { 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["Title"] = ctx.Tr("repo.settings.update_webhook")
ctx.Data["PageIsSettingsHooks"] = true ctx.Data["PageIsSettingsHooks"] = true
ctx.Data["PageIsSettingsHooksEdit"] = true ctx.Data["PageIsSettingsHooksEdit"] = true
@ -319,6 +273,17 @@ func editWebhook(ctx *context.Context, params webhookParams) {
} }
ctx.Data["Webhook"] = w 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() { if ctx.HasError() {
ctx.HTML(http.StatusUnprocessableEntity, orCtx.NewTemplate) ctx.HTML(http.StatusUnprocessableEntity, orCtx.NewTemplate)
return return
@ -326,23 +291,23 @@ func editWebhook(ctx *context.Context, params webhookParams) {
var meta []byte var meta []byte
var err error var err error
if params.Meta != nil { if fields.Metadata != nil {
meta, err = json.Marshal(params.Meta) meta, err = json.Marshal(fields.Metadata)
if err != nil { if err != nil {
ctx.ServerError("Marshal", err) ctx.ServerError("Marshal", err)
return return
} }
} }
w.URL = params.URL w.URL = fields.URL
w.ContentType = params.ContentType w.ContentType = fields.ContentType
w.Secret = params.Secret w.Secret = fields.Secret
w.HookEvent = ParseHookEvent(params.WebhookForm) w.HookEvent = ParseHookEvent(fields.WebhookForm)
w.IsActive = params.WebhookForm.Active w.IsActive = fields.WebhookForm.Active
w.HTTPMethod = params.HTTPMethod w.HTTPMethod = fields.HTTPMethod
w.Meta = string(meta) w.Meta = string(meta)
err = w.SetHeaderAuthorization(params.WebhookForm.AuthorizationHeader) err = w.SetHeaderAuthorization(fields.WebhookForm.AuthorizationHeader)
if err != nil { if err != nil {
ctx.ServerError("SetHeaderAuthorization", err) ctx.ServerError("SetHeaderAuthorization", err)
return return

View file

@ -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() { addSettingsVariablesRoutes := func() {
m.Group("/variables", func() { m.Group("/variables", func() {
m.Get("", repo_setting.Variables) m.Get("", repo_setting.Variables)
@ -618,12 +609,13 @@ func registerRoutes(m *web.Route) {
m.Group("/hooks", func() { m.Group("/hooks", func() {
m.Get("", user_setting.Webhooks) m.Get("", user_setting.Webhooks)
m.Post("/delete", user_setting.DeleteWebhook) 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.Group("/{id}", func() {
m.Get("", repo_setting.WebHooksEdit) m.Get("", repo_setting.WebHooksEdit)
m.Post("/replay/{uuid}", repo_setting.ReplayWebhook) m.Post("/replay/{uuid}", repo_setting.ReplayWebhook)
}) })
addWebhookEditRoutes() m.Post("/{type}/{id:[0-9]+}", repo_setting.WebhookUpdate)
}, webhooksEnabled) }, webhooksEnabled)
m.Group("/blocked_users", func() { m.Group("/blocked_users", func() {
@ -725,11 +717,12 @@ func registerRoutes(m *web.Route) {
m.Get("", repo_setting.WebHooksEdit) m.Get("", repo_setting.WebHooksEdit)
m.Post("/replay/{uuid}", repo_setting.ReplayWebhook) m.Post("/replay/{uuid}", repo_setting.ReplayWebhook)
}) })
addWebhookEditRoutes() m.Post("/{type}/{id:[0-9]+}", repo_setting.WebhookUpdate)
}, webhooksEnabled) }, webhooksEnabled)
m.Group("/{configType:default-hooks|system-hooks}", func() { 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() { m.Group("/auths", func() {
@ -887,12 +880,13 @@ func registerRoutes(m *web.Route) {
m.Group("/hooks", func() { m.Group("/hooks", func() {
m.Get("", org.Webhooks) m.Get("", org.Webhooks)
m.Post("/delete", org.DeleteWebhook) 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.Group("/{id}", func() {
m.Get("", repo_setting.WebHooksEdit) m.Get("", repo_setting.WebHooksEdit)
m.Post("/replay/{uuid}", repo_setting.ReplayWebhook) m.Post("/replay/{uuid}", repo_setting.ReplayWebhook)
}) })
addWebhookEditRoutes() m.Post("/{type}/{id:[0-9]+}", repo_setting.WebhookUpdate)
}, webhooksEnabled) }, webhooksEnabled)
m.Group("/labels", func() { m.Group("/labels", func() {
@ -1061,13 +1055,14 @@ func registerRoutes(m *web.Route) {
m.Group("/hooks", func() { m.Group("/hooks", func() {
m.Get("", repo_setting.Webhooks) m.Get("", repo_setting.Webhooks)
m.Post("/delete", repo_setting.DeleteWebhook) 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.Group("/{id}", func() {
m.Get("", repo_setting.WebHooksEdit) m.Get("", repo_setting.WebHooksEdit)
m.Post("/test", repo_setting.TestWebhook) m.Post("/test", repo_setting.TestWebhook)
m.Post("/replay/{uuid}", repo_setting.ReplayWebhook) m.Post("/replay/{uuid}", repo_setting.ReplayWebhook)
}) })
addWebhookEditRoutes() m.Post("/{type}/{id:[0-9]+}", repo_setting.WebhookUpdate)
}, webhooksEnabled) }, webhooksEnabled)
m.Group("/keys", func() { m.Group("/keys", func() {