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

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