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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. user_model "code.gitea.io/gitea/models/user"
  9. "code.gitea.io/gitea/modules/container"
  10. "xorm.io/builder"
  11. )
  12. type ScheduleList []*ActionSchedule
  13. // GetUserIDs returns a slice of user's id
  14. func (schedules ScheduleList) GetUserIDs() []int64 {
  15. ids := make(container.Set[int64], len(schedules))
  16. for _, schedule := range schedules {
  17. ids.Add(schedule.TriggerUserID)
  18. }
  19. return ids.Values()
  20. }
  21. func (schedules ScheduleList) GetRepoIDs() []int64 {
  22. ids := make(container.Set[int64], len(schedules))
  23. for _, schedule := range schedules {
  24. ids.Add(schedule.RepoID)
  25. }
  26. return ids.Values()
  27. }
  28. func (schedules ScheduleList) LoadTriggerUser(ctx context.Context) error {
  29. userIDs := schedules.GetUserIDs()
  30. users := make(map[int64]*user_model.User, len(userIDs))
  31. if err := db.GetEngine(ctx).In("id", userIDs).Find(&users); err != nil {
  32. return err
  33. }
  34. for _, schedule := range schedules {
  35. if schedule.TriggerUserID == user_model.ActionsUserID {
  36. schedule.TriggerUser = user_model.NewActionsUser()
  37. } else {
  38. schedule.TriggerUser = users[schedule.TriggerUserID]
  39. }
  40. }
  41. return nil
  42. }
  43. func (schedules ScheduleList) LoadRepos() error {
  44. repoIDs := schedules.GetRepoIDs()
  45. repos, err := repo_model.GetRepositoriesMapByIDs(repoIDs)
  46. if err != nil {
  47. return err
  48. }
  49. for _, schedule := range schedules {
  50. schedule.Repo = repos[schedule.RepoID]
  51. }
  52. return nil
  53. }
  54. type FindScheduleOptions struct {
  55. db.ListOptions
  56. RepoID int64
  57. OwnerID int64
  58. }
  59. func (opts FindScheduleOptions) toConds() builder.Cond {
  60. cond := builder.NewCond()
  61. if opts.RepoID > 0 {
  62. cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
  63. }
  64. if opts.OwnerID > 0 {
  65. cond = cond.And(builder.Eq{"owner_id": opts.OwnerID})
  66. }
  67. return cond
  68. }
  69. func FindSchedules(ctx context.Context, opts FindScheduleOptions) (ScheduleList, int64, error) {
  70. e := db.GetEngine(ctx).Where(opts.toConds())
  71. if !opts.ListAll && opts.PageSize > 0 && opts.Page >= 1 {
  72. e.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
  73. }
  74. var schedules ScheduleList
  75. total, err := e.Desc("id").FindAndCount(&schedules)
  76. return schedules, total, err
  77. }
  78. func CountSchedules(ctx context.Context, opts FindScheduleOptions) (int64, error) {
  79. return db.GetEngine(ctx).Where(opts.toConds()).Count(new(ActionSchedule))
  80. }