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

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