add UI for editing secrets

This commit is contained in:
techknowlogick 2024-10-28 17:14:12 -04:00
parent d6d3c96e65
commit 4700d4c6aa
5 changed files with 109 additions and 1 deletions

View File

@ -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) { func SecretsDelete(ctx *context.Context) {
sCtx, err := getSecretsCtx(ctx) sCtx, err := getSecretsCtx(ctx)
if err != nil { if err != nil {

View File

@ -4,6 +4,8 @@
package secrets package secrets
import ( import (
"fmt"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
secret_model "code.gitea.io/gitea/models/secret" secret_model "code.gitea.io/gitea/models/secret"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
@ -38,6 +40,37 @@ func PerformSecretsPost(ctx *context.Context, ownerID, repoID int64, redirectURL
ctx.JSONRedirect(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) { func PerformSecretsDelete(ctx *context.Context, ownerID, repoID int64, redirectURL string) {
id := ctx.FormInt64("id") id := ctx.FormInt64("id")

View File

@ -446,6 +446,7 @@ func registerRoutes(m *web.Router) {
m.Group("/secrets", func() { m.Group("/secrets", func() {
m.Get("", repo_setting.Secrets) m.Get("", repo_setting.Secrets)
m.Post("", web.Bind(forms.AddSecretForm{}), repo_setting.SecretsPost) 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) m.Post("/delete", repo_setting.SecretsDelete)
}) })
} }

View File

@ -335,6 +335,17 @@ func (f *AddSecretForm) Validate(req *http.Request, errs binding.Errors) binding
return middleware.Validate(errs, ctx.Data, f, ctx.Locale) 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 { type EditVariableForm struct {
Name string `binding:"Required;MaxSize(255)"` Name string `binding:"Required;MaxSize(255)"`
Data string `binding:"Required;MaxSize(65535)"` Data string `binding:"Required;MaxSize(65535)"`

View File

@ -30,6 +30,15 @@
<span class="color-text-light-2"> <span class="color-text-light-2">
{{ctx.Locale.Tr "settings.added_on" (DateTime "short" .CreatedUnix)}} {{ctx.Locale.Tr "settings.added_on" (DateTime "short" .CreatedUnix)}}
</span> </span>
<button class="ui btn interact-bg tw-p-2 show-modal"
data-modal="#edit-secret-modal"
data-modal-form.action="{{$.Link}}/{{.ID}}/edit"
data-modal-header="{{ctx.Locale.Tr "secrets.edit"}}"
data-modal-secret-name="{{.Name}}"
data-tooltip-content="{{ctx.Locale.Tr "secrets.edit"}}"
>
{{svg "octicon-pencil"}}
</button>
<button class="ui btn interact-bg link-action tw-p-2" <button class="ui btn interact-bg link-action tw-p-2"
data-url="{{$.Link}}/delete?id={{.ID}}" data-url="{{$.Link}}/delete?id={{.ID}}"
data-modal-confirm="{{ctx.Locale.Tr "secrets.deletion.description"}}" data-modal-confirm="{{ctx.Locale.Tr "secrets.deletion.description"}}"
@ -60,8 +69,42 @@
<div class="field"> <div class="field">
<label for="secret-name">{{ctx.Locale.Tr "name"}}</label> <label for="secret-name">{{ctx.Locale.Tr "name"}}</label>
<input autofocus required <input autofocus required
id="secret-name"
name="name" name="name"
id="secret-name"
value="{{.name}}"
pattern="^(?!GITEA_|GITHUB_)[a-zA-Z_][a-zA-Z0-9_]*$"
placeholder="{{ctx.Locale.Tr "secrets.creation.name_placeholder"}}"
>
</div>
<div class="field">
<label for="secret-data">{{ctx.Locale.Tr "value"}}</label>
<textarea required
id="secret-data"
name="data"
placeholder="{{ctx.Locale.Tr "secrets.creation.value_placeholder"}}"
></textarea>
</div>
</div>
{{template "base/modal_actions_confirm" (dict "ModalButtonTypes" "confirm")}}
</form>
</div>
{{/* Edit secret dialog */}}
<div class="ui small modal" id="edit-secret-modal">
<div class="header">
<span id="actions-modal-header"></span>
</div>
<form class="ui form form-fetch-action" method="post">
<div class="content">
{{.CsrfTokenHtml}}
<div class="field">
{{ctx.Locale.Tr "secrets.description"}}
</div>
<div class="field">
<label for="secret-name">{{ctx.Locale.Tr "name"}}</label>
<input disabled
name="name"
id="secret-name"
value="{{.name}}" value="{{.name}}"
pattern="^(?!GITEA_|GITHUB_)[a-zA-Z_][a-zA-Z0-9_]*$" pattern="^(?!GITEA_|GITHUB_)[a-zA-Z_][a-zA-Z0-9_]*$"
placeholder="{{ctx.Locale.Tr "secrets.creation.name_placeholder"}}" placeholder="{{ctx.Locale.Tr "secrets.creation.name_placeholder"}}"