diff options
author | oliverpool <git@olivier.pfad.fr> | 2024-03-23 22:43:44 +0100 |
---|---|---|
committer | oliverpool <git@olivier.pfad.fr> | 2024-03-27 22:26:55 +0100 |
commit | 635230ca5debd9f230fcf216add83fa5658bc3c2 (patch) | |
tree | 328f29209cb21c6ff40a42519c2bb20f338bb422 /routers | |
parent | [REFACTOR] merge once-called functions (diff) | |
download | forgejo-635230ca5debd9f230fcf216add83fa5658bc3c2.tar.xz forgejo-635230ca5debd9f230fcf216add83fa5658bc3c2.zip |
[TESTS] webhook forms keep submitted data when invalid
Diffstat (limited to 'routers')
-rw-r--r-- | routers/web/repo/setting/webhook.go | 44 |
1 files changed, 31 insertions, 13 deletions
diff --git a/routers/web/repo/setting/webhook.go b/routers/web/repo/setting/webhook.go index e6e869d8f6..29d2573451 100644 --- a/routers/web/repo/setting/webhook.go +++ b/routers/web/repo/setting/webhook.go @@ -219,6 +219,22 @@ func WebhookCreate(ctx *context.Context) { ctx.Data["BaseLinkNew"] = orCtx.LinkNew if ctx.HasError() { + // pre-fill the form with the submitted data + var w webhook.Webhook + 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 + err := w.SetHeaderAuthorization(fields.WebhookForm.AuthorizationHeader) + if err != nil { + ctx.ServerError("SetHeaderAuthorization", err) + return + } + ctx.Data["Webhook"] = w + ctx.Data["HookMetadata"] = fields.Metadata + ctx.HTML(http.StatusUnprocessableEntity, orCtx.NewTemplate) return } @@ -284,13 +300,27 @@ func WebhookUpdate(ctx *context.Context) { middleware.Validate(errs, ctx.Data, form, ctx.Locale) // error checked below in ctx.HasError }) + // pre-fill the form with the submitted data + 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 + + err := w.SetHeaderAuthorization(fields.WebhookForm.AuthorizationHeader) + if err != nil { + ctx.ServerError("SetHeaderAuthorization", err) + return + } + if ctx.HasError() { + ctx.Data["HookMetadata"] = fields.Metadata ctx.HTML(http.StatusUnprocessableEntity, orCtx.NewTemplate) return } var meta []byte - var err error if fields.Metadata != nil { meta, err = json.Marshal(fields.Metadata) if err != nil { @@ -299,20 +329,8 @@ func WebhookUpdate(ctx *context.Context) { } } - 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(fields.WebhookForm.AuthorizationHeader) - if err != nil { - ctx.ServerError("SetHeaderAuthorization", err) - return - } - if err := w.UpdateEvent(); err != nil { ctx.ServerError("UpdateEvent", err) return |