You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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