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.

runner_list.go 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. user_model "code.gitea.io/gitea/models/user"
  9. "code.gitea.io/gitea/modules/container"
  10. )
  11. type RunnerList []*ActionRunner
  12. // GetUserIDs returns a slice of user's id
  13. func (runners RunnerList) GetUserIDs() []int64 {
  14. return container.FilterSlice(runners, func(runner *ActionRunner) (int64, bool) {
  15. return runner.OwnerID, runner.OwnerID != 0
  16. })
  17. }
  18. func (runners RunnerList) LoadOwners(ctx context.Context) error {
  19. userIDs := runners.GetUserIDs()
  20. users := make(map[int64]*user_model.User, len(userIDs))
  21. if err := db.GetEngine(ctx).In("id", userIDs).Find(&users); err != nil {
  22. return err
  23. }
  24. for _, runner := range runners {
  25. if runner.OwnerID > 0 && runner.Owner == nil {
  26. runner.Owner = users[runner.OwnerID]
  27. }
  28. }
  29. return nil
  30. }
  31. func (runners RunnerList) getRepoIDs() []int64 {
  32. repoIDs := make(container.Set[int64], len(runners))
  33. for _, runner := range runners {
  34. if runner.RepoID == 0 {
  35. continue
  36. }
  37. if _, ok := repoIDs[runner.RepoID]; !ok {
  38. repoIDs[runner.RepoID] = struct{}{}
  39. }
  40. }
  41. return repoIDs.Values()
  42. }
  43. func (runners RunnerList) LoadRepos(ctx context.Context) error {
  44. repoIDs := runners.getRepoIDs()
  45. repos := make(map[int64]*repo_model.Repository, len(repoIDs))
  46. if err := db.GetEngine(ctx).In("id", repoIDs).Find(&repos); err != nil {
  47. return err
  48. }
  49. for _, runner := range runners {
  50. if runner.RepoID > 0 && runner.Repo == nil {
  51. runner.Repo = repos[runner.RepoID]
  52. }
  53. }
  54. return nil
  55. }
  56. func (runners RunnerList) LoadAttributes(ctx context.Context) error {
  57. if err := runners.LoadOwners(ctx); err != nil {
  58. return err
  59. }
  60. return runners.LoadRepos(ctx)
  61. }