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.

repo_list.go 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // Copyright 2017 The Gitea 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 models
  5. import (
  6. "fmt"
  7. "strings"
  8. "code.gitea.io/gitea/modules/util"
  9. "github.com/go-xorm/builder"
  10. )
  11. // RepositoryListDefaultPageSize is the default number of repositories
  12. // to load in memory when running administrative tasks on all (or almost
  13. // all) of them.
  14. // The number should be low enough to avoid filling up all RAM with
  15. // repository data...
  16. const RepositoryListDefaultPageSize = 64
  17. // RepositoryList contains a list of repositories
  18. type RepositoryList []*Repository
  19. func (repos RepositoryList) Len() int {
  20. return len(repos)
  21. }
  22. func (repos RepositoryList) Less(i, j int) bool {
  23. return repos[i].FullName() < repos[j].FullName()
  24. }
  25. func (repos RepositoryList) Swap(i, j int) {
  26. repos[i], repos[j] = repos[j], repos[i]
  27. }
  28. // RepositoryListOfMap make list from values of map
  29. func RepositoryListOfMap(repoMap map[int64]*Repository) RepositoryList {
  30. return RepositoryList(valuesRepository(repoMap))
  31. }
  32. func (repos RepositoryList) loadAttributes(e Engine) error {
  33. if len(repos) == 0 {
  34. return nil
  35. }
  36. // Load owners.
  37. set := make(map[int64]struct{})
  38. for i := range repos {
  39. set[repos[i].OwnerID] = struct{}{}
  40. }
  41. users := make(map[int64]*User, len(set))
  42. if err := e.
  43. Where("id > 0").
  44. In("id", keysInt64(set)).
  45. Find(&users); err != nil {
  46. return fmt.Errorf("find users: %v", err)
  47. }
  48. for i := range repos {
  49. repos[i].Owner = users[repos[i].OwnerID]
  50. }
  51. return nil
  52. }
  53. // LoadAttributes loads the attributes for the given RepositoryList
  54. func (repos RepositoryList) LoadAttributes() error {
  55. return repos.loadAttributes(x)
  56. }
  57. // MirrorRepositoryList contains the mirror repositories
  58. type MirrorRepositoryList []*Repository
  59. func (repos MirrorRepositoryList) loadAttributes(e Engine) error {
  60. if len(repos) == 0 {
  61. return nil
  62. }
  63. // Load mirrors.
  64. repoIDs := make([]int64, 0, len(repos))
  65. for i := range repos {
  66. if !repos[i].IsMirror {
  67. continue
  68. }
  69. repoIDs = append(repoIDs, repos[i].ID)
  70. }
  71. mirrors := make([]*Mirror, 0, len(repoIDs))
  72. if err := e.
  73. Where("id > 0").
  74. In("repo_id", repoIDs).
  75. Find(&mirrors); err != nil {
  76. return fmt.Errorf("find mirrors: %v", err)
  77. }
  78. set := make(map[int64]*Mirror)
  79. for i := range mirrors {
  80. set[mirrors[i].RepoID] = mirrors[i]
  81. }
  82. for i := range repos {
  83. repos[i].Mirror = set[repos[i].ID]
  84. }
  85. return nil
  86. }
  87. // LoadAttributes loads the attributes for the given MirrorRepositoryList
  88. func (repos MirrorRepositoryList) LoadAttributes() error {
  89. return repos.loadAttributes(x)
  90. }
  91. // SearchRepoOptions holds the search options
  92. type SearchRepoOptions struct {
  93. Keyword string
  94. OwnerID int64
  95. OrderBy SearchOrderBy
  96. Private bool // Include private repositories in results
  97. Starred bool
  98. Page int
  99. IsProfile bool
  100. AllPublic bool // Include also all public repositories
  101. PageSize int // Can be smaller than or equal to setting.ExplorePagingNum
  102. // None -> include collaborative AND non-collaborative
  103. // True -> include just collaborative
  104. // False -> incude just non-collaborative
  105. Collaborate util.OptionalBool
  106. // None -> include forks AND non-forks
  107. // True -> include just forks
  108. // False -> include just non-forks
  109. Fork util.OptionalBool
  110. // None -> include mirrors AND non-mirrors
  111. // True -> include just mirrors
  112. // False -> include just non-mirrors
  113. Mirror util.OptionalBool
  114. }
  115. //SearchOrderBy is used to sort the result
  116. type SearchOrderBy string
  117. func (s SearchOrderBy) String() string {
  118. return string(s)
  119. }
  120. // Strings for sorting result
  121. const (
  122. SearchOrderByAlphabetically SearchOrderBy = "name ASC"
  123. SearchOrderByAlphabeticallyReverse = "name DESC"
  124. SearchOrderByLeastUpdated = "updated_unix ASC"
  125. SearchOrderByRecentUpdated = "updated_unix DESC"
  126. SearchOrderByOldest = "created_unix ASC"
  127. SearchOrderByNewest = "created_unix DESC"
  128. SearchOrderBySize = "size ASC"
  129. SearchOrderBySizeReverse = "size DESC"
  130. SearchOrderByID = "id ASC"
  131. SearchOrderByIDReverse = "id DESC"
  132. SearchOrderByStars = "num_stars ASC"
  133. SearchOrderByStarsReverse = "num_stars DESC"
  134. SearchOrderByForks = "num_forks ASC"
  135. SearchOrderByForksReverse = "num_forks DESC"
  136. )
  137. // SearchRepositoryByName takes keyword and part of repository name to search,
  138. // it returns results in given range and number of total results.
  139. func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, error) {
  140. if opts.Page <= 0 {
  141. opts.Page = 1
  142. }
  143. var cond = builder.NewCond()
  144. if !opts.Private {
  145. cond = cond.And(builder.Eq{"is_private": false})
  146. }
  147. var starred bool
  148. if opts.OwnerID > 0 {
  149. if opts.Starred {
  150. starred = true
  151. cond = builder.Eq{"star.uid": opts.OwnerID}
  152. } else {
  153. var accessCond = builder.NewCond()
  154. if opts.Collaborate != util.OptionalBoolTrue {
  155. accessCond = builder.Eq{"owner_id": opts.OwnerID}
  156. }
  157. if opts.Collaborate != util.OptionalBoolFalse {
  158. collaborateCond := builder.And(
  159. builder.Expr("id IN (SELECT repo_id FROM `access` WHERE access.user_id = ?)", opts.OwnerID),
  160. builder.Neq{"owner_id": opts.OwnerID})
  161. if !opts.Private {
  162. collaborateCond = collaborateCond.And(builder.Expr("owner_id NOT IN (SELECT org_id FROM org_user WHERE org_user.uid = ? AND org_user.is_public = ?)", opts.OwnerID, false))
  163. }
  164. accessCond = accessCond.Or(collaborateCond)
  165. }
  166. if opts.AllPublic {
  167. accessCond = accessCond.Or(builder.Eq{"is_private": false})
  168. }
  169. cond = cond.And(accessCond)
  170. }
  171. }
  172. if opts.Keyword != "" {
  173. cond = cond.And(builder.Like{"lower_name", strings.ToLower(opts.Keyword)})
  174. }
  175. if opts.Fork != util.OptionalBoolNone {
  176. cond = cond.And(builder.Eq{"is_fork": opts.Fork == util.OptionalBoolTrue})
  177. }
  178. if opts.Mirror != util.OptionalBoolNone {
  179. cond = cond.And(builder.Eq{"is_mirror": opts.Mirror == util.OptionalBoolTrue})
  180. }
  181. if len(opts.OrderBy) == 0 {
  182. opts.OrderBy = SearchOrderByAlphabetically
  183. }
  184. sess := x.NewSession()
  185. defer sess.Close()
  186. if starred {
  187. sess.Join("INNER", "star", "star.repo_id = repository.id")
  188. }
  189. count, err := sess.
  190. Where(cond).
  191. Count(new(Repository))
  192. if err != nil {
  193. return nil, 0, fmt.Errorf("Count: %v", err)
  194. }
  195. // Set again after reset by Count()
  196. if starred {
  197. sess.Join("INNER", "star", "star.repo_id = repository.id")
  198. }
  199. repos := make(RepositoryList, 0, opts.PageSize)
  200. if err = sess.
  201. Where(cond).
  202. Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
  203. OrderBy(opts.OrderBy.String()).
  204. Find(&repos); err != nil {
  205. return nil, 0, fmt.Errorf("Repo: %v", err)
  206. }
  207. if !opts.IsProfile {
  208. if err = repos.loadAttributes(sess); err != nil {
  209. return nil, 0, fmt.Errorf("LoadAttributes: %v", err)
  210. }
  211. }
  212. return repos, count, nil
  213. }
  214. // FindUserAccessibleRepoIDs find all accessible repositories' ID by user's id
  215. func FindUserAccessibleRepoIDs(userID int64) ([]int64, error) {
  216. var accessCond builder.Cond = builder.Eq{"is_private": false}
  217. if userID > 0 {
  218. accessCond = accessCond.Or(
  219. builder.Eq{"owner_id": userID},
  220. builder.And(
  221. builder.Expr("id IN (SELECT repo_id FROM `access` WHERE access.user_id = ?)", userID),
  222. builder.Neq{"owner_id": userID},
  223. ),
  224. )
  225. }
  226. repoIDs := make([]int64, 0, 10)
  227. if err := x.
  228. Table("repository").
  229. Cols("id").
  230. Where(accessCond).
  231. Find(&repoIDs); err != nil {
  232. return nil, fmt.Errorf("FindUserAccesibleRepoIDs: %v", err)
  233. }
  234. return repoIDs, nil
  235. }