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 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package actions
  4. import (
  5. "bytes"
  6. "fmt"
  7. "net/http"
  8. "strings"
  9. actions_model "code.gitea.io/gitea/models/actions"
  10. "code.gitea.io/gitea/models/db"
  11. "code.gitea.io/gitea/models/unit"
  12. "code.gitea.io/gitea/modules/actions"
  13. "code.gitea.io/gitea/modules/base"
  14. "code.gitea.io/gitea/modules/container"
  15. "code.gitea.io/gitea/modules/context"
  16. "code.gitea.io/gitea/modules/git"
  17. "code.gitea.io/gitea/modules/setting"
  18. "code.gitea.io/gitea/modules/util"
  19. "code.gitea.io/gitea/routers/web/repo"
  20. "code.gitea.io/gitea/services/convert"
  21. "github.com/nektos/act/pkg/model"
  22. )
  23. const (
  24. tplListActions base.TplName = "repo/actions/list"
  25. tplViewActions base.TplName = "repo/actions/view"
  26. )
  27. type Workflow struct {
  28. Entry git.TreeEntry
  29. ErrMsg string
  30. }
  31. // MustEnableActions check if actions are enabled in settings
  32. func MustEnableActions(ctx *context.Context) {
  33. if !setting.Actions.Enabled {
  34. ctx.NotFound("MustEnableActions", nil)
  35. return
  36. }
  37. if unit.TypeActions.UnitGlobalDisabled() {
  38. ctx.NotFound("MustEnableActions", nil)
  39. return
  40. }
  41. if ctx.Repo.Repository != nil {
  42. if !ctx.Repo.CanRead(unit.TypeActions) {
  43. ctx.NotFound("MustEnableActions", nil)
  44. return
  45. }
  46. }
  47. }
  48. func List(ctx *context.Context) {
  49. ctx.Data["Title"] = ctx.Tr("actions.actions")
  50. ctx.Data["PageIsActions"] = true
  51. var workflows []Workflow
  52. if empty, err := ctx.Repo.GitRepo.IsEmpty(); err != nil {
  53. ctx.Error(http.StatusInternalServerError, err.Error())
  54. return
  55. } else if !empty {
  56. commit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
  57. if err != nil {
  58. ctx.Error(http.StatusInternalServerError, err.Error())
  59. return
  60. }
  61. entries, err := actions.ListWorkflows(commit)
  62. if err != nil {
  63. ctx.Error(http.StatusInternalServerError, err.Error())
  64. return
  65. }
  66. // Get all runner labels
  67. opts := actions_model.FindRunnerOptions{
  68. RepoID: ctx.Repo.Repository.ID,
  69. IsOnline: util.OptionalBoolTrue,
  70. WithAvailable: true,
  71. }
  72. runners, err := actions_model.FindRunners(ctx, opts)
  73. if err != nil {
  74. ctx.ServerError("FindRunners", err)
  75. return
  76. }
  77. allRunnerLabels := make(container.Set[string])
  78. for _, r := range runners {
  79. allRunnerLabels.AddMultiple(r.AgentLabels...)
  80. }
  81. workflows = make([]Workflow, 0, len(entries))
  82. for _, entry := range entries {
  83. workflow := Workflow{Entry: *entry}
  84. content, err := actions.GetContentFromEntry(entry)
  85. if err != nil {
  86. ctx.Error(http.StatusInternalServerError, err.Error())
  87. return
  88. }
  89. wf, err := model.ReadWorkflow(bytes.NewReader(content))
  90. if err != nil {
  91. workflow.ErrMsg = ctx.Locale.Tr("actions.runs.invalid_workflow_helper", err.Error())
  92. workflows = append(workflows, workflow)
  93. continue
  94. }
  95. // Check whether have matching runner
  96. for _, j := range wf.Jobs {
  97. runsOnList := j.RunsOn()
  98. for _, ro := range runsOnList {
  99. if strings.Contains(ro, "${{") {
  100. // Skip if it contains expressions.
  101. // The expressions could be very complex and could not be evaluated here,
  102. // so just skip it, it's OK since it's just a tooltip message.
  103. continue
  104. }
  105. if !allRunnerLabels.Contains(ro) {
  106. workflow.ErrMsg = ctx.Locale.Tr("actions.runs.no_matching_online_runner_helper", ro)
  107. break
  108. }
  109. }
  110. if workflow.ErrMsg != "" {
  111. break
  112. }
  113. }
  114. workflows = append(workflows, workflow)
  115. }
  116. }
  117. ctx.Data["workflows"] = workflows
  118. ctx.Data["RepoLink"] = ctx.Repo.Repository.Link()
  119. page := ctx.FormInt("page")
  120. if page <= 0 {
  121. page = 1
  122. }
  123. workflow := ctx.FormString("workflow")
  124. actorID := ctx.FormInt64("actor")
  125. status := ctx.FormInt("status")
  126. ctx.Data["CurWorkflow"] = workflow
  127. actionsConfig := ctx.Repo.Repository.MustGetUnit(ctx, unit.TypeActions).ActionsConfig()
  128. ctx.Data["ActionsConfig"] = actionsConfig
  129. if len(workflow) > 0 && ctx.Repo.IsAdmin() {
  130. ctx.Data["AllowDisableOrEnableWorkflow"] = true
  131. ctx.Data["CurWorkflowDisabled"] = actionsConfig.IsWorkflowDisabled(workflow)
  132. }
  133. // if status or actor query param is not given to frontend href, (href="/<repoLink>/actions")
  134. // they will be 0 by default, which indicates get all status or actors
  135. ctx.Data["CurActor"] = actorID
  136. ctx.Data["CurStatus"] = status
  137. if actorID > 0 || status > int(actions_model.StatusUnknown) {
  138. ctx.Data["IsFiltered"] = true
  139. }
  140. opts := actions_model.FindRunOptions{
  141. ListOptions: db.ListOptions{
  142. Page: page,
  143. PageSize: convert.ToCorrectPageSize(ctx.FormInt("limit")),
  144. },
  145. RepoID: ctx.Repo.Repository.ID,
  146. WorkflowID: workflow,
  147. TriggerUserID: actorID,
  148. }
  149. // if status is not StatusUnknown, it means user has selected a status filter
  150. if actions_model.Status(status) != actions_model.StatusUnknown {
  151. opts.Status = []actions_model.Status{actions_model.Status(status)}
  152. }
  153. runs, total, err := actions_model.FindRuns(ctx, opts)
  154. if err != nil {
  155. ctx.Error(http.StatusInternalServerError, err.Error())
  156. return
  157. }
  158. for _, run := range runs {
  159. run.Repo = ctx.Repo.Repository
  160. }
  161. if err := runs.LoadTriggerUser(ctx); err != nil {
  162. ctx.Error(http.StatusInternalServerError, err.Error())
  163. return
  164. }
  165. ctx.Data["Runs"] = runs
  166. actors, err := actions_model.GetActors(ctx, ctx.Repo.Repository.ID)
  167. if err != nil {
  168. ctx.Error(http.StatusInternalServerError, err.Error())
  169. return
  170. }
  171. ctx.Data["Actors"] = repo.MakeSelfOnTop(ctx.Doer, actors)
  172. ctx.Data["StatusInfoList"] = actions_model.GetStatusInfoList(ctx)
  173. pager := context.NewPagination(int(total), opts.PageSize, opts.Page, 5)
  174. pager.SetDefaultParams(ctx)
  175. pager.AddParamString("workflow", workflow)
  176. pager.AddParamString("actor", fmt.Sprint(actorID))
  177. pager.AddParamString("status", fmt.Sprint(status))
  178. ctx.Data["Page"] = pager
  179. ctx.Data["HasWorkflowsOrRuns"] = len(workflows) > 0 || len(runs) > 0
  180. ctx.HTML(http.StatusOK, tplListActions)
  181. }