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.

variables.go 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package org
  4. import (
  5. "errors"
  6. "net/http"
  7. actions_model "code.gitea.io/gitea/models/actions"
  8. "code.gitea.io/gitea/models/db"
  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. actions_service "code.gitea.io/gitea/services/actions"
  14. "code.gitea.io/gitea/services/context"
  15. )
  16. // ListVariables list org-level variables
  17. func ListVariables(ctx *context.APIContext) {
  18. // swagger:operation GET /orgs/{org}/actions/variables organization getOrgVariablesList
  19. // ---
  20. // summary: Get an org-level variables list
  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/VariableList"
  40. // "400":
  41. // "$ref": "#/responses/error"
  42. // "404":
  43. // "$ref": "#/responses/notFound"
  44. vars, count, err := db.FindAndCount[actions_model.ActionVariable](ctx, &actions_model.FindVariablesOpts{
  45. OwnerID: ctx.Org.Organization.ID,
  46. ListOptions: utils.GetListOptions(ctx),
  47. })
  48. if err != nil {
  49. ctx.Error(http.StatusInternalServerError, "FindVariables", err)
  50. return
  51. }
  52. variables := make([]*api.ActionVariable, len(vars))
  53. for i, v := range vars {
  54. variables[i] = &api.ActionVariable{
  55. OwnerID: v.OwnerID,
  56. RepoID: v.RepoID,
  57. Name: v.Name,
  58. Data: v.Data,
  59. }
  60. }
  61. ctx.SetTotalCountHeader(count)
  62. ctx.JSON(http.StatusOK, variables)
  63. }
  64. // GetVariable get an org-level variable
  65. func GetVariable(ctx *context.APIContext) {
  66. // swagger:operation GET /orgs/{org}/actions/variables/{variablename} organization getOrgVariable
  67. // ---
  68. // summary: Get an org-level variable
  69. // produces:
  70. // - application/json
  71. // parameters:
  72. // - name: org
  73. // in: path
  74. // description: name of the organization
  75. // type: string
  76. // required: true
  77. // - name: variablename
  78. // in: path
  79. // description: name of the variable
  80. // type: string
  81. // required: true
  82. // responses:
  83. // "200":
  84. // "$ref": "#/responses/ActionVariable"
  85. // "400":
  86. // "$ref": "#/responses/error"
  87. // "404":
  88. // "$ref": "#/responses/notFound"
  89. v, err := actions_service.GetVariable(ctx, actions_model.FindVariablesOpts{
  90. OwnerID: ctx.Org.Organization.ID,
  91. Name: ctx.Params("variablename"),
  92. })
  93. if err != nil {
  94. if errors.Is(err, util.ErrNotExist) {
  95. ctx.Error(http.StatusNotFound, "GetVariable", err)
  96. } else {
  97. ctx.Error(http.StatusInternalServerError, "GetVariable", err)
  98. }
  99. return
  100. }
  101. variable := &api.ActionVariable{
  102. OwnerID: v.OwnerID,
  103. RepoID: v.RepoID,
  104. Name: v.Name,
  105. Data: v.Data,
  106. }
  107. ctx.JSON(http.StatusOK, variable)
  108. }
  109. // DeleteVariable delete an org-level variable
  110. func DeleteVariable(ctx *context.APIContext) {
  111. // swagger:operation DELETE /orgs/{org}/actions/variables/{variablename} organization deleteOrgVariable
  112. // ---
  113. // summary: Delete an org-level variable
  114. // produces:
  115. // - application/json
  116. // parameters:
  117. // - name: org
  118. // in: path
  119. // description: name of the organization
  120. // type: string
  121. // required: true
  122. // - name: variablename
  123. // in: path
  124. // description: name of the variable
  125. // type: string
  126. // required: true
  127. // responses:
  128. // "200":
  129. // "$ref": "#/responses/ActionVariable"
  130. // "201":
  131. // description: response when deleting a variable
  132. // "204":
  133. // description: response when deleting a variable
  134. // "400":
  135. // "$ref": "#/responses/error"
  136. // "404":
  137. // "$ref": "#/responses/notFound"
  138. if err := actions_service.DeleteVariableByName(ctx, ctx.Org.Organization.ID, 0, ctx.Params("variablename")); err != nil {
  139. if errors.Is(err, util.ErrInvalidArgument) {
  140. ctx.Error(http.StatusBadRequest, "DeleteVariableByName", err)
  141. } else if errors.Is(err, util.ErrNotExist) {
  142. ctx.Error(http.StatusNotFound, "DeleteVariableByName", err)
  143. } else {
  144. ctx.Error(http.StatusInternalServerError, "DeleteVariableByName", err)
  145. }
  146. return
  147. }
  148. ctx.Status(http.StatusNoContent)
  149. }
  150. // CreateVariable create an org-level variable
  151. func CreateVariable(ctx *context.APIContext) {
  152. // swagger:operation POST /orgs/{org}/actions/variables/{variablename} organization createOrgVariable
  153. // ---
  154. // summary: Create an org-level variable
  155. // consumes:
  156. // - application/json
  157. // produces:
  158. // - application/json
  159. // parameters:
  160. // - name: org
  161. // in: path
  162. // description: name of the organization
  163. // type: string
  164. // required: true
  165. // - name: variablename
  166. // in: path
  167. // description: name of the variable
  168. // type: string
  169. // required: true
  170. // - name: body
  171. // in: body
  172. // schema:
  173. // "$ref": "#/definitions/CreateVariableOption"
  174. // responses:
  175. // "201":
  176. // description: response when creating an org-level variable
  177. // "204":
  178. // description: response when creating an org-level variable
  179. // "400":
  180. // "$ref": "#/responses/error"
  181. // "404":
  182. // "$ref": "#/responses/notFound"
  183. opt := web.GetForm(ctx).(*api.CreateVariableOption)
  184. ownerID := ctx.Org.Organization.ID
  185. variableName := ctx.Params("variablename")
  186. v, err := actions_service.GetVariable(ctx, actions_model.FindVariablesOpts{
  187. OwnerID: ownerID,
  188. Name: variableName,
  189. })
  190. if err != nil && !errors.Is(err, util.ErrNotExist) {
  191. ctx.Error(http.StatusInternalServerError, "GetVariable", err)
  192. return
  193. }
  194. if v != nil && v.ID > 0 {
  195. ctx.Error(http.StatusConflict, "VariableNameAlreadyExists", util.NewAlreadyExistErrorf("variable name %s already exists", variableName))
  196. return
  197. }
  198. if _, err := actions_service.CreateVariable(ctx, ownerID, 0, variableName, opt.Value); err != nil {
  199. if errors.Is(err, util.ErrInvalidArgument) {
  200. ctx.Error(http.StatusBadRequest, "CreateVariable", err)
  201. } else {
  202. ctx.Error(http.StatusInternalServerError, "CreateVariable", err)
  203. }
  204. return
  205. }
  206. ctx.Status(http.StatusNoContent)
  207. }
  208. // UpdateVariable update an org-level variable
  209. func UpdateVariable(ctx *context.APIContext) {
  210. // swagger:operation PUT /orgs/{org}/actions/variables/{variablename} organization updateOrgVariable
  211. // ---
  212. // summary: Update an org-level variable
  213. // consumes:
  214. // - application/json
  215. // produces:
  216. // - application/json
  217. // parameters:
  218. // - name: org
  219. // in: path
  220. // description: name of the organization
  221. // type: string
  222. // required: true
  223. // - name: variablename
  224. // in: path
  225. // description: name of the variable
  226. // type: string
  227. // required: true
  228. // - name: body
  229. // in: body
  230. // schema:
  231. // "$ref": "#/definitions/UpdateVariableOption"
  232. // responses:
  233. // "201":
  234. // description: response when updating an org-level variable
  235. // "204":
  236. // description: response when updating an org-level variable
  237. // "400":
  238. // "$ref": "#/responses/error"
  239. // "404":
  240. // "$ref": "#/responses/notFound"
  241. opt := web.GetForm(ctx).(*api.UpdateVariableOption)
  242. v, err := actions_service.GetVariable(ctx, actions_model.FindVariablesOpts{
  243. OwnerID: ctx.Org.Organization.ID,
  244. Name: ctx.Params("variablename"),
  245. })
  246. if err != nil {
  247. if errors.Is(err, util.ErrNotExist) {
  248. ctx.Error(http.StatusNotFound, "GetVariable", err)
  249. } else {
  250. ctx.Error(http.StatusInternalServerError, "GetVariable", err)
  251. }
  252. return
  253. }
  254. if opt.Name == "" {
  255. opt.Name = ctx.Params("variablename")
  256. }
  257. if _, err := actions_service.UpdateVariable(ctx, v.ID, opt.Name, opt.Value); err != nil {
  258. if errors.Is(err, util.ErrInvalidArgument) {
  259. ctx.Error(http.StatusBadRequest, "UpdateVariable", err)
  260. } else {
  261. ctx.Error(http.StatusInternalServerError, "UpdateVariable", err)
  262. }
  263. return
  264. }
  265. ctx.Status(http.StatusNoContent)
  266. }