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 6.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. // RepositoryList contains a list of repositories
  12. type RepositoryList []*Repository
  13. // RepositoryListOfMap make list from values of map
  14. func RepositoryListOfMap(repoMap map[int64]*Repository) RepositoryList {
  15. return RepositoryList(valuesRepository(repoMap))
  16. }
  17. func (repos RepositoryList) loadAttributes(e Engine) error {
  18. if len(repos) == 0 {
  19. return nil
  20. }
  21. // Load owners.
  22. set := make(map[int64]struct{})
  23. for i := range repos {
  24. set[repos[i].OwnerID] = struct{}{}
  25. }
  26. users := make(map[int64]*User, len(set))
  27. if err := e.
  28. Where("id > 0").
  29. In("id", keysInt64(set)).
  30. Find(&users); err != nil {
  31. return fmt.Errorf("find users: %v", err)
  32. }
  33. for i := range repos {
  34. repos[i].Owner = users[repos[i].OwnerID]
  35. }
  36. return nil
  37. }
  38. // LoadAttributes loads the attributes for the given RepositoryList
  39. func (repos RepositoryList) LoadAttributes() error {
  40. return repos.loadAttributes(x)
  41. }
  42. // MirrorRepositoryList contains the mirror repositories
  43. type MirrorRepositoryList []*Repository
  44. func (repos MirrorRepositoryList) loadAttributes(e Engine) error {
  45. if len(repos) == 0 {
  46. return nil
  47. }
  48. // Load mirrors.
  49. repoIDs := make([]int64, 0, len(repos))
  50. for i := range repos {
  51. if !repos[i].IsMirror {
  52. continue
  53. }
  54. repoIDs = append(repoIDs, repos[i].ID)
  55. }
  56. mirrors := make([]*Mirror, 0, len(repoIDs))
  57. if err := e.
  58. Where("id > 0").
  59. In("repo_id", repoIDs).
  60. Find(&mirrors); err != nil {
  61. return fmt.Errorf("find mirrors: %v", err)
  62. }
  63. set := make(map[int64]*Mirror)
  64. for i := range mirrors {
  65. set[mirrors[i].RepoID] = mirrors[i]
  66. }
  67. for i := range repos {
  68. repos[i].Mirror = set[repos[i].ID]
  69. }
  70. return nil
  71. }
  72. // LoadAttributes loads the attributes for the given MirrorRepositoryList
  73. func (repos MirrorRepositoryList) LoadAttributes() error {
  74. return repos.loadAttributes(x)
  75. }
  76. // SearchRepoOptions holds the search options
  77. type SearchRepoOptions struct {
  78. Keyword string
  79. OwnerID int64
  80. OrderBy SearchOrderBy
  81. Private bool // Include private repositories in results
  82. Starred bool
  83. Page int
  84. IsProfile bool
  85. AllPublic bool // Include also all public repositories
  86. PageSize int // Can be smaller than or equal to setting.ExplorePagingNum
  87. // None -> include collaborative AND non-collaborative
  88. // True -> include just collaborative
  89. // False -> incude just non-collaborative
  90. Collaborate util.OptionalBool
  91. // None -> include forks AND non-forks
  92. // True -> include just forks
  93. // False -> include just non-forks
  94. Fork util.OptionalBool
  95. // None -> include mirrors AND non-mirrors
  96. // True -> include just mirrors
  97. // False -> include just non-mirrors
  98. Mirror util.OptionalBool
  99. }
  100. //SearchOrderBy is used to sort the result
  101. type SearchOrderBy string
  102. func (s SearchOrderBy) String() string {
  103. return string(s)
  104. }
  105. // Strings for sorting result
  106. const (
  107. SearchOrderByAlphabetically SearchOrderBy = "name ASC"
  108. SearchOrderByAlphabeticallyReverse = "name DESC"
  109. SearchOrderByLeastUpdated = "updated_unix ASC"
  110. SearchOrderByRecentUpdated = "updated_unix DESC"
  111. SearchOrderByOldest = "created_unix ASC"
  112. SearchOrderByNewest = "created_unix DESC"
  113. SearchOrderBySize = "size ASC"
  114. SearchOrderBySizeReverse = "size DESC"
  115. SearchOrderByID = "id ASC"
  116. SearchOrderByIDReverse = "id DESC"
  117. )
  118. // SearchRepositoryByName takes keyword and part of repository name to search,
  119. // it returns results in given range and number of total results.
  120. func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, error) {
  121. if opts.Page <= 0 {
  122. opts.Page = 1
  123. }
  124. var cond = builder.NewCond()
  125. if !opts.Private {
  126. cond = cond.And(builder.Eq{"is_private": false})
  127. }
  128. var starred bool
  129. if opts.OwnerID > 0 {
  130. if opts.Starred {
  131. starred = true
  132. cond = builder.Eq{"star.uid": opts.OwnerID}
  133. } else {
  134. var accessCond = builder.NewCond()
  135. if opts.Collaborate != util.OptionalBoolTrue {
  136. accessCond = builder.Eq{"owner_id": opts.OwnerID}
  137. }
  138. if opts.Collaborate != util.OptionalBoolFalse {
  139. collaborateCond := builder.And(
  140. builder.Expr("id IN (SELECT repo_id FROM `access` WHERE access.user_id = ?)", opts.OwnerID),
  141. builder.Neq{"owner_id": opts.OwnerID})
  142. if !opts.Private {
  143. 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))
  144. }
  145. accessCond = accessCond.Or(collaborateCond)
  146. }
  147. if opts.AllPublic {
  148. accessCond = accessCond.Or(builder.Eq{"is_private": false})
  149. }
  150. cond = cond.And(accessCond)
  151. }
  152. }
  153. if opts.Keyword != "" {
  154. cond = cond.And(builder.Like{"lower_name", strings.ToLower(opts.Keyword)})
  155. }
  156. if opts.Fork != util.OptionalBoolNone {
  157. cond = cond.And(builder.Eq{"is_fork": opts.Fork == util.OptionalBoolTrue})
  158. }
  159. if opts.Mirror != util.OptionalBoolNone {
  160. cond = cond.And(builder.Eq{"is_mirror": opts.Mirror == util.OptionalBoolTrue})
  161. }
  162. if len(opts.OrderBy) == 0 {
  163. opts.OrderBy = SearchOrderByAlphabetically
  164. }
  165. sess := x.NewSession()
  166. defer sess.Close()
  167. if starred {
  168. sess.Join("INNER", "star", "star.repo_id = repository.id")
  169. }
  170. count, err := sess.
  171. Where(cond).
  172. Count(new(Repository))
  173. if err != nil {
  174. return nil, 0, fmt.Errorf("Count: %v", err)
  175. }
  176. // Set again after reset by Count()
  177. if starred {
  178. sess.Join("INNER", "star", "star.repo_id = repository.id")
  179. }
  180. repos := make(RepositoryList, 0, opts.PageSize)
  181. if err = sess.
  182. Where(cond).
  183. Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
  184. OrderBy(opts.OrderBy.String()).
  185. Find(&repos); err != nil {
  186. return nil, 0, fmt.Errorf("Repo: %v", err)
  187. }
  188. if !opts.IsProfile {
  189. if err = repos.loadAttributes(sess); err != nil {
  190. return nil, 0, fmt.Errorf("LoadAttributes: %v", err)
  191. }
  192. }
  193. return repos, count, nil
  194. }