gitea源码

run_job_list.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. "code.gitea.io/gitea/modules/container"
  9. "code.gitea.io/gitea/modules/timeutil"
  10. "xorm.io/builder"
  11. )
  12. type ActionJobList []*ActionRunJob
  13. func (jobs ActionJobList) GetRunIDs() []int64 {
  14. return container.FilterSlice(jobs, func(j *ActionRunJob) (int64, bool) {
  15. return j.RunID, j.RunID != 0
  16. })
  17. }
  18. func (jobs ActionJobList) LoadRepos(ctx context.Context) error {
  19. repoIDs := container.FilterSlice(jobs, func(j *ActionRunJob) (int64, bool) {
  20. return j.RepoID, j.RepoID != 0 && j.Repo == nil
  21. })
  22. if len(repoIDs) == 0 {
  23. return nil
  24. }
  25. repos := make(map[int64]*repo_model.Repository, len(repoIDs))
  26. if err := db.GetEngine(ctx).In("id", repoIDs).Find(&repos); err != nil {
  27. return err
  28. }
  29. for _, j := range jobs {
  30. if j.RepoID > 0 && j.Repo == nil {
  31. j.Repo = repos[j.RepoID]
  32. }
  33. }
  34. return nil
  35. }
  36. func (jobs ActionJobList) LoadRuns(ctx context.Context, withRepo bool) error {
  37. if withRepo {
  38. if err := jobs.LoadRepos(ctx); err != nil {
  39. return err
  40. }
  41. }
  42. runIDs := jobs.GetRunIDs()
  43. runs := make(map[int64]*ActionRun, len(runIDs))
  44. if err := db.GetEngine(ctx).In("id", runIDs).Find(&runs); err != nil {
  45. return err
  46. }
  47. for _, j := range jobs {
  48. if j.RunID > 0 && j.Run == nil {
  49. j.Run = runs[j.RunID]
  50. j.Run.Repo = j.Repo
  51. }
  52. }
  53. return nil
  54. }
  55. func (jobs ActionJobList) LoadAttributes(ctx context.Context, withRepo bool) error {
  56. return jobs.LoadRuns(ctx, withRepo)
  57. }
  58. type FindRunJobOptions struct {
  59. db.ListOptions
  60. RunID int64
  61. RepoID int64
  62. OwnerID int64
  63. CommitSHA string
  64. Statuses []Status
  65. UpdatedBefore timeutil.TimeStamp
  66. }
  67. func (opts FindRunJobOptions) ToConds() builder.Cond {
  68. cond := builder.NewCond()
  69. if opts.RunID > 0 {
  70. cond = cond.And(builder.Eq{"`action_run_job`.run_id": opts.RunID})
  71. }
  72. if opts.RepoID > 0 {
  73. cond = cond.And(builder.Eq{"`action_run_job`.repo_id": opts.RepoID})
  74. }
  75. if opts.CommitSHA != "" {
  76. cond = cond.And(builder.Eq{"`action_run_job`.commit_sha": opts.CommitSHA})
  77. }
  78. if len(opts.Statuses) > 0 {
  79. cond = cond.And(builder.In("`action_run_job`.status", opts.Statuses))
  80. }
  81. if opts.UpdatedBefore > 0 {
  82. cond = cond.And(builder.Lt{"`action_run_job`.updated": opts.UpdatedBefore})
  83. }
  84. return cond
  85. }
  86. func (opts FindRunJobOptions) ToJoins() []db.JoinFunc {
  87. if opts.OwnerID > 0 {
  88. return []db.JoinFunc{
  89. func(sess db.Engine) error {
  90. sess.Join("INNER", "repository", "repository.id = repo_id AND repository.owner_id = ?", opts.OwnerID)
  91. return nil
  92. },
  93. }
  94. }
  95. return nil
  96. }