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 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. if opts.OwnerID > 0 {
  150. if opts.Starred {
  151. cond = cond.And(builder.In("id", builder.Select("repo_id").From("star").Where(builder.Eq{"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("repository.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. // separate keyword
  174. var subQueryCond = builder.NewCond()
  175. for _, v := range strings.Split(opts.Keyword, ",") {
  176. subQueryCond = subQueryCond.Or(builder.Like{"topic.name", strings.ToLower(v)})
  177. }
  178. subQuery := builder.Select("repo_topic.repo_id").From("repo_topic").
  179. Join("INNER", "topic", "topic.id = repo_topic.topic_id").
  180. Where(subQueryCond).
  181. GroupBy("repo_topic.repo_id")
  182. var keywordCond = builder.In("id", subQuery)
  183. if !opts.TopicOnly {
  184. var likes = builder.NewCond()
  185. for _, v := range strings.Split(opts.Keyword, ",") {
  186. likes = likes.Or(builder.Like{"lower_name", strings.ToLower(v)})
  187. }
  188. keywordCond = keywordCond.Or(likes)
  189. }
  190. cond = cond.And(keywordCond)
  191. }
  192. if opts.Fork != util.OptionalBoolNone {
  193. cond = cond.And(builder.Eq{"is_fork": opts.Fork == util.OptionalBoolTrue})
  194. }
  195. if opts.Mirror != util.OptionalBoolNone {
  196. cond = cond.And(builder.Eq{"is_mirror": opts.Mirror == util.OptionalBoolTrue})
  197. }
  198. if len(opts.OrderBy) == 0 {
  199. opts.OrderBy = SearchOrderByAlphabetically
  200. }
  201. sess := x.NewSession()
  202. defer sess.Close()
  203. count, err := sess.
  204. Where(cond).
  205. Count(new(Repository))
  206. if err != nil {
  207. return nil, 0, fmt.Errorf("Count: %v", err)
  208. }
  209. repos := make(RepositoryList, 0, opts.PageSize)
  210. if err = sess.
  211. Where(cond).
  212. OrderBy(opts.OrderBy.String()).
  213. Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
  214. Find(&repos); err != nil {
  215. return nil, 0, fmt.Errorf("Repo: %v", err)
  216. }
  217. if !opts.IsProfile {
  218. if err = repos.loadAttributes(sess); err != nil {
  219. return nil, 0, fmt.Errorf("LoadAttributes: %v", err)
  220. }
  221. }
  222. return repos, count, nil
  223. }
  224. // FindUserAccessibleRepoIDs find all accessible repositories' ID by user's id
  225. func FindUserAccessibleRepoIDs(userID int64) ([]int64, error) {
  226. var accessCond builder.Cond = builder.Eq{"is_private": false}
  227. if userID > 0 {
  228. accessCond = accessCond.Or(
  229. builder.Eq{"owner_id": userID},
  230. builder.And(
  231. builder.Expr("id IN (SELECT repo_id FROM `access` WHERE access.user_id = ?)", userID),
  232. builder.Neq{"owner_id": userID},
  233. ),
  234. )
  235. }
  236. repoIDs := make([]int64, 0, 10)
  237. if err := x.
  238. Table("repository").
  239. Cols("id").
  240. Where(accessCond).
  241. Find(&repoIDs); err != nil {
  242. return nil, fmt.Errorf("FindUserAccesibleRepoIDs: %v", err)
  243. }
  244. return repoIDs, nil
  245. }