Skip to content

Commit 62bede0

Browse files
committed
refactor: refactor secret creation and update logic
- Modify the function `CreateOrUpdateOrgSecret` in `routers/api/v1/org/action.go` - Replace the call to `secret_model.UpdateSecret` with `secret_model.CreateOrUpdateSecret` - Remove the block of code that handles the case when the secret is not found - Remove the call to `secret_model.InsertEncryptedSecret` - Add a new variable `isCreated` to check if the secret was created or updated - Update the error handling for the function `CreateOrUpdateOrgSecret` - Modify the function `CreateOrUpdateSecret` in `routers/api/v1/repo/action.go` - Replace the call to `secret_model.UpdateSecret` with `secret_model.CreateOrUpdateSecret` - Remove the block of code that handles the case when the secret is not found - Remove the call to `secret_model.InsertEncryptedSecret` - Add a new variable `isCreated` to check if the secret was created or updated - Update the error handling for the function `CreateOrUpdateSecret` Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent 52735c7 commit 62bede0

File tree

2 files changed

+10
-28
lines changed

2 files changed

+10
-28
lines changed

routers/api/v1/org/action.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,13 @@ func CreateOrUpdateOrgSecret(ctx *context.APIContext) {
112112
return
113113
}
114114
opt := web.GetForm(ctx).(*api.CreateOrUpdateSecretOption)
115-
err := secret_model.UpdateSecret(
116-
ctx, ctx.Org.Organization.ID, 0, secretName, opt.Data,
117-
)
118-
if secret_model.IsErrSecretNotFound(err) {
119-
_, err := secret_model.InsertEncryptedSecret(
120-
ctx, ctx.Org.Organization.ID, 0, secretName, actions.ReserveLineBreakForTextarea(opt.Data),
121-
)
122-
if err != nil {
123-
ctx.Error(http.StatusInternalServerError, "InsertEncryptedSecret", err)
124-
return
125-
}
126-
ctx.Status(http.StatusCreated)
115+
isCreated, err := secret_model.CreateOrUpdateSecret(ctx, ctx.Org.Organization.ID, 0, secretName, opt.Data)
116+
if err != nil {
117+
ctx.Error(http.StatusInternalServerError, "CreateOrUpdateSecret", err)
127118
return
128119
}
129-
if err != nil {
130-
ctx.Error(http.StatusInternalServerError, "UpdateSecret", err)
120+
if isCreated {
121+
ctx.Status(http.StatusCreated)
131122
return
132123
}
133124

routers/api/v1/repo/action.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,13 @@ func CreateOrUpdateSecret(ctx *context.APIContext) {
6161
return
6262
}
6363
opt := web.GetForm(ctx).(*api.CreateOrUpdateSecretOption)
64-
err := secret_model.UpdateSecret(
65-
ctx, owner.ID, repo.ID, secretName, opt.Data,
66-
)
67-
if secret_model.IsErrSecretNotFound(err) {
68-
_, err := secret_model.InsertEncryptedSecret(
69-
ctx, owner.ID, repo.ID, secretName, actions.ReserveLineBreakForTextarea(opt.Data),
70-
)
71-
if err != nil {
72-
ctx.Error(http.StatusInternalServerError, "InsertEncryptedSecret", err)
73-
return
74-
}
75-
ctx.Status(http.StatusCreated)
64+
isCreated, err := secret_model.CreateOrUpdateSecret(ctx, owner.ID, repo.ID, secretName, opt.Data)
65+
if err != nil {
66+
ctx.Error(http.StatusInternalServerError, "CreateOrUpdateSecret", err)
7667
return
7768
}
78-
if err != nil {
79-
ctx.Error(http.StatusInternalServerError, "UpdateSecret", err)
69+
if isCreated {
70+
ctx.Status(http.StatusCreated)
8071
return
8172
}
8273

0 commit comments

Comments
 (0)