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.

251 lines
6.6 KiB

  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. )
  133. // SearchRepositoryByName takes keyword and part of repository name to search,
  134. // it returns results in given range and number of total results.
  135. func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, error) {
  136. if opts.Page <= 0 {
  137. opts.Page = 1
  138. }
  139. var cond = builder.NewCond()
  140. if !opts.Private {
  141. cond = cond.And(builder.Eq{"is_private": false})
  142. }
  143. var starred bool
  144. if opts.OwnerID > 0 {
  145. if opts.Starred {
  146. starred = true
  147. cond = builder.Eq{"star.uid": opts.OwnerID}
  148. } else {
  149. var accessCond = builder.NewCond()
  150. if opts.Collaborate != util.OptionalBoolTrue {
  151. accessCond = builder.Eq{"owner_id": opts.OwnerID}
  152. }
  153. if opts.Collaborate != util.OptionalBoolFalse {
  154. collaborateCond := builder.And(
  155. builder.Expr("id IN (SELECT repo_id FROM `access` WHERE access.user_id = ?)", opts.OwnerID),
  156. builder.Neq{"owner_id": opts.OwnerID})
  157. if !opts.Private {
  158. 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))
  159. }
  160. accessCond = accessCond.Or(collaborateCond)
  161. }
  162. if opts.AllPublic {
  163. accessCond = accessCond.Or(builder.Eq{"is_private": false})
  164. }
  165. cond = cond.And(accessCond)
  166. }
  167. }
  168. if opts.Keyword != "" {
  169. cond = cond.And(builder.Like{"lower_name", strings.ToLower(opts.Keyword)})
  170. }
  171. if opts.Fork != util.OptionalBoolNone {
  172. cond = cond.And(builder.Eq{"is_fork": opts.Fork == util.OptionalBoolTrue})
  173. }
  174. if opts.Mirror != util.OptionalBoolNone {
  175. cond = cond.And(builder.Eq{"is_mirror": opts.Mirror == util.OptionalBoolTrue})
  176. }
  177. if len(opts.OrderBy) == 0 {
  178. opts.OrderBy = SearchOrderByAlphabetically
  179. }
  180. sess := x.NewSession()
  181. defer sess.Close()
  182. if starred {
  183. sess.Join("INNER", "star", "star.repo_id = repository.id")
  184. }
  185. count, err := sess.
  186. Where(cond).
  187. Count(new(Repository))
  188. if err != nil {
  189. return nil, 0, fmt.Errorf("Count: %v", err)
  190. }
  191. // Set again after reset by Count()
  192. if starred {
  193. sess.Join("INNER", "star", "star.repo_id = repository.id")
  194. }
  195. repos := make(RepositoryList, 0, opts.PageSize)
  196. if err = sess.
  197. Where(cond).
  198. Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
  199. OrderBy(opts.OrderBy.String()).
  200. Find(&repos); err != nil {
  201. return nil, 0, fmt.Errorf("Repo: %v", err)
  202. }
  203. if !opts.IsProfile {
  204. if err = repos.loadAttributes(sess); err != nil {
  205. return nil, 0, fmt.Errorf("LoadAttributes: %v", err)
  206. }
  207. }
  208. return repos, count, nil
  209. }