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.

run_list.go 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package actions
  4. import (
  5. "context"
  6. "code.gitea.io/gitea/models/db"
  7. repo_model "code.gitea.io/gitea/models/repo"
  8. user_model "code.gitea.io/gitea/models/user"
  9. "code.gitea.io/gitea/modules/container"
  10. webhook_module "code.gitea.io/gitea/modules/webhook"
  11. "xorm.io/builder"
  12. )
  13. type RunList []*ActionRun
  14. // GetUserIDs returns a slice of user's id
  15. func (runs RunList) GetUserIDs() []int64 {
  16. ids := make(container.Set[int64], len(runs))
  17. for _, run := range runs {
  18. ids.Add(run.TriggerUserID)
  19. }
  20. return ids.Values()
  21. }
  22. func (runs RunList) GetRepoIDs() []int64 {
  23. ids := make(container.Set[int64], len(runs))
  24. for _, run := range runs {
  25. ids.Add(run.RepoID)
  26. }
  27. return ids.Values()
  28. }
  29. func (runs RunList) LoadTriggerUser(ctx context.Context) error {
  30. userIDs := runs.GetUserIDs()
  31. users := make(map[int64]*user_model.User, len(userIDs))
  32. if err := db.GetEngine(ctx).In("id", userIDs).Find(&users); err != nil {
  33. return err
  34. }
  35. for _, run := range runs {
  36. if run.TriggerUserID == user_model.ActionsUserID {
  37. run.TriggerUser = user_model.NewActionsUser()
  38. } else {
  39. run.TriggerUser = users[run.TriggerUserID]
  40. if run.TriggerUser == nil {
  41. run.TriggerUser = user_model.NewGhostUser()
  42. }
  43. }
  44. }
  45. return nil
  46. }
  47. func (runs RunList) LoadRepos() error {
  48. repoIDs := runs.GetRepoIDs()
  49. repos, err := repo_model.GetRepositoriesMapByIDs(repoIDs)
  50. if err != nil {
  51. return err
  52. }
  53. for _, run := range runs {
  54. run.Repo = repos[run.RepoID]
  55. }
  56. return nil
  57. }
  58. type FindRunOptions struct {
  59. db.ListOptions
  60. RepoID int64
  61. OwnerID int64
  62. WorkflowID string
  63. Ref string // the commit/tag/… that caused this workflow
  64. TriggerUserID int64
  65. TriggerEvent webhook_module.HookEventType
  66. Approved bool // not util.OptionalBool, it works only when it's true
  67. Status []Status
  68. }
  69. func (opts FindRunOptions) toConds() builder.Cond {
  70. cond := builder.NewCond()
  71. if opts.RepoID > 0 {
  72. cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
  73. }
  74. if opts.OwnerID > 0 {
  75. cond = cond.And(builder.Eq{"owner_id": opts.OwnerID})
  76. }
  77. if opts.WorkflowID != "" {
  78. cond = cond.And(builder.Eq{"workflow_id": opts.WorkflowID})
  79. }
  80. if opts.TriggerUserID > 0 {
  81. cond = cond.And(builder.Eq{"trigger_user_id": opts.TriggerUserID})
  82. }
  83. if opts.Approved {
  84. cond = cond.And(builder.Gt{"approved_by": 0})
  85. }
  86. if len(opts.Status) > 0 {
  87. cond = cond.And(builder.In("status", opts.Status))
  88. }
  89. if opts.Ref != "" {
  90. cond = cond.And(builder.Eq{"ref": opts.Ref})
  91. }
  92. if opts.TriggerEvent != "" {
  93. cond = cond.And(builder.Eq{"trigger_event": opts.TriggerEvent})
  94. }
  95. return cond
  96. }
  97. func FindRuns(ctx context.Context, opts FindRunOptions) (RunList, int64, error) {
  98. e := db.GetEngine(ctx).Where(opts.toConds())
  99. if opts.PageSize > 0 && opts.Page >= 1 {
  100. e.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
  101. }
  102. var runs RunList
  103. total, err := e.Desc("id").FindAndCount(&runs)
  104. return runs, total, err
  105. }
  106. func CountRuns(ctx context.Context, opts FindRunOptions) (int64, error) {
  107. return db.GetEngine(ctx).Where(opts.toConds()).Count(new(ActionRun))
  108. }
  109. type StatusInfo struct {
  110. Status int
  111. DisplayedStatus string
  112. }
  113. // GetStatusInfoList returns a slice of StatusInfo
  114. func GetStatusInfoList(ctx context.Context) []StatusInfo {
  115. // same as those in aggregateJobStatus
  116. allStatus := []Status{StatusSuccess, StatusFailure, StatusWaiting, StatusRunning}
  117. statusInfoList := make([]StatusInfo, 0, 4)
  118. for _, s := range allStatus {
  119. statusInfoList = append(statusInfoList, StatusInfo{
  120. Status: int(s),
  121. DisplayedStatus: s.String(),
  122. })
  123. }
  124. return statusInfoList
  125. }
  126. // GetActors returns a slice of Actors
  127. func GetActors(ctx context.Context, repoID int64) ([]*user_model.User, error) {
  128. actors := make([]*user_model.User, 0, 10)
  129. return actors, db.GetEngine(ctx).Where(builder.In("id", builder.Select("`action_run`.trigger_user_id").From("`action_run`").
  130. GroupBy("`action_run`.trigger_user_id").
  131. Where(builder.Eq{"`action_run`.repo_id": repoID}))).
  132. Cols("id", "name", "full_name", "avatar", "avatar_email", "use_custom_avatar").
  133. OrderBy(user_model.GetOrderByName()).
  134. Find(&actors)
  135. }