gitea源码

lfs_lock_list.go 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package git
  4. import (
  5. "context"
  6. "fmt"
  7. "code.gitea.io/gitea/models/db"
  8. user_model "code.gitea.io/gitea/models/user"
  9. "code.gitea.io/gitea/modules/container"
  10. )
  11. // LFSLockList is a list of LFSLock
  12. type LFSLockList []*LFSLock
  13. // LoadAttributes loads the attributes for the given locks
  14. func (locks LFSLockList) LoadAttributes(ctx context.Context) error {
  15. if len(locks) == 0 {
  16. return nil
  17. }
  18. if err := locks.LoadOwner(ctx); err != nil {
  19. return fmt.Errorf("load owner: %w", err)
  20. }
  21. return nil
  22. }
  23. // LoadOwner loads the owner of the locks
  24. func (locks LFSLockList) LoadOwner(ctx context.Context) error {
  25. if len(locks) == 0 {
  26. return nil
  27. }
  28. usersIDs := container.FilterSlice(locks, func(lock *LFSLock) (int64, bool) {
  29. return lock.OwnerID, true
  30. })
  31. users := make(map[int64]*user_model.User, len(usersIDs))
  32. if err := db.GetEngine(ctx).
  33. In("id", usersIDs).
  34. Find(&users); err != nil {
  35. return fmt.Errorf("find users: %w", err)
  36. }
  37. for _, v := range locks {
  38. v.Owner = users[v.OwnerID]
  39. if v.Owner == nil { // not exist
  40. v.Owner = user_model.NewGhostUser()
  41. }
  42. }
  43. return nil
  44. }