From 4700d4c6aa048a1166d212892e9a85cba22738c5 Mon Sep 17 00:00:00 2001 From: techknowlogick Date: Mon, 28 Oct 2024 17:14:12 -0400 Subject: [PATCH] add UI for editing secrets --- routers/web/repo/setting/secrets.go | 20 ++++++++++++ routers/web/shared/secrets/secrets.go | 33 +++++++++++++++++++ routers/web/web.go | 1 + services/forms/user_form.go | 11 +++++++ templates/shared/secrets/add_list.tmpl | 45 +++++++++++++++++++++++++- 5 files changed, 109 insertions(+), 1 deletion(-) diff --git a/routers/web/repo/setting/secrets.go b/routers/web/repo/setting/secrets.go index df11729344..ee3e7e7812 100644 --- a/routers/web/repo/setting/secrets.go +++ b/routers/web/repo/setting/secrets.go @@ -114,6 +114,26 @@ func SecretsPost(ctx *context.Context) { ) } +func SecretsEdit(ctx *context.Context) { + sCtx, err := getSecretsCtx(ctx) + if err != nil { + ctx.ServerError("getSecretsCtx", err) + return + } + + if ctx.HasError() { + ctx.JSONError(ctx.GetErrMsg()) + return + } + + shared.PerformSecretsEdit( + ctx, + sCtx.OwnerID, + sCtx.RepoID, + sCtx.RedirectLink, + ) +} + func SecretsDelete(ctx *context.Context) { sCtx, err := getSecretsCtx(ctx) if err != nil { diff --git a/routers/web/shared/secrets/secrets.go b/routers/web/shared/secrets/secrets.go index 3bd421f86a..e1437e691b 100644 --- a/routers/web/shared/secrets/secrets.go +++ b/routers/web/shared/secrets/secrets.go @@ -4,6 +4,8 @@ package secrets import ( + "fmt" + "code.gitea.io/gitea/models/db" secret_model "code.gitea.io/gitea/models/secret" "code.gitea.io/gitea/modules/log" @@ -38,6 +40,37 @@ func PerformSecretsPost(ctx *context.Context, ownerID, repoID int64, redirectURL ctx.JSONRedirect(redirectURL) } +func PerformSecretsEdit(ctx *context.Context, ownerID, repoID int64, redirectURL string) { + form := web.GetForm(ctx).(*forms.EditSecretForm) + id := ctx.PathParamInt64("secret_id") + + if id == 0 { + ctx.ServerError("PerformSecretsEdit", fmt.Errorf("id not found")) + return + } + + secrets, err := db.Find[secret_model.Secret](ctx, secret_model.FindSecretsOptions{OwnerID: ownerID, RepoID: repoID, SecretID: id}) + if err != nil { + ctx.ServerError("FindSecrets", err) + return + } + + if len(secrets) != 1 { + ctx.ServerError("FindSecrets", fmt.Errorf("secret not found")) + return + } + + s, _, err := secret_service.CreateOrUpdateSecret(ctx, ownerID, repoID, secrets[0].Name, util.ReserveLineBreakForTextarea(form.Data)) + if err != nil { + log.Error("CreateOrUpdateSecret failed: %v", err) + ctx.JSONError(ctx.Tr("secrets.edit.failed")) + return + } + + ctx.Flash.Success(ctx.Tr("secrets.edit.success", s.Name)) + ctx.JSONRedirect(redirectURL) +} + func PerformSecretsDelete(ctx *context.Context, ownerID, repoID int64, redirectURL string) { id := ctx.FormInt64("id") diff --git a/routers/web/web.go b/routers/web/web.go index 69258bca18..4e0cf35ba0 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -446,6 +446,7 @@ func registerRoutes(m *web.Router) { m.Group("/secrets", func() { m.Get("", repo_setting.Secrets) m.Post("", web.Bind(forms.AddSecretForm{}), repo_setting.SecretsPost) + m.Post("/{secret_id}/edit", web.Bind(forms.EditSecretForm{}), repo_setting.SecretsEdit) m.Post("/delete", repo_setting.SecretsDelete) }) } diff --git a/services/forms/user_form.go b/services/forms/user_form.go index 5b7a43642a..a3f3737224 100644 --- a/services/forms/user_form.go +++ b/services/forms/user_form.go @@ -335,6 +335,17 @@ func (f *AddSecretForm) Validate(req *http.Request, errs binding.Errors) binding return middleware.Validate(errs, ctx.Data, f, ctx.Locale) } +// AddSecretForm for adding secrets +type EditSecretForm struct { + Data string `binding:"Required;MaxSize(65535)"` +} + +// Validate validates the fields +func (f *EditSecretForm) Validate(req *http.Request, errs binding.Errors) binding.Errors { + ctx := context.GetValidateContext(req) + return middleware.Validate(errs, ctx.Data, f, ctx.Locale) +} + type EditVariableForm struct { Name string `binding:"Required;MaxSize(255)"` Data string `binding:"Required;MaxSize(65535)"` diff --git a/templates/shared/secrets/add_list.tmpl b/templates/shared/secrets/add_list.tmpl index ea59459083..f3fdd628ce 100644 --- a/templates/shared/secrets/add_list.tmpl +++ b/templates/shared/secrets/add_list.tmpl @@ -30,6 +30,15 @@ {{ctx.Locale.Tr "settings.added_on" (DateTime "short" .CreatedUnix)}} +