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.

action.go 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package user
  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. secret_service "code.gitea.io/gitea/services/secrets"
  16. )
  17. // create or update one secret of the user scope
  18. func CreateOrUpdateSecret(ctx *context.APIContext) {
  19. // swagger:operation PUT /user/actions/secrets/{secretname} user updateUserSecret
  20. // ---
  21. // summary: Create or Update a secret value in a user scope
  22. // consumes:
  23. // - application/json
  24. // produces:
  25. // - application/json
  26. // parameters:
  27. // - name: secretname
  28. // in: path
  29. // description: name of the secret
  30. // type: string
  31. // required: true
  32. // - name: body
  33. // in: body
  34. // schema:
  35. // "$ref": "#/definitions/CreateOrUpdateSecretOption"
  36. // responses:
  37. // "201":
  38. // description: response when creating a secret
  39. // "204":
  40. // description: response when updating a secret
  41. // "400":
  42. // "$ref": "#/responses/error"
  43. // "404":
  44. // "$ref": "#/responses/notFound"
  45. opt := web.GetForm(ctx).(*api.CreateOrUpdateSecretOption)
  46. _, created, err := secret_service.CreateOrUpdateSecret(ctx, ctx.Doer.ID, 0, ctx.Params("secretname"), opt.Data)
  47. if err != nil {
  48. if errors.Is(err, util.ErrInvalidArgument) {
  49. ctx.Error(http.StatusBadRequest, "CreateOrUpdateSecret", err)
  50. } else if errors.Is(err, util.ErrNotExist) {
  51. ctx.Error(http.StatusNotFound, "CreateOrUpdateSecret", err)
  52. } else {
  53. ctx.Error(http.StatusInternalServerError, "CreateOrUpdateSecret", err)
  54. }
  55. return
  56. }
  57. if created {
  58. ctx.Status(http.StatusCreated)
  59. } else {
  60. ctx.Status(http.StatusNoContent)
  61. }
  62. }
  63. // DeleteSecret delete one secret of the user scope
  64. func DeleteSecret(ctx *context.APIContext) {
  65. // swagger:operation DELETE /user/actions/secrets/{secretname} user deleteUserSecret
  66. // ---
  67. // summary: Delete a secret in a user scope
  68. // consumes:
  69. // - application/json
  70. // produces:
  71. // - application/json
  72. // parameters:
  73. // - name: secretname
  74. // in: path
  75. // description: name of the secret
  76. // type: string
  77. // required: true
  78. // responses:
  79. // "204":
  80. // description: delete one secret of the user
  81. // "400":
  82. // "$ref": "#/responses/error"
  83. // "404":
  84. // "$ref": "#/responses/notFound"
  85. err := secret_service.DeleteSecretByName(ctx, ctx.Doer.ID, 0, ctx.Params("secretname"))
  86. if err != nil {
  87. if errors.Is(err, util.ErrInvalidArgument) {
  88. ctx.Error(http.StatusBadRequest, "DeleteSecret", err)
  89. } else if errors.Is(err, util.ErrNotExist) {
  90. ctx.Error(http.StatusNotFound, "DeleteSecret", err)
  91. } else {
  92. ctx.Error(http.StatusInternalServerError, "DeleteSecret", err)
  93. }
  94. return
  95. }
  96. ctx.Status(http.StatusNoContent)
  97. }
  98. // CreateVariable create a user-level variable
  99. func CreateVariable(ctx *context.APIContext) {
  100. // swagger:operation POST /user/actions/variables/{variablename} user createUserVariable
  101. // ---
  102. // summary: Create a user-level variable
  103. // consumes:
  104. // - application/json
  105. // produces:
  106. // - application/json
  107. // parameters:
  108. // - name: variablename
  109. // in: path
  110. // description: name of the variable
  111. // type: string
  112. // required: true
  113. // - name: body
  114. // in: body
  115. // schema:
  116. // "$ref": "#/definitions/CreateVariableOption"
  117. // responses:
  118. // "201":
  119. // description: response when creating a variable
  120. // "204":
  121. // description: response when creating a variable
  122. // "400":
  123. // "$ref": "#/responses/error"
  124. // "404":
  125. // "$ref": "#/responses/notFound"
  126. opt := web.GetForm(ctx).(*api.CreateVariableOption)
  127. ownerID := ctx.Doer.ID
  128. variableName := ctx.Params("variablename")
  129. v, err := actions_service.GetVariable(ctx, actions_model.FindVariablesOpts{
  130. OwnerID: ownerID,
  131. Name: variableName,
  132. })
  133. if err != nil && !errors.Is(err, util.ErrNotExist) {
  134. ctx.Error(http.StatusInternalServerError, "GetVariable", err)
  135. return
  136. }
  137. if v != nil && v.ID > 0 {
  138. ctx.Error(http.StatusConflict, "VariableNameAlreadyExists", util.NewAlreadyExistErrorf("variable name %s already exists", variableName))
  139. return
  140. }
  141. if _, err := actions_service.CreateVariable(ctx, ownerID, 0, variableName, opt.Value); err != nil {
  142. if errors.Is(err, util.ErrInvalidArgument) {
  143. ctx.Error(http.StatusBadRequest, "CreateVariable", err)
  144. } else {
  145. ctx.Error(http.StatusInternalServerError, "CreateVariable", err)
  146. }
  147. return
  148. }
  149. ctx.Status(http.StatusNoContent)
  150. }
  151. // UpdateVariable update a user-level variable which is created by current doer
  152. func UpdateVariable(ctx *context.APIContext) {
  153. // swagger:operation PUT /user/actions/variables/{variablename} user updateUserVariable
  154. // ---
  155. // summary: Update a user-level variable which is created by current doer
  156. // consumes:
  157. // - application/json
  158. // produces:
  159. // - application/json
  160. // parameters:
  161. // - name: variablename
  162. // in: path
  163. // description: name of the variable
  164. // type: string
  165. // required: true
  166. // - name: body
  167. // in: body
  168. // schema:
  169. // "$ref": "#/definitions/UpdateVariableOption"
  170. // responses:
  171. // "201":
  172. // description: response when updating a variable
  173. // "204":
  174. // description: response when updating a variable
  175. // "400":
  176. // "$ref": "#/responses/error"
  177. // "404":
  178. // "$ref": "#/responses/notFound"
  179. opt := web.GetForm(ctx).(*api.UpdateVariableOption)
  180. v, err := actions_service.GetVariable(ctx, actions_model.FindVariablesOpts{
  181. OwnerID: ctx.Doer.ID,
  182. Name: ctx.Params("variablename"),
  183. })
  184. if err != nil {
  185. if errors.Is(err, util.ErrNotExist) {
  186. ctx.Error(http.StatusNotFound, "GetVariable", err)
  187. } else {
  188. ctx.Error(http.StatusInternalServerError, "GetVariable", err)
  189. }
  190. return
  191. }
  192. if opt.Name == "" {
  193. opt.Name = ctx.Params("variablename")
  194. }
  195. if _, err := actions_service.UpdateVariable(ctx, v.ID, opt.Name, opt.Value); err != nil {
  196. if errors.Is(err, util.ErrInvalidArgument) {
  197. ctx.Error(http.StatusBadRequest, "UpdateVariable", err)
  198. } else {
  199. ctx.Error(http.StatusInternalServerError, "UpdateVariable", err)
  200. }
  201. return
  202. }
  203. ctx.Status(http.StatusNoContent)
  204. }
  205. // DeleteVariable delete a user-level variable which is created by current doer
  206. func DeleteVariable(ctx *context.APIContext) {
  207. // swagger:operation DELETE /user/actions/variables/{variablename} user deleteUserVariable
  208. // ---
  209. // summary: Delete a user-level variable which is created by current doer
  210. // produces:
  211. // - application/json
  212. // parameters:
  213. // - name: variablename
  214. // in: path
  215. // description: name of the variable
  216. // type: string
  217. // required: true
  218. // responses:
  219. // "201":
  220. // description: response when deleting a variable
  221. // "204":
  222. // description: response when deleting a variable
  223. // "400":
  224. // "$ref": "#/responses/error"
  225. // "404":
  226. // "$ref": "#/responses/notFound"
  227. if err := actions_service.DeleteVariableByName(ctx, ctx.Doer.ID, 0, ctx.Params("variablename")); err != nil {
  228. if errors.Is(err, util.ErrInvalidArgument) {
  229. ctx.Error(http.StatusBadRequest, "DeleteVariableByName", err)
  230. } else if errors.Is(err, util.ErrNotExist) {
  231. ctx.Error(http.StatusNotFound, "DeleteVariableByName", err)
  232. } else {
  233. ctx.Error(http.StatusInternalServerError, "DeleteVariableByName", err)
  234. }
  235. return
  236. }
  237. ctx.Status(http.StatusNoContent)
  238. }
  239. // GetVariable get a user-level variable which is created by current doer
  240. func GetVariable(ctx *context.APIContext) {
  241. // swagger:operation GET /user/actions/variables/{variablename} user getUserVariable
  242. // ---
  243. // summary: Get a user-level variable which is created by current doer
  244. // produces:
  245. // - application/json
  246. // parameters:
  247. // - name: variablename
  248. // in: path
  249. // description: name of the variable
  250. // type: string
  251. // required: true
  252. // responses:
  253. // "200":
  254. // "$ref": "#/responses/ActionVariable"
  255. // "400":
  256. // "$ref": "#/responses/error"
  257. // "404":
  258. // "$ref": "#/responses/notFound"
  259. v, err := actions_service.GetVariable(ctx, actions_model.FindVariablesOpts{
  260. OwnerID: ctx.Doer.ID,
  261. Name: ctx.Params("variablename"),
  262. })
  263. if err != nil {
  264. if errors.Is(err, util.ErrNotExist) {
  265. ctx.Error(http.StatusNotFound, "GetVariable", err)
  266. } else {
  267. ctx.Error(http.StatusInternalServerError, "GetVariable", err)
  268. }
  269. return
  270. }
  271. variable := &api.ActionVariable{
  272. OwnerID: v.OwnerID,
  273. RepoID: v.RepoID,
  274. Name: v.Name,
  275. Data: v.Data,
  276. }
  277. ctx.JSON(http.StatusOK, variable)
  278. }
  279. // ListVariables list user-level variables
  280. func ListVariables(ctx *context.APIContext) {
  281. // swagger:operation GET /user/actions/variables user getUserVariablesList
  282. // ---
  283. // summary: Get the user-level list of variables which is created by current doer
  284. // produces:
  285. // - application/json
  286. // parameters:
  287. // - name: page
  288. // in: query
  289. // description: page number of results to return (1-based)
  290. // type: integer
  291. // - name: limit
  292. // in: query
  293. // description: page size of results
  294. // type: integer
  295. // responses:
  296. // "200":
  297. // "$ref": "#/responses/VariableList"
  298. // "400":
  299. // "$ref": "#/responses/error"
  300. // "404":
  301. // "$ref": "#/responses/notFound"
  302. vars, count, err := db.FindAndCount[actions_model.ActionVariable](ctx, &actions_model.FindVariablesOpts{
  303. OwnerID: ctx.Doer.ID,
  304. ListOptions: utils.GetListOptions(ctx),
  305. })
  306. if err != nil {
  307. ctx.Error(http.StatusInternalServerError, "FindVariables", err)
  308. return
  309. }
  310. variables := make([]*api.ActionVariable, len(vars))
  311. for i, v := range vars {
  312. variables[i] = &api.ActionVariable{
  313. OwnerID: v.OwnerID,
  314. RepoID: v.RepoID,
  315. Name: v.Name,
  316. Data: v.Data,
  317. }
  318. }
  319. ctx.SetTotalCountHeader(count)
  320. ctx.JSON(http.StatusOK, variables)
  321. }