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.

feat: implement organization secret creation API (#26566)

- Add a new `CreateSecretOption` struct for creating secrets
- Implement a `CreateOrgSecret` function to create a secret in an
organization
- Add a new route in `api.go` to handle the creation of organization
secrets
- Update the Swagger template to include the new `CreateOrgSecret` API
endpoint

---------

Signed-off-by: appleboy <appleboy.tw@gmail.com>

authored by

Bo-Yi Wu and committed by
GitHub
23addde2 a4a567f2

+161 -7
+13 -1
modules/structs/secret.go
··· 5 5 6 6 import "time" 7 7 8 - // User represents a secret 8 + // Secret represents a secret 9 9 // swagger:model 10 10 type Secret struct { 11 11 // the secret's name ··· 13 13 // swagger:strfmt date-time 14 14 Created time.Time `json:"created_at"` 15 15 } 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 + }
+1
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) 1303 1304 }) 1304 1305 m.Group("/public_members", func() { 1305 1306 m.Get("", org.ListPublicMembers)
+47 -4
routers/api/v1/org/action.go
··· 6 6 import ( 7 7 "net/http" 8 8 9 - "code.gitea.io/gitea/models/secret" 9 + secret_model "code.gitea.io/gitea/models/secret" 10 10 "code.gitea.io/gitea/modules/context" 11 11 api "code.gitea.io/gitea/modules/structs" 12 + "code.gitea.io/gitea/modules/web" 12 13 "code.gitea.io/gitea/routers/api/v1/utils" 14 + "code.gitea.io/gitea/routers/web/shared/actions" 15 + "code.gitea.io/gitea/services/convert" 13 16 ) 14 17 15 18 // ListActionsSecrets list an organization's actions secrets ··· 42 45 43 46 // listActionsSecrets list an organization's actions secrets 44 47 func listActionsSecrets(ctx *context.APIContext) { 45 - opts := &secret.FindSecretsOptions{ 48 + opts := &secret_model.FindSecretsOptions{ 46 49 OwnerID: ctx.Org.Organization.ID, 47 50 ListOptions: utils.GetListOptions(ctx), 48 51 } 49 52 50 - count, err := secret.CountSecrets(ctx, opts) 53 + count, err := secret_model.CountSecrets(ctx, opts) 51 54 if err != nil { 52 55 ctx.InternalServerError(err) 53 56 return 54 57 } 55 58 56 - secrets, err := secret.FindSecrets(ctx, *opts) 59 + secrets, err := secret_model.FindSecrets(ctx, *opts) 57 60 if err != nil { 58 61 ctx.InternalServerError(err) 59 62 return ··· 70 73 ctx.SetTotalCountHeader(count) 71 74 ctx.JSON(http.StatusOK, apiSecrets) 72 75 } 76 + 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 + s, err := secret_model.InsertEncryptedSecret( 107 + ctx, ctx.Org.Organization.ID, 0, opt.Name, actions.ReserveLineBreakForTextarea(opt.Data), 108 + ) 109 + if err != nil { 110 + ctx.Error(http.StatusInternalServerError, "InsertEncryptedSecret", err) 111 + return 112 + } 113 + 114 + ctx.JSON(http.StatusCreated, convert.ToSecret(s)) 115 + }
+7
routers/api/v1/swagger/action.go
··· 11 11 // in:body 12 12 Body []api.Secret `json:"body"` 13 13 } 14 + 15 + // Secret 16 + // swagger:response Secret 17 + type swaggerResponseSecret struct { 18 + // in:body 19 + Body api.Secret `json:"body"` 20 + }
+3
routers/api/v1/swagger/options.go
··· 187 187 188 188 // in:body 189 189 UpdateRepoAvatarOptions api.UpdateRepoAvatarOption 190 + 191 + // in:body 192 + CreateSecretOption api.CreateSecretOption 190 193 }
+18
services/convert/secret.go
··· 1 + // Copyright 2023 The Gitea Authors. All rights reserved. 2 + // SPDX-License-Identifier: MIT 3 + 4 + package convert 5 + 6 + import ( 7 + secret_model "code.gitea.io/gitea/models/secret" 8 + api "code.gitea.io/gitea/modules/structs" 9 + ) 10 + 11 + // ToSecret converts Secret to API format 12 + func ToSecret(secret *secret_model.Secret) *api.Secret { 13 + result := &api.Secret{ 14 + Name: secret.Name, 15 + } 16 + 17 + return result 18 + }
+72 -2
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 + } 1589 1632 } 1590 1633 }, 1591 1634 "/orgs/{org}/activities/feeds": { ··· 17443 17486 }, 17444 17487 "x-go-package": "code.gitea.io/gitea/modules/structs" 17445 17488 }, 17489 + "CreateSecretOption": { 17490 + "description": "CreateSecretOption options when creating secret", 17491 + "type": "object", 17492 + "required": [ 17493 + "name" 17494 + ], 17495 + "properties": { 17496 + "data": { 17497 + "description": "Data of the secret to create", 17498 + "type": "string", 17499 + "x-go-name": "Data" 17500 + }, 17501 + "name": { 17502 + "description": "Name of the secret to create", 17503 + "type": "string", 17504 + "uniqueItems": true, 17505 + "x-go-name": "Name" 17506 + } 17507 + }, 17508 + "x-go-package": "code.gitea.io/gitea/modules/structs" 17509 + }, 17446 17510 "CreateStatusOption": { 17447 17511 "description": "CreateStatusOption holds the information needed to create a new CommitStatus for a Commit", 17448 17512 "type": "object", ··· 21334 21398 "x-go-package": "code.gitea.io/gitea/modules/structs" 21335 21399 }, 21336 21400 "Secret": { 21337 - "description": "User represents a secret", 21401 + "description": "Secret represents a secret", 21338 21402 "type": "object", 21339 21403 "properties": { 21340 21404 "created_at": { ··· 22921 22985 "$ref": "#/definitions/SearchResults" 22922 22986 } 22923 22987 }, 22988 + "Secret": { 22989 + "description": "Secret", 22990 + "schema": { 22991 + "$ref": "#/definitions/Secret" 22992 + } 22993 + }, 22924 22994 "SecretList": { 22925 22995 "description": "SecretList", 22926 22996 "schema": { ··· 23137 23207 "parameterBodies": { 23138 23208 "description": "parameterBodies", 23139 23209 "schema": { 23140 - "$ref": "#/definitions/UpdateRepoAvatarOption" 23210 + "$ref": "#/definitions/CreateSecretOption" 23141 23211 } 23142 23212 }, 23143 23213 "redirect": {