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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. ids := make(container.Set[int64], len(runners))
  15. for _, runner := range runners {
  16. if runner.OwnerID == 0 {
  17. continue
  18. }
  19. ids.Add(runner.OwnerID)
  20. }
  21. return ids.Values()
  22. }
  23. func (runners RunnerList) LoadOwners(ctx context.Context) error {
  24. userIDs := runners.GetUserIDs()
  25. users := make(map[int64]*user_model.User, len(userIDs))
  26. if err := db.GetEngine(ctx).In("id", userIDs).Find(&users); err != nil {
  27. return err
  28. }
  29. for _, runner := range runners {
  30. if runner.OwnerID > 0 && runner.Owner == nil {
  31. runner.Owner = users[runner.OwnerID]
  32. }
  33. }
  34. return nil
  35. }
  36. func (runners RunnerList) getRepoIDs() []int64 {
  37. repoIDs := make(container.Set[int64], len(runners))
  38. for _, runner := range runners {
  39. if runner.RepoID == 0 {
  40. continue
  41. }
  42. if _, ok := repoIDs[runner.RepoID]; !ok {
  43. repoIDs[runner.RepoID] = struct{}{}
  44. }
  45. }
  46. return repoIDs.Values()
  47. }
  48. func (runners RunnerList) LoadRepos(ctx context.Context) error {
  49. repoIDs := runners.getRepoIDs()
  50. repos := make(map[int64]*repo_model.Repository, len(repoIDs))
  51. if err := db.GetEngine(ctx).In("id", repoIDs).Find(&repos); err != nil {
  52. return err
  53. }
  54. for _, runner := range runners {
  55. if runner.RepoID > 0 && runner.Repo == nil {
  56. runner.Repo = repos[runner.RepoID]
  57. }
  58. }
  59. return nil
  60. }
  61. func (runners RunnerList) LoadAttributes(ctx context.Context) error {
  62. if err := runners.LoadOwners(ctx); err != nil {
  63. return err
  64. }
  65. return runners.LoadRepos(ctx)
  66. }