您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. secret_model "code.gitea.io/gitea/models/secret"
  10. api "code.gitea.io/gitea/modules/structs"
  11. "code.gitea.io/gitea/modules/util"
  12. "code.gitea.io/gitea/modules/web"
  13. "code.gitea.io/gitea/routers/api/v1/shared"
  14. "code.gitea.io/gitea/routers/api/v1/utils"
  15. actions_service "code.gitea.io/gitea/services/actions"
  16. "code.gitea.io/gitea/services/context"
  17. secret_service "code.gitea.io/gitea/services/secrets"
  18. )
  19. // ListActionsSecrets list an organization's actions secrets
  20. func (Action) 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. opts := &secret_model.FindSecretsOptions{
  46. OwnerID: ctx.Org.Organization.ID,
  47. ListOptions: utils.GetListOptions(ctx),
  48. }
  49. secrets, count, err := db.FindAndCount[secret_model.Secret](ctx, opts)
  50. if err != nil {
  51. ctx.InternalServerError(err)
  52. return
  53. }
  54. apiSecrets := make([]*api.Secret, len(secrets))
  55. for k, v := range secrets {
  56. apiSecrets[k] = &api.Secret{
  57. Name: v.Name,
  58. Created: v.CreatedUnix.AsTime(),
  59. }
  60. }
  61. ctx.SetTotalCountHeader(count)
  62. ctx.JSON(http.StatusOK, apiSecrets)
  63. }
  64. // create or update one secret of the organization
  65. func (Action) CreateOrUpdateSecret(ctx *context.APIContext) {
  66. // swagger:operation PUT /orgs/{org}/actions/secrets/{secretname} organization updateOrgSecret
  67. // ---
  68. // summary: Create or Update a secret value in an organization
  69. // consumes:
  70. // - application/json
  71. // produces:
  72. // - application/json
  73. // parameters:
  74. // - name: org
  75. // in: path
  76. // description: name of organization
  77. // type: string
  78. // required: true
  79. // - name: secretname
  80. // in: path
  81. // description: name of the secret
  82. // type: string
  83. // required: true
  84. // - name: body
  85. // in: body
  86. // schema:
  87. // "$ref": "#/definitions/CreateOrUpdateSecretOption"
  88. // responses:
  89. // "201":
  90. // description: response when creating a secret
  91. // "204":
  92. // description: response when updating a secret
  93. // "400":
  94. // "$ref": "#/responses/error"
  95. // "404":
  96. // "$ref": "#/responses/notFound"
  97. opt := web.GetForm(ctx).(*api.CreateOrUpdateSecretOption)
  98. _, created, err := secret_service.CreateOrUpdateSecret(ctx, ctx.Org.Organization.ID, 0, ctx.Params("secretname"), opt.Data)
  99. if err != nil {
  100. if errors.Is(err, util.ErrInvalidArgument) {
  101. ctx.Error(http.StatusBadRequest, "CreateOrUpdateSecret", err)
  102. } else if errors.Is(err, util.ErrNotExist) {
  103. ctx.Error(http.StatusNotFound, "CreateOrUpdateSecret", err)
  104. } else {
  105. ctx.Error(http.StatusInternalServerError, "CreateOrUpdateSecret", err)
  106. }
  107. return
  108. }
  109. if created {
  110. ctx.Status(http.StatusCreated)
  111. } else {
  112. ctx.Status(http.StatusNoContent)
  113. }
  114. }
  115. // DeleteSecret delete one secret of the organization
  116. func (Action) DeleteSecret(ctx *context.APIContext) {
  117. // swagger:operation DELETE /orgs/{org}/actions/secrets/{secretname} organization deleteOrgSecret
  118. // ---
  119. // summary: Delete a secret in an organization
  120. // consumes:
  121. // - application/json
  122. // produces:
  123. // - application/json
  124. // parameters:
  125. // - name: org
  126. // in: path
  127. // description: name of organization
  128. // type: string
  129. // required: true
  130. // - name: secretname
  131. // in: path
  132. // description: name of the secret
  133. // type: string
  134. // required: true
  135. // responses:
  136. // "204":
  137. // description: delete one secret of the organization
  138. // "400":
  139. // "$ref": "#/responses/error"
  140. // "404":
  141. // "$ref": "#/responses/notFound"
  142. err := secret_service.DeleteSecretByName(ctx, ctx.Org.Organization.ID, 0, ctx.Params("secretname"))
  143. if err != nil {
  144. if errors.Is(err, util.ErrInvalidArgument) {
  145. ctx.Error(http.StatusBadRequest, "DeleteSecret", err)
  146. } else if errors.Is(err, util.ErrNotExist) {
  147. ctx.Error(http.StatusNotFound, "DeleteSecret", err)
  148. } else {
  149. ctx.Error(http.StatusInternalServerError, "DeleteSecret", err)
  150. }
  151. return
  152. }
  153. ctx.Status(http.StatusNoContent)
  154. }
  155. // https://docs.github.com/en/rest/actions/self-hosted-runners?apiVersion=2022-11-28#create-a-registration-token-for-an-organization
  156. // GetRegistrationToken returns the token to register org runners
  157. func (Action) GetRegistrationToken(ctx *context.APIContext) {
  158. // swagger:operation GET /orgs/{org}/actions/runners/registration-token organization orgGetRunnerRegistrationToken
  159. // ---
  160. // summary: Get an organization's actions runner registration token
  161. // produces:
  162. // - application/json
  163. // parameters:
  164. // - name: org
  165. // in: path
  166. // description: name of the organization
  167. // type: string
  168. // required: true
  169. // responses:
  170. // "200":
  171. // "$ref": "#/responses/RegistrationToken"
  172. shared.GetRegistrationToken(ctx, ctx.Org.Organization.ID, 0)
  173. }
  174. // ListVariables list org-level variables
  175. func (Action) ListVariables(ctx *context.APIContext) {
  176. // swagger:operation GET /orgs/{org}/actions/variables organization getOrgVariablesList
  177. // ---
  178. // summary: Get an org-level variables list
  179. // produces:
  180. // - application/json
  181. // parameters:
  182. // - name: org
  183. // in: path
  184. // description: name of the organization
  185. // type: string
  186. // required: true
  187. // - name: page
  188. // in: query
  189. // description: page number of results to return (1-based)
  190. // type: integer
  191. // - name: limit
  192. // in: query
  193. // description: page size of results
  194. // type: integer
  195. // responses:
  196. // "200":
  197. // "$ref": "#/responses/VariableList"
  198. // "400":
  199. // "$ref": "#/responses/error"
  200. // "404":
  201. // "$ref": "#/responses/notFound"
  202. vars, count, err := db.FindAndCount[actions_model.ActionVariable](ctx, &actions_model.FindVariablesOpts{
  203. OwnerID: ctx.Org.Organization.ID,
  204. ListOptions: utils.GetListOptions(ctx),
  205. })
  206. if err != nil {
  207. ctx.Error(http.StatusInternalServerError, "FindVariables", err)
  208. return
  209. }
  210. variables := make([]*api.ActionVariable, len(vars))
  211. for i, v := range vars {
  212. variables[i] = &api.ActionVariable{
  213. OwnerID: v.OwnerID,
  214. RepoID: v.RepoID,
  215. Name: v.Name,
  216. Data: v.Data,
  217. }
  218. }
  219. ctx.SetTotalCountHeader(count)
  220. ctx.JSON(http.StatusOK, variables)
  221. }
  222. // GetVariable get an org-level variable
  223. func (Action) GetVariable(ctx *context.APIContext) {
  224. // swagger:operation GET /orgs/{org}/actions/variables/{variablename} organization getOrgVariable
  225. // ---
  226. // summary: Get an org-level variable
  227. // produces:
  228. // - application/json
  229. // parameters:
  230. // - name: org
  231. // in: path
  232. // description: name of the organization
  233. // type: string
  234. // required: true
  235. // - name: variablename
  236. // in: path
  237. // description: name of the variable
  238. // type: string
  239. // required: true
  240. // responses:
  241. // "200":
  242. // "$ref": "#/responses/ActionVariable"
  243. // "400":
  244. // "$ref": "#/responses/error"
  245. // "404":
  246. // "$ref": "#/responses/notFound"
  247. v, err := actions_service.GetVariable(ctx, actions_model.FindVariablesOpts{
  248. OwnerID: ctx.Org.Organization.ID,
  249. Name: ctx.Params("variablename"),
  250. })
  251. if err != nil {
  252. if errors.Is(err, util.ErrNotExist) {
  253. ctx.Error(http.StatusNotFound, "GetVariable", err)
  254. } else {
  255. ctx.Error(http.StatusInternalServerError, "GetVariable", err)
  256. }
  257. return
  258. }
  259. variable := &api.ActionVariable{
  260. OwnerID: v.OwnerID,
  261. RepoID: v.RepoID,
  262. Name: v.Name,
  263. Data: v.Data,
  264. }
  265. ctx.JSON(http.StatusOK, variable)
  266. }
  267. // DeleteVariable delete an org-level variable
  268. func (Action) DeleteVariable(ctx *context.APIContext) {
  269. // swagger:operation DELETE /orgs/{org}/actions/variables/{variablename} organization deleteOrgVariable
  270. // ---
  271. // summary: Delete an org-level variable
  272. // produces:
  273. // - application/json
  274. // parameters:
  275. // - name: org
  276. // in: path
  277. // description: name of the organization
  278. // type: string
  279. // required: true
  280. // - name: variablename
  281. // in: path
  282. // description: name of the variable
  283. // type: string
  284. // required: true
  285. // responses:
  286. // "200":
  287. // "$ref": "#/responses/ActionVariable"
  288. // "201":
  289. // description: response when deleting a variable
  290. // "204":
  291. // description: response when deleting a variable
  292. // "400":
  293. // "$ref": "#/responses/error"
  294. // "404":
  295. // "$ref": "#/responses/notFound"
  296. if err := actions_service.DeleteVariableByName(ctx, ctx.Org.Organization.ID, 0, ctx.Params("variablename")); err != nil {
  297. if errors.Is(err, util.ErrInvalidArgument) {
  298. ctx.Error(http.StatusBadRequest, "DeleteVariableByName", err)
  299. } else if errors.Is(err, util.ErrNotExist) {
  300. ctx.Error(http.StatusNotFound, "DeleteVariableByName", err)
  301. } else {
  302. ctx.Error(http.StatusInternalServerError, "DeleteVariableByName", err)
  303. }
  304. return
  305. }
  306. ctx.Status(http.StatusNoContent)
  307. }
  308. // CreateVariable create an org-level variable
  309. func (Action) CreateVariable(ctx *context.APIContext) {
  310. // swagger:operation POST /orgs/{org}/actions/variables/{variablename} organization createOrgVariable
  311. // ---
  312. // summary: Create an org-level variable
  313. // consumes:
  314. // - application/json
  315. // produces:
  316. // - application/json
  317. // parameters:
  318. // - name: org
  319. // in: path
  320. // description: name of the organization
  321. // type: string
  322. // required: true
  323. // - name: variablename
  324. // in: path
  325. // description: name of the variable
  326. // type: string
  327. // required: true
  328. // - name: body
  329. // in: body
  330. // schema:
  331. // "$ref": "#/definitions/CreateVariableOption"
  332. // responses:
  333. // "201":
  334. // description: response when creating an org-level variable
  335. // "204":
  336. // description: response when creating an org-level variable
  337. // "400":
  338. // "$ref": "#/responses/error"
  339. // "404":
  340. // "$ref": "#/responses/notFound"
  341. opt := web.GetForm(ctx).(*api.CreateVariableOption)
  342. ownerID := ctx.Org.Organization.ID
  343. variableName := ctx.Params("variablename")
  344. v, err := actions_service.GetVariable(ctx, actions_model.FindVariablesOpts{
  345. OwnerID: ownerID,
  346. Name: variableName,
  347. })
  348. if err != nil && !errors.Is(err, util.ErrNotExist) {
  349. ctx.Error(http.StatusInternalServerError, "GetVariable", err)
  350. return
  351. }
  352. if v != nil && v.ID > 0 {
  353. ctx.Error(http.StatusConflict, "VariableNameAlreadyExists", util.NewAlreadyExistErrorf("variable name %s already exists", variableName))
  354. return
  355. }
  356. if _, err := actions_service.CreateVariable(ctx, ownerID, 0, variableName, opt.Value); err != nil {
  357. if errors.Is(err, util.ErrInvalidArgument) {
  358. ctx.Error(http.StatusBadRequest, "CreateVariable", err)
  359. } else {
  360. ctx.Error(http.StatusInternalServerError, "CreateVariable", err)
  361. }
  362. return
  363. }
  364. ctx.Status(http.StatusNoContent)
  365. }
  366. // UpdateVariable update an org-level variable
  367. func (Action) UpdateVariable(ctx *context.APIContext) {
  368. // swagger:operation PUT /orgs/{org}/actions/variables/{variablename} organization updateOrgVariable
  369. // ---
  370. // summary: Update an org-level variable
  371. // consumes:
  372. // - application/json
  373. // produces:
  374. // - application/json
  375. // parameters:
  376. // - name: org
  377. // in: path
  378. // description: name of the organization
  379. // type: string
  380. // required: true
  381. // - name: variablename
  382. // in: path
  383. // description: name of the variable
  384. // type: string
  385. // required: true
  386. // - name: body
  387. // in: body
  388. // schema:
  389. // "$ref": "#/definitions/UpdateVariableOption"
  390. // responses:
  391. // "201":
  392. // description: response when updating an org-level variable
  393. // "204":
  394. // description: response when updating an org-level variable
  395. // "400":
  396. // "$ref": "#/responses/error"
  397. // "404":
  398. // "$ref": "#/responses/notFound"
  399. opt := web.GetForm(ctx).(*api.UpdateVariableOption)
  400. v, err := actions_service.GetVariable(ctx, actions_model.FindVariablesOpts{
  401. OwnerID: ctx.Org.Organization.ID,
  402. Name: ctx.Params("variablename"),
  403. })
  404. if err != nil {
  405. if errors.Is(err, util.ErrNotExist) {
  406. ctx.Error(http.StatusNotFound, "GetVariable", err)
  407. } else {
  408. ctx.Error(http.StatusInternalServerError, "GetVariable", err)
  409. }
  410. return
  411. }
  412. if opt.Name == "" {
  413. opt.Name = ctx.Params("variablename")
  414. }
  415. if _, err := actions_service.UpdateVariable(ctx, v.ID, opt.Name, opt.Value); err != nil {
  416. if errors.Is(err, util.ErrInvalidArgument) {
  417. ctx.Error(http.StatusBadRequest, "UpdateVariable", err)
  418. } else {
  419. ctx.Error(http.StatusInternalServerError, "UpdateVariable", err)
  420. }
  421. return
  422. }
  423. ctx.Status(http.StatusNoContent)
  424. }
  425. var _ actions_service.API = new(Action)
  426. // Action implements actions_service.API
  427. type Action struct{}
  428. // NewAction creates a new Action service
  429. func NewAction() actions_service.API {
  430. return Action{}
  431. }