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.3 kB

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