code.gitea.io/gitea@v1.21.7/routers/api/v1/org/action.go (about) 1 // Copyright 2023 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package org 5 6 import ( 7 "errors" 8 "net/http" 9 10 secret_model "code.gitea.io/gitea/models/secret" 11 "code.gitea.io/gitea/modules/context" 12 api "code.gitea.io/gitea/modules/structs" 13 "code.gitea.io/gitea/modules/util" 14 "code.gitea.io/gitea/modules/web" 15 "code.gitea.io/gitea/routers/api/v1/utils" 16 secret_service "code.gitea.io/gitea/services/secrets" 17 ) 18 19 // ListActionsSecrets list an organization's actions secrets 20 func ListActionsSecrets(ctx *context.APIContext) { 21 // swagger:operation GET /orgs/{org}/actions/secrets organization orgListActionsSecrets 22 // --- 23 // summary: List an organization's actions secrets 24 // produces: 25 // - application/json 26 // parameters: 27 // - name: org 28 // in: path 29 // description: name of the organization 30 // type: string 31 // required: true 32 // - name: page 33 // in: query 34 // description: page number of results to return (1-based) 35 // type: integer 36 // - name: limit 37 // in: query 38 // description: page size of results 39 // type: integer 40 // responses: 41 // "200": 42 // "$ref": "#/responses/SecretList" 43 // "404": 44 // "$ref": "#/responses/notFound" 45 46 opts := &secret_model.FindSecretsOptions{ 47 OwnerID: ctx.Org.Organization.ID, 48 ListOptions: utils.GetListOptions(ctx), 49 } 50 51 count, err := secret_model.CountSecrets(ctx, opts) 52 if err != nil { 53 ctx.InternalServerError(err) 54 return 55 } 56 57 secrets, err := secret_model.FindSecrets(ctx, *opts) 58 if err != nil { 59 ctx.InternalServerError(err) 60 return 61 } 62 63 apiSecrets := make([]*api.Secret, len(secrets)) 64 for k, v := range secrets { 65 apiSecrets[k] = &api.Secret{ 66 Name: v.Name, 67 Created: v.CreatedUnix.AsTime(), 68 } 69 } 70 71 ctx.SetTotalCountHeader(count) 72 ctx.JSON(http.StatusOK, apiSecrets) 73 } 74 75 // create or update one secret of the organization 76 func CreateOrUpdateSecret(ctx *context.APIContext) { 77 // swagger:operation PUT /orgs/{org}/actions/secrets/{secretname} organization updateOrgSecret 78 // --- 79 // summary: Create or Update a secret value in an organization 80 // consumes: 81 // - application/json 82 // produces: 83 // - application/json 84 // parameters: 85 // - name: org 86 // in: path 87 // description: name of organization 88 // type: string 89 // required: true 90 // - name: secretname 91 // in: path 92 // description: name of the secret 93 // type: string 94 // required: true 95 // - name: body 96 // in: body 97 // schema: 98 // "$ref": "#/definitions/CreateOrUpdateSecretOption" 99 // responses: 100 // "201": 101 // description: response when creating a secret 102 // "204": 103 // description: response when updating a secret 104 // "400": 105 // "$ref": "#/responses/error" 106 // "404": 107 // "$ref": "#/responses/notFound" 108 109 opt := web.GetForm(ctx).(*api.CreateOrUpdateSecretOption) 110 111 _, created, err := secret_service.CreateOrUpdateSecret(ctx, ctx.Org.Organization.ID, 0, ctx.Params("secretname"), opt.Data) 112 if err != nil { 113 if errors.Is(err, util.ErrInvalidArgument) { 114 ctx.Error(http.StatusBadRequest, "CreateOrUpdateSecret", err) 115 } else if errors.Is(err, util.ErrNotExist) { 116 ctx.Error(http.StatusNotFound, "CreateOrUpdateSecret", err) 117 } else { 118 ctx.Error(http.StatusInternalServerError, "CreateOrUpdateSecret", err) 119 } 120 return 121 } 122 123 if created { 124 ctx.Status(http.StatusCreated) 125 } else { 126 ctx.Status(http.StatusNoContent) 127 } 128 } 129 130 // DeleteSecret delete one secret of the organization 131 func DeleteSecret(ctx *context.APIContext) { 132 // swagger:operation DELETE /orgs/{org}/actions/secrets/{secretname} organization deleteOrgSecret 133 // --- 134 // summary: Delete a secret in an organization 135 // consumes: 136 // - application/json 137 // produces: 138 // - application/json 139 // parameters: 140 // - name: org 141 // in: path 142 // description: name of organization 143 // type: string 144 // required: true 145 // - name: secretname 146 // in: path 147 // description: name of the secret 148 // type: string 149 // required: true 150 // responses: 151 // "204": 152 // description: delete one secret of the organization 153 // "400": 154 // "$ref": "#/responses/error" 155 // "404": 156 // "$ref": "#/responses/notFound" 157 158 err := secret_service.DeleteSecretByName(ctx, ctx.Org.Organization.ID, 0, ctx.Params("secretname")) 159 if err != nil { 160 if errors.Is(err, util.ErrInvalidArgument) { 161 ctx.Error(http.StatusBadRequest, "DeleteSecret", err) 162 } else if errors.Is(err, util.ErrNotExist) { 163 ctx.Error(http.StatusNotFound, "DeleteSecret", err) 164 } else { 165 ctx.Error(http.StatusInternalServerError, "DeleteSecret", err) 166 } 167 return 168 } 169 170 ctx.Status(http.StatusNoContent) 171 }