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_job_list.go 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. "code.gitea.io/gitea/modules/container"
  8. "code.gitea.io/gitea/modules/timeutil"
  9. "xorm.io/builder"
  10. )
  11. type ActionJobList []*ActionRunJob
  12. func (jobs ActionJobList) GetRunIDs() []int64 {
  13. ids := make(container.Set[int64], len(jobs))
  14. for _, j := range jobs {
  15. if j.RunID == 0 {
  16. continue
  17. }
  18. ids.Add(j.RunID)
  19. }
  20. return ids.Values()
  21. }
  22. func (jobs ActionJobList) LoadRuns(ctx context.Context, withRepo bool) error {
  23. runIDs := jobs.GetRunIDs()
  24. runs := make(map[int64]*ActionRun, len(runIDs))
  25. if err := db.GetEngine(ctx).In("id", runIDs).Find(&runs); err != nil {
  26. return err
  27. }
  28. for _, j := range jobs {
  29. if j.RunID > 0 && j.Run == nil {
  30. j.Run = runs[j.RunID]
  31. }
  32. }
  33. if withRepo {
  34. var runsList RunList = make([]*ActionRun, 0, len(runs))
  35. for _, r := range runs {
  36. runsList = append(runsList, r)
  37. }
  38. return runsList.LoadRepos()
  39. }
  40. return nil
  41. }
  42. func (jobs ActionJobList) LoadAttributes(ctx context.Context, withRepo bool) error {
  43. return jobs.LoadRuns(ctx, withRepo)
  44. }
  45. type FindRunJobOptions struct {
  46. db.ListOptions
  47. RunID int64
  48. RepoID int64
  49. OwnerID int64
  50. CommitSHA string
  51. Statuses []Status
  52. UpdatedBefore timeutil.TimeStamp
  53. }
  54. func (opts FindRunJobOptions) toConds() builder.Cond {
  55. cond := builder.NewCond()
  56. if opts.RunID > 0 {
  57. cond = cond.And(builder.Eq{"run_id": opts.RunID})
  58. }
  59. if opts.RepoID > 0 {
  60. cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
  61. }
  62. if opts.OwnerID > 0 {
  63. cond = cond.And(builder.Eq{"owner_id": opts.OwnerID})
  64. }
  65. if opts.CommitSHA != "" {
  66. cond = cond.And(builder.Eq{"commit_sha": opts.CommitSHA})
  67. }
  68. if len(opts.Statuses) > 0 {
  69. cond = cond.And(builder.In("status", opts.Statuses))
  70. }
  71. if opts.UpdatedBefore > 0 {
  72. cond = cond.And(builder.Lt{"updated": opts.UpdatedBefore})
  73. }
  74. return cond
  75. }
  76. func FindRunJobs(ctx context.Context, opts FindRunJobOptions) (ActionJobList, int64, error) {
  77. e := db.GetEngine(ctx).Where(opts.toConds())
  78. if opts.PageSize > 0 && opts.Page >= 1 {
  79. e.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
  80. }
  81. var tasks ActionJobList
  82. total, err := e.FindAndCount(&tasks)
  83. return tasks, total, err
  84. }
  85. func CountRunJobs(ctx context.Context, opts FindRunJobOptions) (int64, error) {
  86. return db.GetEngine(ctx).Where(opts.toConds()).Count(new(ActionRunJob))
  87. }