You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

repo_list.go 8.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. // only search topic name
  115. TopicOnly bool
  116. }
  117. //SearchOrderBy is used to sort the result
  118. type SearchOrderBy string
  119. func (s SearchOrderBy) String() string {
  120. return string(s)
  121. }
  122. // Strings for sorting result
  123. const (
  124. SearchOrderByAlphabetically SearchOrderBy = "name ASC"
  125. SearchOrderByAlphabeticallyReverse = "name DESC"
  126. SearchOrderByLeastUpdated = "updated_unix ASC"
  127. SearchOrderByRecentUpdated = "updated_unix DESC"
  128. SearchOrderByOldest = "created_unix ASC"
  129. SearchOrderByNewest = "created_unix DESC"
  130. SearchOrderBySize = "size ASC"
  131. SearchOrderBySizeReverse = "size DESC"
  132. SearchOrderByID = "id ASC"
  133. SearchOrderByIDReverse = "id DESC"
  134. SearchOrderByStars = "num_stars ASC"
  135. SearchOrderByStarsReverse = "num_stars DESC"
  136. SearchOrderByForks = "num_forks ASC"
  137. SearchOrderByForksReverse = "num_forks DESC"
  138. )
  139. // SearchRepositoryByName takes keyword and part of repository name to search,
  140. // it returns results in given range and number of total results.
  141. func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, error) {
  142. if opts.Page <= 0 {
  143. opts.Page = 1
  144. }
  145. var cond = builder.NewCond()
  146. if !opts.Private {
  147. cond = cond.And(builder.Eq{"is_private": false})
  148. }
  149. var starred bool
  150. if opts.OwnerID > 0 {
  151. if opts.Starred {
  152. starred = true
  153. cond = builder.Eq{"star.uid": opts.OwnerID}
  154. } else {
  155. var accessCond = builder.NewCond()
  156. if opts.Collaborate != util.OptionalBoolTrue {
  157. accessCond = builder.Eq{"owner_id": opts.OwnerID}
  158. }
  159. if opts.Collaborate != util.OptionalBoolFalse {
  160. collaborateCond := builder.And(
  161. builder.Expr("repository.id IN (SELECT repo_id FROM `access` WHERE access.user_id = ?)", opts.OwnerID),
  162. builder.Neq{"owner_id": opts.OwnerID})
  163. if !opts.Private {
  164. 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))
  165. }
  166. accessCond = accessCond.Or(collaborateCond)
  167. }
  168. if opts.AllPublic {
  169. accessCond = accessCond.Or(builder.Eq{"is_private": false})
  170. }
  171. cond = cond.And(accessCond)
  172. }
  173. }
  174. if opts.Keyword != "" {
  175. var keywordCond = builder.NewCond()
  176. if opts.TopicOnly {
  177. keywordCond = keywordCond.Or(builder.Like{"topic.name", strings.ToLower(opts.Keyword)})
  178. } else {
  179. keywordCond = keywordCond.Or(builder.Like{"lower_name", strings.ToLower(opts.Keyword)})
  180. keywordCond = keywordCond.Or(builder.Like{"topic.name", strings.ToLower(opts.Keyword)})
  181. }
  182. cond = cond.And(keywordCond)
  183. }
  184. if opts.Fork != util.OptionalBoolNone {
  185. cond = cond.And(builder.Eq{"is_fork": opts.Fork == util.OptionalBoolTrue})
  186. }
  187. if opts.Mirror != util.OptionalBoolNone {
  188. cond = cond.And(builder.Eq{"is_mirror": opts.Mirror == util.OptionalBoolTrue})
  189. }
  190. if len(opts.OrderBy) == 0 {
  191. opts.OrderBy = SearchOrderByAlphabetically
  192. }
  193. sess := x.NewSession()
  194. defer sess.Close()
  195. if starred {
  196. sess.Join("INNER", "star", "star.repo_id = repository.id")
  197. }
  198. if opts.Keyword != "" {
  199. sess.Join("LEFT", "repo_topic", "repo_topic.repo_id = repository.id")
  200. sess.Join("LEFT", "topic", "repo_topic.topic_id = topic.id")
  201. }
  202. count, err := sess.
  203. Where(cond).
  204. Count(new(Repository))
  205. if err != nil {
  206. return nil, 0, fmt.Errorf("Count: %v", err)
  207. }
  208. // Set again after reset by Count()
  209. if starred {
  210. sess.Join("INNER", "star", "star.repo_id = repository.id")
  211. }
  212. if opts.Keyword != "" {
  213. sess.Join("LEFT", "repo_topic", "repo_topic.repo_id = repository.id")
  214. sess.Join("LEFT", "topic", "repo_topic.topic_id = topic.id")
  215. }
  216. if opts.Keyword != "" {
  217. sess.Select("repository.*")
  218. sess.GroupBy("repository.id")
  219. sess.OrderBy("repository." + opts.OrderBy.String())
  220. } else {
  221. sess.OrderBy(opts.OrderBy.String())
  222. }
  223. repos := make(RepositoryList, 0, opts.PageSize)
  224. if err = sess.
  225. Where(cond).
  226. Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
  227. Find(&repos); err != nil {
  228. return nil, 0, fmt.Errorf("Repo: %v", err)
  229. }
  230. if !opts.IsProfile {
  231. if err = repos.loadAttributes(sess); err != nil {
  232. return nil, 0, fmt.Errorf("LoadAttributes: %v", err)
  233. }
  234. }
  235. return repos, count, nil
  236. }
  237. // FindUserAccessibleRepoIDs find all accessible repositories' ID by user's id
  238. func FindUserAccessibleRepoIDs(userID int64) ([]int64, error) {
  239. var accessCond builder.Cond = builder.Eq{"is_private": false}
  240. if userID > 0 {
  241. accessCond = accessCond.Or(
  242. builder.Eq{"owner_id": userID},
  243. builder.And(
  244. builder.Expr("id IN (SELECT repo_id FROM `access` WHERE access.user_id = ?)", userID),
  245. builder.Neq{"owner_id": userID},
  246. ),
  247. )
  248. }
  249. repoIDs := make([]int64, 0, 10)
  250. if err := x.
  251. Table("repository").
  252. Cols("id").
  253. Where(accessCond).
  254. Find(&repoIDs); err != nil {
  255. return nil, fmt.Errorf("FindUserAccesibleRepoIDs: %v", err)
  256. }
  257. return repoIDs, nil
  258. }