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

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