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.

task_list.go 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 TaskList []*ActionTask
  12. func (tasks TaskList) GetJobIDs() []int64 {
  13. return container.FilterSlice(tasks, func(t *ActionTask) (int64, bool) {
  14. return t.JobID, t.JobID != 0
  15. })
  16. }
  17. func (tasks TaskList) LoadJobs(ctx context.Context) error {
  18. jobIDs := tasks.GetJobIDs()
  19. jobs := make(map[int64]*ActionRunJob, len(jobIDs))
  20. if err := db.GetEngine(ctx).In("id", jobIDs).Find(&jobs); err != nil {
  21. return err
  22. }
  23. for _, t := range tasks {
  24. if t.JobID > 0 && t.Job == nil {
  25. t.Job = jobs[t.JobID]
  26. }
  27. }
  28. // TODO: Replace with "ActionJobList(maps.Values(jobs))" once available
  29. var jobsList ActionJobList = make([]*ActionRunJob, 0, len(jobs))
  30. for _, j := range jobs {
  31. jobsList = append(jobsList, j)
  32. }
  33. return jobsList.LoadAttributes(ctx, true)
  34. }
  35. func (tasks TaskList) LoadAttributes(ctx context.Context) error {
  36. return tasks.LoadJobs(ctx)
  37. }
  38. type FindTaskOptions struct {
  39. db.ListOptions
  40. RepoID int64
  41. OwnerID int64
  42. CommitSHA string
  43. Status Status
  44. UpdatedBefore timeutil.TimeStamp
  45. StartedBefore timeutil.TimeStamp
  46. RunnerID int64
  47. IDOrderDesc bool
  48. }
  49. func (opts FindTaskOptions) ToConds() builder.Cond {
  50. cond := builder.NewCond()
  51. if opts.RepoID > 0 {
  52. cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
  53. }
  54. if opts.OwnerID > 0 {
  55. cond = cond.And(builder.Eq{"owner_id": opts.OwnerID})
  56. }
  57. if opts.CommitSHA != "" {
  58. cond = cond.And(builder.Eq{"commit_sha": opts.CommitSHA})
  59. }
  60. if opts.Status > StatusUnknown {
  61. cond = cond.And(builder.Eq{"status": opts.Status})
  62. }
  63. if opts.UpdatedBefore > 0 {
  64. cond = cond.And(builder.Lt{"updated": opts.UpdatedBefore})
  65. }
  66. if opts.StartedBefore > 0 {
  67. cond = cond.And(builder.Lt{"started": opts.StartedBefore})
  68. }
  69. if opts.RunnerID > 0 {
  70. cond = cond.And(builder.Eq{"runner_id": opts.RunnerID})
  71. }
  72. return cond
  73. }
  74. func (opts FindTaskOptions) ToOrders() string {
  75. if opts.IDOrderDesc {
  76. return "`id` DESC"
  77. }
  78. return ""
  79. }