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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. "github.com/go-xorm/xorm"
  10. )
  11. // RepositoryList contains a list of repositories
  12. type RepositoryList []*Repository
  13. func (repos RepositoryList) loadAttributes(e Engine) error {
  14. if len(repos) == 0 {
  15. return nil
  16. }
  17. // Load owners.
  18. set := make(map[int64]struct{})
  19. for i := range repos {
  20. set[repos[i].OwnerID] = struct{}{}
  21. }
  22. users := make(map[int64]*User, len(set))
  23. if err := e.
  24. Where("id > 0").
  25. In("id", keysInt64(set)).
  26. Find(&users); err != nil {
  27. return fmt.Errorf("find users: %v", err)
  28. }
  29. for i := range repos {
  30. repos[i].Owner = users[repos[i].OwnerID]
  31. }
  32. return nil
  33. }
  34. // LoadAttributes loads the attributes for the given RepositoryList
  35. func (repos RepositoryList) LoadAttributes() error {
  36. return repos.loadAttributes(x)
  37. }
  38. // MirrorRepositoryList contains the mirror repositories
  39. type MirrorRepositoryList []*Repository
  40. func (repos MirrorRepositoryList) loadAttributes(e Engine) error {
  41. if len(repos) == 0 {
  42. return nil
  43. }
  44. // Load mirrors.
  45. repoIDs := make([]int64, 0, len(repos))
  46. for i := range repos {
  47. if !repos[i].IsMirror {
  48. continue
  49. }
  50. repoIDs = append(repoIDs, repos[i].ID)
  51. }
  52. mirrors := make([]*Mirror, 0, len(repoIDs))
  53. if err := e.
  54. Where("id > 0").
  55. In("repo_id", repoIDs).
  56. Find(&mirrors); err != nil {
  57. return fmt.Errorf("find mirrors: %v", err)
  58. }
  59. set := make(map[int64]*Mirror)
  60. for i := range mirrors {
  61. set[mirrors[i].RepoID] = mirrors[i]
  62. }
  63. for i := range repos {
  64. repos[i].Mirror = set[repos[i].ID]
  65. }
  66. return nil
  67. }
  68. // LoadAttributes loads the attributes for the given MirrorRepositoryList
  69. func (repos MirrorRepositoryList) LoadAttributes() error {
  70. return repos.loadAttributes(x)
  71. }
  72. // SearchRepoOptions holds the search options
  73. // swagger:parameters repoSearch
  74. type SearchRepoOptions struct {
  75. // Keyword to search
  76. //
  77. // in: query
  78. Keyword string `json:"q"`
  79. // Owner in we search search
  80. //
  81. // in: query
  82. OwnerID int64 `json:"uid"`
  83. Searcher *User `json:"-"` //ID of the person who's seeking
  84. OrderBy string `json:"-"`
  85. Private bool `json:"-"` // Include private repositories in results
  86. Starred bool `json:"-"`
  87. Page int `json:"-"`
  88. IsProfile bool `json:"-"`
  89. // Limit of result
  90. //
  91. // maximum: setting.ExplorePagingNum
  92. // in: query
  93. PageSize int `json:"limit"` // Can be smaller than or equal to setting.ExplorePagingNum
  94. }
  95. // SearchRepositoryByName takes keyword and part of repository name to search,
  96. // it returns results in given range and number of total results.
  97. func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, count int64, err error) {
  98. var (
  99. sess *xorm.Session
  100. cond = builder.NewCond()
  101. )
  102. opts.Keyword = strings.ToLower(opts.Keyword)
  103. if opts.Page <= 0 {
  104. opts.Page = 1
  105. }
  106. repos = make([]*Repository, 0, opts.PageSize)
  107. if opts.Starred && opts.OwnerID > 0 {
  108. cond = builder.Eq{
  109. "star.uid": opts.OwnerID,
  110. }
  111. }
  112. cond = cond.And(builder.Like{"lower_name", opts.Keyword})
  113. // Append conditions
  114. if !opts.Starred && opts.OwnerID > 0 {
  115. cond = cond.And(builder.Eq{"owner_id": opts.OwnerID})
  116. }
  117. if !opts.Private {
  118. cond = cond.And(builder.Eq{"is_private": false})
  119. }
  120. if opts.Searcher != nil {
  121. var ownerIds []int64
  122. ownerIds = append(ownerIds, opts.Searcher.ID)
  123. err = opts.Searcher.GetOrganizations(true)
  124. if err != nil {
  125. return nil, 0, fmt.Errorf("Organization: %v", err)
  126. }
  127. for _, org := range opts.Searcher.Orgs {
  128. ownerIds = append(ownerIds, org.ID)
  129. }
  130. cond = cond.Or(builder.And(builder.Like{"lower_name", opts.Keyword}, builder.In("owner_id", ownerIds)))
  131. }
  132. if len(opts.OrderBy) == 0 {
  133. opts.OrderBy = "name ASC"
  134. }
  135. if opts.Starred && opts.OwnerID > 0 {
  136. sess = x.
  137. Join("INNER", "star", "star.repo_id = repository.id").
  138. Where(cond)
  139. count, err = x.
  140. Join("INNER", "star", "star.repo_id = repository.id").
  141. Where(cond).
  142. Count(new(Repository))
  143. if err != nil {
  144. return nil, 0, fmt.Errorf("Count: %v", err)
  145. }
  146. } else {
  147. sess = x.Where(cond)
  148. count, err = x.
  149. Where(cond).
  150. Count(new(Repository))
  151. if err != nil {
  152. return nil, 0, fmt.Errorf("Count: %v", err)
  153. }
  154. }
  155. if err = sess.
  156. Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
  157. OrderBy(opts.OrderBy).
  158. Find(&repos); err != nil {
  159. return nil, 0, fmt.Errorf("Repo: %v", err)
  160. }
  161. if !opts.IsProfile {
  162. if err = repos.loadAttributes(x); err != nil {
  163. return nil, 0, fmt.Errorf("LoadAttributes: %v", err)
  164. }
  165. }
  166. return
  167. }
  168. // Repositories returns all repositories
  169. func Repositories(opts *SearchRepoOptions) (_ RepositoryList, count int64, err error) {
  170. if len(opts.OrderBy) == 0 {
  171. opts.OrderBy = "id ASC"
  172. }
  173. repos := make(RepositoryList, 0, opts.PageSize)
  174. if err = x.
  175. Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
  176. OrderBy(opts.OrderBy).
  177. Find(&repos); err != nil {
  178. return nil, 0, fmt.Errorf("Repo: %v", err)
  179. }
  180. if err = repos.loadAttributes(x); err != nil {
  181. return nil, 0, fmt.Errorf("LoadAttributes: %v", err)
  182. }
  183. count = countRepositories(-1, opts.Private)
  184. return repos, count, nil
  185. }
  186. // GetRecentUpdatedRepositories returns the list of repositories that are recently updated.
  187. func GetRecentUpdatedRepositories(opts *SearchRepoOptions) (repos RepositoryList, _ int64, _ error) {
  188. var cond = builder.NewCond()
  189. if len(opts.OrderBy) == 0 {
  190. opts.OrderBy = "updated_unix DESC"
  191. }
  192. if !opts.Private {
  193. cond = builder.Eq{
  194. "is_private": false,
  195. }
  196. }
  197. if opts.Searcher != nil && !opts.Searcher.IsAdmin {
  198. var ownerIds []int64
  199. ownerIds = append(ownerIds, opts.Searcher.ID)
  200. err := opts.Searcher.GetOrganizations(true)
  201. if err != nil {
  202. return nil, 0, fmt.Errorf("Organization: %v", err)
  203. }
  204. for _, org := range opts.Searcher.Orgs {
  205. ownerIds = append(ownerIds, org.ID)
  206. }
  207. cond = cond.Or(builder.In("owner_id", ownerIds))
  208. }
  209. count, err := x.Where(cond).Count(new(Repository))
  210. if err != nil {
  211. return nil, 0, fmt.Errorf("Count: %v", err)
  212. }
  213. if err = x.Where(cond).
  214. Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
  215. Limit(opts.PageSize).
  216. OrderBy(opts.OrderBy).
  217. Find(&repos); err != nil {
  218. return nil, 0, fmt.Errorf("Repo: %v", err)
  219. }
  220. if err = repos.loadAttributes(x); err != nil {
  221. return nil, 0, fmt.Errorf("LoadAttributes: %v", err)
  222. }
  223. return repos, count, nil
  224. }