gitea源码

runners.go 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package shared
  4. import (
  5. "errors"
  6. "net/http"
  7. actions_model "code.gitea.io/gitea/models/actions"
  8. "code.gitea.io/gitea/models/db"
  9. "code.gitea.io/gitea/modules/setting"
  10. api "code.gitea.io/gitea/modules/structs"
  11. "code.gitea.io/gitea/modules/util"
  12. "code.gitea.io/gitea/routers/api/v1/utils"
  13. "code.gitea.io/gitea/services/context"
  14. "code.gitea.io/gitea/services/convert"
  15. )
  16. // RegistrationToken is response related to registration token
  17. // swagger:response RegistrationToken
  18. type RegistrationToken struct {
  19. Token string `json:"token"`
  20. }
  21. func GetRegistrationToken(ctx *context.APIContext, ownerID, repoID int64) {
  22. token, err := actions_model.GetLatestRunnerToken(ctx, ownerID, repoID)
  23. if errors.Is(err, util.ErrNotExist) || (token != nil && !token.IsActive) {
  24. token, err = actions_model.NewRunnerToken(ctx, ownerID, repoID)
  25. }
  26. if err != nil {
  27. ctx.APIErrorInternal(err)
  28. return
  29. }
  30. ctx.JSON(http.StatusOK, RegistrationToken{Token: token.Token})
  31. }
  32. // ListRunners lists runners for api route validated ownerID and repoID
  33. // ownerID == 0 and repoID == 0 means all runners including global runners, does not appear in sql where clause
  34. // ownerID == 0 and repoID != 0 means all runners for the given repo
  35. // ownerID != 0 and repoID == 0 means all runners for the given user/org
  36. // ownerID != 0 and repoID != 0 undefined behavior
  37. // Access rights are checked at the API route level
  38. func ListRunners(ctx *context.APIContext, ownerID, repoID int64) {
  39. if ownerID != 0 && repoID != 0 {
  40. setting.PanicInDevOrTesting("ownerID and repoID should not be both set")
  41. }
  42. runners, total, err := db.FindAndCount[actions_model.ActionRunner](ctx, &actions_model.FindRunnerOptions{
  43. OwnerID: ownerID,
  44. RepoID: repoID,
  45. ListOptions: utils.GetListOptions(ctx),
  46. })
  47. if err != nil {
  48. ctx.APIErrorInternal(err)
  49. return
  50. }
  51. res := new(api.ActionRunnersResponse)
  52. res.TotalCount = total
  53. res.Entries = make([]*api.ActionRunner, len(runners))
  54. for i, runner := range runners {
  55. res.Entries[i] = convert.ToActionRunner(ctx, runner)
  56. }
  57. ctx.JSON(http.StatusOK, &res)
  58. }
  59. func getRunnerByID(ctx *context.APIContext, ownerID, repoID, runnerID int64) (*actions_model.ActionRunner, bool) {
  60. if ownerID != 0 && repoID != 0 {
  61. setting.PanicInDevOrTesting("ownerID and repoID should not be both set")
  62. }
  63. runner, err := actions_model.GetRunnerByID(ctx, runnerID)
  64. if err != nil {
  65. if errors.Is(err, util.ErrNotExist) {
  66. ctx.APIErrorNotFound("Runner not found")
  67. } else {
  68. ctx.APIErrorInternal(err)
  69. }
  70. return nil, false
  71. }
  72. if !runner.EditableInContext(ownerID, repoID) {
  73. ctx.APIErrorNotFound("No permission to access this runner")
  74. return nil, false
  75. }
  76. return runner, true
  77. }
  78. // GetRunner get the runner for api route validated ownerID and repoID
  79. // ownerID == 0 and repoID == 0 means any runner including global runners
  80. // ownerID == 0 and repoID != 0 means any runner for the given repo
  81. // ownerID != 0 and repoID == 0 means any runner for the given user/org
  82. // ownerID != 0 and repoID != 0 undefined behavior
  83. // Access rights are checked at the API route level
  84. func GetRunner(ctx *context.APIContext, ownerID, repoID, runnerID int64) {
  85. if ownerID != 0 && repoID != 0 {
  86. setting.PanicInDevOrTesting("ownerID and repoID should not be both set")
  87. }
  88. runner, ok := getRunnerByID(ctx, ownerID, repoID, runnerID)
  89. if !ok {
  90. return
  91. }
  92. ctx.JSON(http.StatusOK, convert.ToActionRunner(ctx, runner))
  93. }
  94. // DeleteRunner deletes the runner for api route validated ownerID and repoID
  95. // ownerID == 0 and repoID == 0 means any runner including global runners
  96. // ownerID == 0 and repoID != 0 means any runner for the given repo
  97. // ownerID != 0 and repoID == 0 means any runner for the given user/org
  98. // ownerID != 0 and repoID != 0 undefined behavior
  99. // Access rights are checked at the API route level
  100. func DeleteRunner(ctx *context.APIContext, ownerID, repoID, runnerID int64) {
  101. runner, ok := getRunnerByID(ctx, ownerID, repoID, runnerID)
  102. if !ok {
  103. return
  104. }
  105. err := actions_model.DeleteRunner(ctx, runner.ID)
  106. if err != nil {
  107. ctx.APIErrorInternal(err)
  108. return
  109. }
  110. ctx.Status(http.StatusNoContent)
  111. }