Skip to content

Commit 8cd4602

Browse files
authored
refactor(API): refactor secret creation and update functionality (#26751)
According to the GitHub API Spec: https://docs.github.com/en/rest/actions/secrets?apiVersion=2022-11-28#create-or-update-an-organization-secret Merge the Create and Update secret into a single API. - Remove the `CreateSecretOption` struct and replace it with `CreateOrUpdateSecretOption` in `modules/structs/secret.go` - Update the `CreateOrUpdateOrgSecret` function in `routers/api/v1/org/action.go` to use `CreateOrUpdateSecretOption` instead of `UpdateSecretOption` - Remove the `CreateOrgSecret` function in `routers/api/v1/org/action.go` and replace it with `CreateOrUpdateOrgSecret` - Update the Swagger documentation in `routers/api/v1/swagger/options.go` and `templates/swagger/v1_json.tmpl` to reflect the changes in the struct names and function names Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent 6945918 commit 8cd4602

File tree

5 files changed

+51
-155
lines changed

5 files changed

+51
-155
lines changed

modules/structs/secret.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,9 @@ type Secret struct {
1414
Created time.Time `json:"created_at"`
1515
}
1616

17-
// CreateSecretOption options when creating secret
17+
// CreateOrUpdateSecretOption options when creating or updating secret
1818
// swagger:model
19-
type CreateSecretOption struct {
20-
// Name of the secret to create
21-
//
22-
// required: true
23-
// unique: true
24-
Name string `json:"name" binding:"Required;AlphaDashDot;MaxSize(100)"`
25-
// Data of the secret to create
26-
Data string `json:"data" binding:"Required"`
27-
}
28-
29-
// UpdateSecretOption options when updating secret
30-
// swagger:model
31-
type UpdateSecretOption struct {
19+
type CreateOrUpdateSecretOption struct {
3220
// Data of the secret to update
3321
//
3422
// required: true

routers/api/v1/api.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,9 +1300,8 @@ func Routes() *web.Route {
13001300
})
13011301
m.Group("/actions/secrets", func() {
13021302
m.Get("", reqToken(), reqOrgOwnership(), org.ListActionsSecrets)
1303-
m.Post("", reqToken(), reqOrgOwnership(), bind(api.CreateSecretOption{}), org.CreateOrgSecret)
13041303
m.Combo("/{secretname}").
1305-
Put(reqToken(), reqOrgOwnership(), bind(api.UpdateSecretOption{}), org.UpdateOrgSecret).
1304+
Put(reqToken(), reqOrgOwnership(), bind(api.CreateOrUpdateSecretOption{}), org.CreateOrUpdateOrgSecret).
13061305
Delete(reqToken(), reqOrgOwnership(), org.DeleteOrgSecret)
13071306
})
13081307
m.Group("/public_members", func() {

routers/api/v1/org/action.go

Lines changed: 22 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"code.gitea.io/gitea/modules/web"
1313
"code.gitea.io/gitea/routers/api/v1/utils"
1414
"code.gitea.io/gitea/routers/web/shared/actions"
15-
"code.gitea.io/gitea/services/convert"
1615
)
1716

1817
// ListActionsSecrets list an organization's actions secrets
@@ -74,55 +73,11 @@ func listActionsSecrets(ctx *context.APIContext) {
7473
ctx.JSON(http.StatusOK, apiSecrets)
7574
}
7675

77-
// CreateOrgSecret create one secret of the organization
78-
func CreateOrgSecret(ctx *context.APIContext) {
79-
// swagger:operation POST /orgs/{org}/actions/secrets organization createOrgSecret
80-
// ---
81-
// summary: Create a secret in an organization
82-
// consumes:
83-
// - application/json
84-
// produces:
85-
// - application/json
86-
// parameters:
87-
// - name: org
88-
// in: path
89-
// description: name of organization
90-
// type: string
91-
// required: true
92-
// - name: body
93-
// in: body
94-
// schema:
95-
// "$ref": "#/definitions/CreateSecretOption"
96-
// responses:
97-
// "201":
98-
// "$ref": "#/responses/Secret"
99-
// "400":
100-
// "$ref": "#/responses/error"
101-
// "404":
102-
// "$ref": "#/responses/notFound"
103-
// "403":
104-
// "$ref": "#/responses/forbidden"
105-
opt := web.GetForm(ctx).(*api.CreateSecretOption)
106-
if err := actions.NameRegexMatch(opt.Name); err != nil {
107-
ctx.Error(http.StatusBadRequest, "CreateOrgSecret", err)
108-
return
109-
}
110-
s, err := secret_model.InsertEncryptedSecret(
111-
ctx, ctx.Org.Organization.ID, 0, opt.Name, actions.ReserveLineBreakForTextarea(opt.Data),
112-
)
113-
if err != nil {
114-
ctx.Error(http.StatusInternalServerError, "InsertEncryptedSecret", err)
115-
return
116-
}
117-
118-
ctx.JSON(http.StatusCreated, convert.ToSecret(s))
119-
}
120-
121-
// UpdateOrgSecret update one secret of the organization
122-
func UpdateOrgSecret(ctx *context.APIContext) {
76+
// create or update one secret of the organization
77+
func CreateOrUpdateOrgSecret(ctx *context.APIContext) {
12378
// swagger:operation PUT /orgs/{org}/actions/secrets/{secretname} organization updateOrgSecret
12479
// ---
125-
// summary: Update a secret value in an organization
80+
// summary: Create or Update a secret value in an organization
12681
// consumes:
12782
// - application/json
12883
// produces:
@@ -141,19 +96,34 @@ func UpdateOrgSecret(ctx *context.APIContext) {
14196
// - name: body
14297
// in: body
14398
// schema:
144-
// "$ref": "#/definitions/UpdateSecretOption"
99+
// "$ref": "#/definitions/CreateOrUpdateSecretOption"
145100
// responses:
101+
// "201":
102+
// description: response when creating a secret
146103
// "204":
147-
// description: update one secret of the organization
104+
// description: response when updating a secret
105+
// "400":
106+
// "$ref": "#/responses/error"
148107
// "403":
149108
// "$ref": "#/responses/forbidden"
150109
secretName := ctx.Params(":secretname")
151-
opt := web.GetForm(ctx).(*api.UpdateSecretOption)
110+
if err := actions.NameRegexMatch(secretName); err != nil {
111+
ctx.Error(http.StatusBadRequest, "CreateOrUpdateOrgSecret", err)
112+
return
113+
}
114+
opt := web.GetForm(ctx).(*api.CreateOrUpdateSecretOption)
152115
err := secret_model.UpdateSecret(
153116
ctx, ctx.Org.Organization.ID, 0, secretName, opt.Data,
154117
)
155118
if secret_model.IsErrSecretNotFound(err) {
156-
ctx.NotFound(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)
157127
return
158128
}
159129
if err != nil {

routers/api/v1/swagger/options.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,5 @@ type swaggerParameterBodies struct {
189189
UpdateRepoAvatarOptions api.UpdateRepoAvatarOption
190190

191191
// in:body
192-
CreateSecretOption api.CreateSecretOption
193-
194-
// in:body
195-
UpdateSecretOption api.UpdateSecretOption
192+
CreateOrUpdateSecretOption api.CreateOrUpdateSecretOption
196193
}

templates/swagger/v1_json.tmpl

Lines changed: 25 additions & 83 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)