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.

actions.go 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package actions
  4. import (
  5. "net/http"
  6. actions_model "code.gitea.io/gitea/models/actions"
  7. "code.gitea.io/gitea/models/db"
  8. "code.gitea.io/gitea/models/unit"
  9. "code.gitea.io/gitea/modules/actions"
  10. "code.gitea.io/gitea/modules/base"
  11. "code.gitea.io/gitea/modules/context"
  12. "code.gitea.io/gitea/modules/git"
  13. "code.gitea.io/gitea/modules/setting"
  14. "code.gitea.io/gitea/modules/util"
  15. "code.gitea.io/gitea/services/convert"
  16. )
  17. const (
  18. tplListActions base.TplName = "repo/actions/list"
  19. tplViewActions base.TplName = "repo/actions/view"
  20. )
  21. // MustEnableActions check if actions are enabled in settings
  22. func MustEnableActions(ctx *context.Context) {
  23. if !setting.Actions.Enabled {
  24. ctx.NotFound("MustEnableActions", nil)
  25. return
  26. }
  27. if unit.TypeActions.UnitGlobalDisabled() {
  28. ctx.NotFound("MustEnableActions", nil)
  29. return
  30. }
  31. if ctx.Repo.Repository != nil {
  32. if !ctx.Repo.CanRead(unit.TypeActions) {
  33. ctx.NotFound("MustEnableActions", nil)
  34. return
  35. }
  36. }
  37. }
  38. func List(ctx *context.Context) {
  39. ctx.Data["Title"] = ctx.Tr("actions.actions")
  40. ctx.Data["PageIsActions"] = true
  41. var workflows git.Entries
  42. if empty, err := ctx.Repo.GitRepo.IsEmpty(); err != nil {
  43. ctx.Error(http.StatusInternalServerError, err.Error())
  44. return
  45. } else if !empty {
  46. defaultBranch, err := ctx.Repo.GitRepo.GetDefaultBranch()
  47. if err != nil {
  48. ctx.Error(http.StatusInternalServerError, err.Error())
  49. return
  50. }
  51. commit, err := ctx.Repo.GitRepo.GetBranchCommit(defaultBranch)
  52. if err != nil {
  53. ctx.Error(http.StatusInternalServerError, err.Error())
  54. return
  55. }
  56. workflows, err = actions.ListWorkflows(commit)
  57. if err != nil {
  58. ctx.Error(http.StatusInternalServerError, err.Error())
  59. return
  60. }
  61. }
  62. ctx.Data["workflows"] = workflows
  63. ctx.Data["RepoLink"] = ctx.Repo.Repository.HTMLURL()
  64. page := ctx.FormInt("page")
  65. if page <= 0 {
  66. page = 1
  67. }
  68. workflow := ctx.FormString("workflow")
  69. ctx.Data["CurWorkflow"] = workflow
  70. opts := actions_model.FindRunOptions{
  71. ListOptions: db.ListOptions{
  72. Page: page,
  73. PageSize: convert.ToCorrectPageSize(ctx.FormInt("limit")),
  74. },
  75. RepoID: ctx.Repo.Repository.ID,
  76. WorkflowFileName: workflow,
  77. }
  78. // open counts
  79. opts.IsClosed = util.OptionalBoolFalse
  80. numOpenRuns, err := actions_model.CountRuns(ctx, opts)
  81. if err != nil {
  82. ctx.Error(http.StatusInternalServerError, err.Error())
  83. return
  84. }
  85. ctx.Data["NumOpenActionRuns"] = numOpenRuns
  86. // closed counts
  87. opts.IsClosed = util.OptionalBoolTrue
  88. numClosedRuns, err := actions_model.CountRuns(ctx, opts)
  89. if err != nil {
  90. ctx.Error(http.StatusInternalServerError, err.Error())
  91. return
  92. }
  93. ctx.Data["NumClosedActionRuns"] = numClosedRuns
  94. opts.IsClosed = util.OptionalBoolNone
  95. if ctx.FormString("state") == "closed" {
  96. opts.IsClosed = util.OptionalBoolTrue
  97. ctx.Data["IsShowClosed"] = true
  98. } else {
  99. opts.IsClosed = util.OptionalBoolFalse
  100. }
  101. runs, total, err := actions_model.FindRuns(ctx, opts)
  102. if err != nil {
  103. ctx.Error(http.StatusInternalServerError, err.Error())
  104. return
  105. }
  106. for _, run := range runs {
  107. run.Repo = ctx.Repo.Repository
  108. }
  109. if err := runs.LoadTriggerUser(ctx); err != nil {
  110. ctx.Error(http.StatusInternalServerError, err.Error())
  111. return
  112. }
  113. ctx.Data["Runs"] = runs
  114. pager := context.NewPagination(int(total), opts.PageSize, opts.Page, 5)
  115. pager.SetDefaultParams(ctx)
  116. ctx.Data["Page"] = pager
  117. ctx.HTML(http.StatusOK, tplListActions)
  118. }