gitea源码

action.go 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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/shared"
  13. "code.gitea.io/gitea/routers/api/v1/utils"
  14. actions_service "code.gitea.io/gitea/services/actions"
  15. "code.gitea.io/gitea/services/context"
  16. secret_service "code.gitea.io/gitea/services/secrets"
  17. )
  18. // create or update one secret of the user scope
  19. func CreateOrUpdateSecret(ctx *context.APIContext) {
  20. // swagger:operation PUT /user/actions/secrets/{secretname} user updateUserSecret
  21. // ---
  22. // summary: Create or Update a secret value in a user scope
  23. // consumes:
  24. // - application/json
  25. // produces:
  26. // - application/json
  27. // parameters:
  28. // - name: secretname
  29. // in: path
  30. // description: name of the secret
  31. // type: string
  32. // required: true
  33. // - name: body
  34. // in: body
  35. // schema:
  36. // "$ref": "#/definitions/CreateOrUpdateSecretOption"
  37. // responses:
  38. // "201":
  39. // description: response when creating a secret
  40. // "204":
  41. // description: response when updating a secret
  42. // "400":
  43. // "$ref": "#/responses/error"
  44. // "404":
  45. // "$ref": "#/responses/notFound"
  46. opt := web.GetForm(ctx).(*api.CreateOrUpdateSecretOption)
  47. _, created, err := secret_service.CreateOrUpdateSecret(ctx, ctx.Doer.ID, 0, ctx.PathParam("secretname"), opt.Data, opt.Description)
  48. if err != nil {
  49. if errors.Is(err, util.ErrInvalidArgument) {
  50. ctx.APIError(http.StatusBadRequest, err)
  51. } else if errors.Is(err, util.ErrNotExist) {
  52. ctx.APIError(http.StatusNotFound, err)
  53. } else {
  54. ctx.APIErrorInternal(err)
  55. }
  56. return
  57. }
  58. if created {
  59. ctx.Status(http.StatusCreated)
  60. } else {
  61. ctx.Status(http.StatusNoContent)
  62. }
  63. }
  64. // DeleteSecret delete one secret of the user scope
  65. func DeleteSecret(ctx *context.APIContext) {
  66. // swagger:operation DELETE /user/actions/secrets/{secretname} user deleteUserSecret
  67. // ---
  68. // summary: Delete a secret in a user scope
  69. // consumes:
  70. // - application/json
  71. // produces:
  72. // - application/json
  73. // parameters:
  74. // - name: secretname
  75. // in: path
  76. // description: name of the secret
  77. // type: string
  78. // required: true
  79. // responses:
  80. // "204":
  81. // description: delete one secret of the user
  82. // "400":
  83. // "$ref": "#/responses/error"
  84. // "404":
  85. // "$ref": "#/responses/notFound"
  86. err := secret_service.DeleteSecretByName(ctx, ctx.Doer.ID, 0, ctx.PathParam("secretname"))
  87. if err != nil {
  88. if errors.Is(err, util.ErrInvalidArgument) {
  89. ctx.APIError(http.StatusBadRequest, err)
  90. } else if errors.Is(err, util.ErrNotExist) {
  91. ctx.APIError(http.StatusNotFound, err)
  92. } else {
  93. ctx.APIErrorInternal(err)
  94. }
  95. return
  96. }
  97. ctx.Status(http.StatusNoContent)
  98. }
  99. // CreateVariable create a user-level variable
  100. func CreateVariable(ctx *context.APIContext) {
  101. // swagger:operation POST /user/actions/variables/{variablename} user createUserVariable
  102. // ---
  103. // summary: Create a user-level variable
  104. // consumes:
  105. // - application/json
  106. // produces:
  107. // - application/json
  108. // parameters:
  109. // - name: variablename
  110. // in: path
  111. // description: name of the variable
  112. // type: string
  113. // required: true
  114. // - name: body
  115. // in: body
  116. // schema:
  117. // "$ref": "#/definitions/CreateVariableOption"
  118. // responses:
  119. // "201":
  120. // description: successfully created the user-level variable
  121. // "400":
  122. // "$ref": "#/responses/error"
  123. // "409":
  124. // description: variable name already exists.
  125. opt := web.GetForm(ctx).(*api.CreateVariableOption)
  126. ownerID := ctx.Doer.ID
  127. variableName := ctx.PathParam("variablename")
  128. v, err := actions_service.GetVariable(ctx, actions_model.FindVariablesOpts{
  129. OwnerID: ownerID,
  130. Name: variableName,
  131. })
  132. if err != nil && !errors.Is(err, util.ErrNotExist) {
  133. ctx.APIErrorInternal(err)
  134. return
  135. }
  136. if v != nil && v.ID > 0 {
  137. ctx.APIError(http.StatusConflict, util.NewAlreadyExistErrorf("variable name %s already exists", variableName))
  138. return
  139. }
  140. if _, err := actions_service.CreateVariable(ctx, ownerID, 0, variableName, opt.Value, opt.Description); err != nil {
  141. if errors.Is(err, util.ErrInvalidArgument) {
  142. ctx.APIError(http.StatusBadRequest, err)
  143. } else {
  144. ctx.APIErrorInternal(err)
  145. }
  146. return
  147. }
  148. ctx.Status(http.StatusCreated)
  149. }
  150. // UpdateVariable update a user-level variable which is created by current doer
  151. func UpdateVariable(ctx *context.APIContext) {
  152. // swagger:operation PUT /user/actions/variables/{variablename} user updateUserVariable
  153. // ---
  154. // summary: Update a user-level variable which is created by current doer
  155. // consumes:
  156. // - application/json
  157. // produces:
  158. // - application/json
  159. // parameters:
  160. // - name: variablename
  161. // in: path
  162. // description: name of the variable
  163. // type: string
  164. // required: true
  165. // - name: body
  166. // in: body
  167. // schema:
  168. // "$ref": "#/definitions/UpdateVariableOption"
  169. // responses:
  170. // "201":
  171. // description: response when updating a variable
  172. // "204":
  173. // description: response when updating a variable
  174. // "400":
  175. // "$ref": "#/responses/error"
  176. // "404":
  177. // "$ref": "#/responses/notFound"
  178. opt := web.GetForm(ctx).(*api.UpdateVariableOption)
  179. v, err := actions_service.GetVariable(ctx, actions_model.FindVariablesOpts{
  180. OwnerID: ctx.Doer.ID,
  181. Name: ctx.PathParam("variablename"),
  182. })
  183. if err != nil {
  184. if errors.Is(err, util.ErrNotExist) {
  185. ctx.APIError(http.StatusNotFound, err)
  186. } else {
  187. ctx.APIErrorInternal(err)
  188. }
  189. return
  190. }
  191. if opt.Name == "" {
  192. opt.Name = ctx.PathParam("variablename")
  193. }
  194. v.Name = opt.Name
  195. v.Data = opt.Value
  196. v.Description = opt.Description
  197. if _, err := actions_service.UpdateVariableNameData(ctx, v); err != nil {
  198. if errors.Is(err, util.ErrInvalidArgument) {
  199. ctx.APIError(http.StatusBadRequest, err)
  200. } else {
  201. ctx.APIErrorInternal(err)
  202. }
  203. return
  204. }
  205. ctx.Status(http.StatusNoContent)
  206. }
  207. // DeleteVariable delete a user-level variable which is created by current doer
  208. func DeleteVariable(ctx *context.APIContext) {
  209. // swagger:operation DELETE /user/actions/variables/{variablename} user deleteUserVariable
  210. // ---
  211. // summary: Delete a user-level variable which is created by current doer
  212. // produces:
  213. // - application/json
  214. // parameters:
  215. // - name: variablename
  216. // in: path
  217. // description: name of the variable
  218. // type: string
  219. // required: true
  220. // responses:
  221. // "201":
  222. // description: response when deleting a variable
  223. // "204":
  224. // description: response when deleting a variable
  225. // "400":
  226. // "$ref": "#/responses/error"
  227. // "404":
  228. // "$ref": "#/responses/notFound"
  229. if err := actions_service.DeleteVariableByName(ctx, ctx.Doer.ID, 0, ctx.PathParam("variablename")); err != nil {
  230. if errors.Is(err, util.ErrInvalidArgument) {
  231. ctx.APIError(http.StatusBadRequest, err)
  232. } else if errors.Is(err, util.ErrNotExist) {
  233. ctx.APIError(http.StatusNotFound, err)
  234. } else {
  235. ctx.APIErrorInternal(err)
  236. }
  237. return
  238. }
  239. ctx.Status(http.StatusNoContent)
  240. }
  241. // GetVariable get a user-level variable which is created by current doer
  242. func GetVariable(ctx *context.APIContext) {
  243. // swagger:operation GET /user/actions/variables/{variablename} user getUserVariable
  244. // ---
  245. // summary: Get a user-level variable which is created by current doer
  246. // produces:
  247. // - application/json
  248. // parameters:
  249. // - name: variablename
  250. // in: path
  251. // description: name of the variable
  252. // type: string
  253. // required: true
  254. // responses:
  255. // "200":
  256. // "$ref": "#/responses/ActionVariable"
  257. // "400":
  258. // "$ref": "#/responses/error"
  259. // "404":
  260. // "$ref": "#/responses/notFound"
  261. v, err := actions_service.GetVariable(ctx, actions_model.FindVariablesOpts{
  262. OwnerID: ctx.Doer.ID,
  263. Name: ctx.PathParam("variablename"),
  264. })
  265. if err != nil {
  266. if errors.Is(err, util.ErrNotExist) {
  267. ctx.APIError(http.StatusNotFound, err)
  268. } else {
  269. ctx.APIErrorInternal(err)
  270. }
  271. return
  272. }
  273. variable := &api.ActionVariable{
  274. OwnerID: v.OwnerID,
  275. RepoID: v.RepoID,
  276. Name: v.Name,
  277. Data: v.Data,
  278. Description: v.Description,
  279. }
  280. ctx.JSON(http.StatusOK, variable)
  281. }
  282. // ListVariables list user-level variables
  283. func ListVariables(ctx *context.APIContext) {
  284. // swagger:operation GET /user/actions/variables user getUserVariablesList
  285. // ---
  286. // summary: Get the user-level list of variables which is created by current doer
  287. // produces:
  288. // - application/json
  289. // parameters:
  290. // - name: page
  291. // in: query
  292. // description: page number of results to return (1-based)
  293. // type: integer
  294. // - name: limit
  295. // in: query
  296. // description: page size of results
  297. // type: integer
  298. // responses:
  299. // "200":
  300. // "$ref": "#/responses/VariableList"
  301. // "400":
  302. // "$ref": "#/responses/error"
  303. // "404":
  304. // "$ref": "#/responses/notFound"
  305. vars, count, err := db.FindAndCount[actions_model.ActionVariable](ctx, &actions_model.FindVariablesOpts{
  306. OwnerID: ctx.Doer.ID,
  307. ListOptions: utils.GetListOptions(ctx),
  308. })
  309. if err != nil {
  310. ctx.APIErrorInternal(err)
  311. return
  312. }
  313. variables := make([]*api.ActionVariable, len(vars))
  314. for i, v := range vars {
  315. variables[i] = &api.ActionVariable{
  316. OwnerID: v.OwnerID,
  317. RepoID: v.RepoID,
  318. Name: v.Name,
  319. Data: v.Data,
  320. Description: v.Description,
  321. }
  322. }
  323. ctx.SetTotalCountHeader(count)
  324. ctx.JSON(http.StatusOK, variables)
  325. }
  326. // ListWorkflowRuns lists workflow runs
  327. func ListWorkflowRuns(ctx *context.APIContext) {
  328. // swagger:operation GET /user/actions/runs user getUserWorkflowRuns
  329. // ---
  330. // summary: Get workflow runs
  331. // parameters:
  332. // - name: event
  333. // in: query
  334. // description: workflow event name
  335. // type: string
  336. // required: false
  337. // - name: branch
  338. // in: query
  339. // description: workflow branch
  340. // type: string
  341. // required: false
  342. // - name: status
  343. // in: query
  344. // description: workflow status (pending, queued, in_progress, failure, success, skipped)
  345. // type: string
  346. // required: false
  347. // - name: actor
  348. // in: query
  349. // description: triggered by user
  350. // type: string
  351. // required: false
  352. // - name: head_sha
  353. // in: query
  354. // description: triggering sha of the workflow run
  355. // type: string
  356. // required: false
  357. // - name: page
  358. // in: query
  359. // description: page number of results to return (1-based)
  360. // type: integer
  361. // - name: limit
  362. // in: query
  363. // description: page size of results
  364. // type: integer
  365. // produces:
  366. // - application/json
  367. // responses:
  368. // "200":
  369. // "$ref": "#/responses/WorkflowRunsList"
  370. // "400":
  371. // "$ref": "#/responses/error"
  372. // "404":
  373. // "$ref": "#/responses/notFound"
  374. shared.ListRuns(ctx, ctx.Doer.ID, 0)
  375. }
  376. // ListWorkflowJobs lists workflow jobs
  377. func ListWorkflowJobs(ctx *context.APIContext) {
  378. // swagger:operation GET /user/actions/jobs user getUserWorkflowJobs
  379. // ---
  380. // summary: Get workflow jobs
  381. // parameters:
  382. // - name: status
  383. // in: query
  384. // description: workflow status (pending, queued, in_progress, failure, success, skipped)
  385. // type: string
  386. // required: false
  387. // - name: page
  388. // in: query
  389. // description: page number of results to return (1-based)
  390. // type: integer
  391. // - name: limit
  392. // in: query
  393. // description: page size of results
  394. // type: integer
  395. // produces:
  396. // - application/json
  397. // responses:
  398. // "200":
  399. // "$ref": "#/responses/WorkflowJobsList"
  400. // "400":
  401. // "$ref": "#/responses/error"
  402. // "404":
  403. // "$ref": "#/responses/notFound"
  404. shared.ListJobs(ctx, ctx.Doer.ID, 0, 0)
  405. }