gitea源码

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