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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. type SearchRepoOptions struct {
  74. Keyword string
  75. OwnerID int64
  76. Searcher *User //ID of the person who's seeking
  77. OrderBy string
  78. Private bool // Include private repositories in results
  79. Starred bool
  80. Page int
  81. IsProfile bool
  82. PageSize int // Can be smaller than or equal to setting.ExplorePagingNum
  83. }
  84. // SearchRepositoryByName takes keyword and part of repository name to search,
  85. // it returns results in given range and number of total results.
  86. func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, count int64, err error) {
  87. var (
  88. sess *xorm.Session
  89. cond = builder.NewCond()
  90. )
  91. if len(opts.Keyword) == 0 {
  92. return repos, 0, nil
  93. }
  94. opts.Keyword = strings.ToLower(opts.Keyword)
  95. if opts.Page <= 0 {
  96. opts.Page = 1
  97. }
  98. repos = make([]*Repository, 0, opts.PageSize)
  99. if opts.Starred && opts.OwnerID > 0 {
  100. cond = builder.Eq{
  101. "star.uid": opts.OwnerID,
  102. }
  103. }
  104. cond = cond.And(builder.Like{"lower_name", opts.Keyword})
  105. // Append conditions
  106. if !opts.Starred && opts.OwnerID > 0 {
  107. cond = cond.And(builder.Eq{"owner_id": opts.OwnerID})
  108. }
  109. if !opts.Private {
  110. cond = cond.And(builder.Eq{"is_private": false})
  111. }
  112. if opts.Searcher != nil {
  113. var ownerIds []int64
  114. ownerIds = append(ownerIds, opts.Searcher.ID)
  115. err = opts.Searcher.GetOrganizations(true)
  116. if err != nil {
  117. return nil, 0, fmt.Errorf("Organization: %v", err)
  118. }
  119. for _, org := range opts.Searcher.Orgs {
  120. ownerIds = append(ownerIds, org.ID)
  121. }
  122. cond = cond.Or(builder.And(builder.Like{"lower_name", opts.Keyword}, builder.In("owner_id", ownerIds)))
  123. }
  124. if len(opts.OrderBy) == 0 {
  125. opts.OrderBy = "name ASC"
  126. }
  127. if opts.Starred && opts.OwnerID > 0 {
  128. sess = x.
  129. Join("INNER", "star", "star.repo_id = repository.id").
  130. Where(cond)
  131. count, err = x.
  132. Join("INNER", "star", "star.repo_id = repository.id").
  133. Where(cond).
  134. Count(new(Repository))
  135. if err != nil {
  136. return nil, 0, fmt.Errorf("Count: %v", err)
  137. }
  138. } else {
  139. sess = x.Where(cond)
  140. count, err = x.
  141. Where(cond).
  142. Count(new(Repository))
  143. if err != nil {
  144. return nil, 0, fmt.Errorf("Count: %v", err)
  145. }
  146. }
  147. if err = sess.
  148. Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
  149. OrderBy(opts.OrderBy).
  150. Find(&repos); err != nil {
  151. return nil, 0, fmt.Errorf("Repo: %v", err)
  152. }
  153. if !opts.IsProfile {
  154. if err = repos.loadAttributes(x); err != nil {
  155. return nil, 0, fmt.Errorf("LoadAttributes: %v", err)
  156. }
  157. }
  158. return
  159. }
  160. // Repositories returns all repositories
  161. func Repositories(opts *SearchRepoOptions) (_ RepositoryList, count int64, err error) {
  162. if len(opts.OrderBy) == 0 {
  163. opts.OrderBy = "id ASC"
  164. }
  165. repos := make(RepositoryList, 0, opts.PageSize)
  166. if err = x.
  167. Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
  168. OrderBy(opts.OrderBy).
  169. Find(&repos); err != nil {
  170. return nil, 0, fmt.Errorf("Repo: %v", err)
  171. }
  172. if err = repos.loadAttributes(x); err != nil {
  173. return nil, 0, fmt.Errorf("LoadAttributes: %v", err)
  174. }
  175. count = countRepositories(-1, opts.Private)
  176. return repos, count, nil
  177. }
  178. // GetRecentUpdatedRepositories returns the list of repositories that are recently updated.
  179. func GetRecentUpdatedRepositories(opts *SearchRepoOptions) (repos RepositoryList, _ int64, _ error) {
  180. var cond = builder.NewCond()
  181. if len(opts.OrderBy) == 0 {
  182. opts.OrderBy = "updated_unix DESC"
  183. }
  184. if !opts.Private {
  185. cond = builder.Eq{
  186. "is_private": false,
  187. }
  188. }
  189. if opts.Searcher != nil && !opts.Searcher.IsAdmin {
  190. var ownerIds []int64
  191. ownerIds = append(ownerIds, opts.Searcher.ID)
  192. err := opts.Searcher.GetOrganizations(true)
  193. if err != nil {
  194. return nil, 0, fmt.Errorf("Organization: %v", err)
  195. }
  196. for _, org := range opts.Searcher.Orgs {
  197. ownerIds = append(ownerIds, org.ID)
  198. }
  199. cond = cond.Or(builder.In("owner_id", ownerIds))
  200. }
  201. count, err := x.Where(cond).Count(new(Repository))
  202. if err != nil {
  203. return nil, 0, fmt.Errorf("Count: %v", err)
  204. }
  205. if err = x.Where(cond).
  206. Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
  207. Limit(opts.PageSize).
  208. OrderBy(opts.OrderBy).
  209. Find(&repos); err != nil {
  210. return nil, 0, fmt.Errorf("Repo: %v", err)
  211. }
  212. if err = repos.loadAttributes(x); err != nil {
  213. return nil, 0, fmt.Errorf("LoadAttributes: %v", err)
  214. }
  215. return repos, count, nil
  216. }