gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. return container.FilterSlice(runners, func(runner *ActionRunner) (int64, bool) {
  33. return runner.RepoID, runner.RepoID > 0
  34. })
  35. }
  36. func (runners RunnerList) LoadRepos(ctx context.Context) error {
  37. repoIDs := runners.getRepoIDs()
  38. repos := make(map[int64]*repo_model.Repository, len(repoIDs))
  39. if err := db.GetEngine(ctx).In("id", repoIDs).Find(&repos); err != nil {
  40. return err
  41. }
  42. for _, runner := range runners {
  43. if runner.RepoID > 0 && runner.Repo == nil {
  44. runner.Repo = repos[runner.RepoID]
  45. }
  46. }
  47. return nil
  48. }
  49. func (runners RunnerList) LoadAttributes(ctx context.Context) error {
  50. if err := runners.LoadOwners(ctx); err != nil {
  51. return err
  52. }
  53. return runners.LoadRepos(ctx)
  54. }