loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 <appleboy.tw@gmail.com>

authored by

Bo-Yi Wu and committed by
GitHub
8cd46024 6945918d

+51 -155
+2 -14
modules/structs/secret.go
··· 14 14 Created time.Time `json:"created_at"` 15 15 } 16 16 17 - // CreateSecretOption options when creating secret 18 - // 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 17 + // CreateOrUpdateSecretOption options when creating or updating secret 30 18 // swagger:model 31 - type UpdateSecretOption struct { 19 + type CreateOrUpdateSecretOption struct { 32 20 // Data of the secret to update 33 21 // 34 22 // required: true
+1 -2
routers/api/v1/api.go
··· 1300 1300 }) 1301 1301 m.Group("/actions/secrets", func() { 1302 1302 m.Get("", reqToken(), reqOrgOwnership(), org.ListActionsSecrets) 1303 - m.Post("", reqToken(), reqOrgOwnership(), bind(api.CreateSecretOption{}), org.CreateOrgSecret) 1304 1303 m.Combo("/{secretname}"). 1305 - Put(reqToken(), reqOrgOwnership(), bind(api.UpdateSecretOption{}), org.UpdateOrgSecret). 1304 + Put(reqToken(), reqOrgOwnership(), bind(api.CreateOrUpdateSecretOption{}), org.CreateOrUpdateOrgSecret). 1306 1305 Delete(reqToken(), reqOrgOwnership(), org.DeleteOrgSecret) 1307 1306 }) 1308 1307 m.Group("/public_members", func() {
+22 -52
routers/api/v1/org/action.go
··· 12 12 "code.gitea.io/gitea/modules/web" 13 13 "code.gitea.io/gitea/routers/api/v1/utils" 14 14 "code.gitea.io/gitea/routers/web/shared/actions" 15 - "code.gitea.io/gitea/services/convert" 16 15 ) 17 16 18 17 // ListActionsSecrets list an organization's actions secrets ··· 74 73 ctx.JSON(http.StatusOK, apiSecrets) 75 74 } 76 75 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) { 123 78 // swagger:operation PUT /orgs/{org}/actions/secrets/{secretname} organization updateOrgSecret 124 79 // --- 125 - // summary: Update a secret value in an organization 80 + // summary: Create or Update a secret value in an organization 126 81 // consumes: 127 82 // - application/json 128 83 // produces: ··· 141 96 // - name: body 142 97 // in: body 143 98 // schema: 144 - // "$ref": "#/definitions/UpdateSecretOption" 99 + // "$ref": "#/definitions/CreateOrUpdateSecretOption" 145 100 // responses: 101 + // "201": 102 + // description: response when creating a secret 146 103 // "204": 147 - // description: update one secret of the organization 104 + // description: response when updating a secret 105 + // "400": 106 + // "$ref": "#/responses/error" 148 107 // "403": 149 108 // "$ref": "#/responses/forbidden" 150 109 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) 152 115 err := secret_model.UpdateSecret( 153 116 ctx, ctx.Org.Organization.ID, 0, secretName, opt.Data, 154 117 ) 155 118 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) 157 127 return 158 128 } 159 129 if err != nil {
+1 -4
routers/api/v1/swagger/options.go
··· 189 189 UpdateRepoAvatarOptions api.UpdateRepoAvatarOption 190 190 191 191 // in:body 192 - CreateSecretOption api.CreateSecretOption 193 - 194 - // in:body 195 - UpdateSecretOption api.UpdateSecretOption 192 + CreateOrUpdateSecretOption api.CreateOrUpdateSecretOption 196 193 }
+25 -83
templates/swagger/v1_json.tmpl
··· 1586 1586 "$ref": "#/responses/SecretList" 1587 1587 } 1588 1588 } 1589 - }, 1590 - "post": { 1591 - "consumes": [ 1592 - "application/json" 1593 - ], 1594 - "produces": [ 1595 - "application/json" 1596 - ], 1597 - "tags": [ 1598 - "organization" 1599 - ], 1600 - "summary": "Create a secret in an organization", 1601 - "operationId": "createOrgSecret", 1602 - "parameters": [ 1603 - { 1604 - "type": "string", 1605 - "description": "name of organization", 1606 - "name": "org", 1607 - "in": "path", 1608 - "required": true 1609 - }, 1610 - { 1611 - "name": "body", 1612 - "in": "body", 1613 - "schema": { 1614 - "$ref": "#/definitions/CreateSecretOption" 1615 - } 1616 - } 1617 - ], 1618 - "responses": { 1619 - "201": { 1620 - "$ref": "#/responses/Secret" 1621 - }, 1622 - "400": { 1623 - "$ref": "#/responses/error" 1624 - }, 1625 - "403": { 1626 - "$ref": "#/responses/forbidden" 1627 - }, 1628 - "404": { 1629 - "$ref": "#/responses/notFound" 1630 - } 1631 - } 1632 1589 } 1633 1590 }, 1634 1591 "/orgs/{org}/actions/secrets/{secretname}": { ··· 1642 1599 "tags": [ 1643 1600 "organization" 1644 1601 ], 1645 - "summary": "Update a secret value in an organization", 1602 + "summary": "Create or Update a secret value in an organization", 1646 1603 "operationId": "updateOrgSecret", 1647 1604 "parameters": [ 1648 1605 { ··· 1663 1620 "name": "body", 1664 1621 "in": "body", 1665 1622 "schema": { 1666 - "$ref": "#/definitions/UpdateSecretOption" 1623 + "$ref": "#/definitions/CreateOrUpdateSecretOption" 1667 1624 } 1668 1625 } 1669 1626 ], 1670 1627 "responses": { 1628 + "201": { 1629 + "description": "response when creating a secret" 1630 + }, 1671 1631 "204": { 1672 - "description": "update one secret of the organization" 1632 + "description": "response when updating a secret" 1633 + }, 1634 + "400": { 1635 + "$ref": "#/responses/error" 1673 1636 }, 1674 1637 "403": { 1675 1638 "$ref": "#/responses/forbidden" ··· 17283 17246 }, 17284 17247 "x-go-package": "code.gitea.io/gitea/modules/structs" 17285 17248 }, 17249 + "CreateOrUpdateSecretOption": { 17250 + "description": "CreateOrUpdateSecretOption options when creating or updating secret", 17251 + "type": "object", 17252 + "required": [ 17253 + "data" 17254 + ], 17255 + "properties": { 17256 + "data": { 17257 + "description": "Data of the secret to update", 17258 + "type": "string", 17259 + "x-go-name": "Data" 17260 + } 17261 + }, 17262 + "x-go-package": "code.gitea.io/gitea/modules/structs" 17263 + }, 17286 17264 "CreateOrgOption": { 17287 17265 "description": "CreateOrgOption options for creating an organization", 17288 17266 "type": "object", ··· 17565 17543 "collaboratorcommitter" 17566 17544 ], 17567 17545 "x-go-name": "TrustModel" 17568 - } 17569 - }, 17570 - "x-go-package": "code.gitea.io/gitea/modules/structs" 17571 - }, 17572 - "CreateSecretOption": { 17573 - "description": "CreateSecretOption options when creating secret", 17574 - "type": "object", 17575 - "required": [ 17576 - "name" 17577 - ], 17578 - "properties": { 17579 - "data": { 17580 - "description": "Data of the secret to create", 17581 - "type": "string", 17582 - "x-go-name": "Data" 17583 - }, 17584 - "name": { 17585 - "description": "Name of the secret to create", 17586 - "type": "string", 17587 - "uniqueItems": true, 17588 - "x-go-name": "Name" 17589 17546 } 17590 17547 }, 17591 17548 "x-go-package": "code.gitea.io/gitea/modules/structs" ··· 21978 21935 }, 21979 21936 "x-go-package": "code.gitea.io/gitea/modules/structs" 21980 21937 }, 21981 - "UpdateSecretOption": { 21982 - "description": "UpdateSecretOption options when updating secret", 21983 - "type": "object", 21984 - "required": [ 21985 - "data" 21986 - ], 21987 - "properties": { 21988 - "data": { 21989 - "description": "Data of the secret to update", 21990 - "type": "string", 21991 - "x-go-name": "Data" 21992 - } 21993 - }, 21994 - "x-go-package": "code.gitea.io/gitea/modules/structs" 21995 - }, 21996 21938 "UpdateUserAvatarOption": { 21997 21939 "description": "UpdateUserAvatarUserOption options when updating the user avatar", 21998 21940 "type": "object", ··· 23309 23251 "parameterBodies": { 23310 23252 "description": "parameterBodies", 23311 23253 "schema": { 23312 - "$ref": "#/definitions/UpdateSecretOption" 23254 + "$ref": "#/definitions/CreateOrUpdateSecretOption" 23313 23255 } 23314 23256 }, 23315 23257 "redirect": {