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.

schedule_spec_list.go 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2023 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. "xorm.io/builder"
  10. )
  11. type SpecList []*ActionScheduleSpec
  12. func (specs SpecList) GetScheduleIDs() []int64 {
  13. ids := make(container.Set[int64], len(specs))
  14. for _, spec := range specs {
  15. ids.Add(spec.ScheduleID)
  16. }
  17. return ids.Values()
  18. }
  19. func (specs SpecList) LoadSchedules(ctx context.Context) error {
  20. scheduleIDs := specs.GetScheduleIDs()
  21. schedules, err := GetSchedulesMapByIDs(ctx, scheduleIDs)
  22. if err != nil {
  23. return err
  24. }
  25. for _, spec := range specs {
  26. spec.Schedule = schedules[spec.ScheduleID]
  27. }
  28. repoIDs := specs.GetRepoIDs()
  29. repos, err := GetReposMapByIDs(ctx, repoIDs)
  30. if err != nil {
  31. return err
  32. }
  33. for _, spec := range specs {
  34. spec.Repo = repos[spec.RepoID]
  35. }
  36. return nil
  37. }
  38. func (specs SpecList) GetRepoIDs() []int64 {
  39. ids := make(container.Set[int64], len(specs))
  40. for _, spec := range specs {
  41. ids.Add(spec.RepoID)
  42. }
  43. return ids.Values()
  44. }
  45. func (specs SpecList) LoadRepos() error {
  46. repoIDs := specs.GetRepoIDs()
  47. repos, err := repo_model.GetRepositoriesMapByIDs(repoIDs)
  48. if err != nil {
  49. return err
  50. }
  51. for _, spec := range specs {
  52. spec.Repo = repos[spec.RepoID]
  53. }
  54. return nil
  55. }
  56. type FindSpecOptions struct {
  57. db.ListOptions
  58. RepoID int64
  59. Next int64
  60. }
  61. func (opts FindSpecOptions) toConds() builder.Cond {
  62. cond := builder.NewCond()
  63. if opts.RepoID > 0 {
  64. cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
  65. }
  66. if opts.Next > 0 {
  67. cond = cond.And(builder.Lte{"next": opts.Next})
  68. }
  69. return cond
  70. }
  71. func FindSpecs(ctx context.Context, opts FindSpecOptions) (SpecList, int64, error) {
  72. e := db.GetEngine(ctx).Where(opts.toConds())
  73. if opts.PageSize > 0 && opts.Page >= 1 {
  74. e.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
  75. }
  76. var specs SpecList
  77. total, err := e.Desc("id").FindAndCount(&specs)
  78. if err != nil {
  79. return nil, 0, err
  80. }
  81. if err := specs.LoadSchedules(ctx); err != nil {
  82. return nil, 0, err
  83. }
  84. return specs, total, nil
  85. }
  86. func CountSpecs(ctx context.Context, opts FindSpecOptions) (int64, error) {
  87. return db.GetEngine(ctx).Where(opts.toConds()).Count(new(ActionScheduleSpec))
  88. }