org.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package db
  5. import (
  6. "fmt"
  7. "xorm.io/builder"
  8. )
  9. // GetUserMirrorRepositories returns mirror repositories of the organization which the user has access to.
  10. func (u *User) GetUserMirrorRepositories(userID int64) ([]*Repository, error) {
  11. teamIDs, err := u.GetUserTeamIDs(userID)
  12. if err != nil {
  13. return nil, fmt.Errorf("GetUserTeamIDs: %v", err)
  14. }
  15. if len(teamIDs) == 0 {
  16. teamIDs = []int64{-1}
  17. }
  18. var teamRepoIDs []int64
  19. err = x.Table("team_repo").In("team_id", teamIDs).Distinct("repo_id").Find(&teamRepoIDs)
  20. if err != nil {
  21. return nil, fmt.Errorf("get team repository ids: %v", err)
  22. }
  23. if len(teamRepoIDs) == 0 {
  24. // team has no repo but "IN ()" is invalid SQL
  25. teamRepoIDs = []int64{-1} // there is no repo with id=-1
  26. }
  27. repos := make([]*Repository, 0, 10)
  28. if err = x.Where("owner_id = ?", u.ID).
  29. And("is_private = ?", false).
  30. Or(builder.In("id", teamRepoIDs)).
  31. And("is_mirror = ?", true). // Don't move up because it's an independent condition
  32. Desc("updated_unix").
  33. Find(&repos); err != nil {
  34. return nil, fmt.Errorf("get user repositories: %v", err)
  35. }
  36. return repos, nil
  37. }