您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

repo_list.go 8.4KB

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