aboutsummaryrefslogtreecommitdiffstats
path: root/models/secret/secret.go
diff options
context:
space:
mode:
authorBo-Yi Wu <appleboy.tw@gmail.com>2023-08-30 04:54:49 +0800
committerGitHub <noreply@github.com>2023-08-29 20:54:49 +0000
commitb91057b172dea07a9db1bf96a32d2ab25a0e030d (patch)
treeb4a88a07895a2fba14998a577cd8b778c2400dfb /models/secret/secret.go
parent2d9249b6d9b57dea57b70357432bda945504c4b5 (diff)
downloadgitea-b91057b172dea07a9db1bf96a32d2ab25a0e030d.tar.gz
gitea-b91057b172dea07a9db1bf96a32d2ab25a0e030d.zip
feat(API): add route and implementation for creating/updating repository secret (#26766)
spec: https://docs.github.com/en/rest/actions/secrets?apiVersion=2022-11-28#create-or-update-a-repository-secret - Add a new route for creating or updating a secret value in a repository - Create a new file `routers/api/v1/repo/action.go` with the implementation of the `CreateOrUpdateSecret` function - Update the Swagger documentation for the `updateRepoSecret` operation in the `v1_json.tmpl` template file --------- Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com> Co-authored-by: Giteabot <teabot@gitea.io>
Diffstat (limited to 'models/secret/secret.go')
-rw-r--r--models/secret/secret.go28
1 files changed, 28 insertions, 0 deletions
diff --git a/models/secret/secret.go b/models/secret/secret.go
index 410cb3770e..1cb816e9db 100644
--- a/models/secret/secret.go
+++ b/models/secret/secret.go
@@ -160,3 +160,31 @@ func DeleteSecret(ctx context.Context, orgID, repoID int64, name string) error {
return nil
}
+
+// CreateOrUpdateSecret creates or updates a secret and returns true if it was created
+func CreateOrUpdateSecret(ctx context.Context, orgID, repoID int64, name, data string) (bool, error) {
+ sc := new(Secret)
+ name = strings.ToUpper(name)
+ has, err := db.GetEngine(ctx).
+ Where("owner_id=?", orgID).
+ And("repo_id=?", repoID).
+ And("name=?", name).
+ Get(sc)
+ if err != nil {
+ return false, err
+ }
+
+ if !has {
+ _, err = InsertEncryptedSecret(ctx, orgID, repoID, name, data)
+ if err != nil {
+ return false, err
+ }
+ return true, nil
+ }
+
+ if err := UpdateSecret(ctx, orgID, repoID, name, data); err != nil {
+ return false, err
+ }
+
+ return false, nil
+}