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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. if len(opts.Keyword) == 0 {
  103. return repos, 0, nil
  104. }
  105. opts.Keyword = strings.ToLower(opts.Keyword)
  106. if opts.Page <= 0 {
  107. opts.Page = 1
  108. }
  109. repos = make([]*Repository, 0, opts.PageSize)
  110. if opts.Starred && opts.OwnerID > 0 {
  111. cond = builder.Eq{
  112. "star.uid": opts.OwnerID,
  113. }
  114. }
  115. cond = cond.And(builder.Like{"lower_name", opts.Keyword})
  116. // Append conditions
  117. if !opts.Starred && opts.OwnerID > 0 {
  118. cond = cond.And(builder.Eq{"owner_id": opts.OwnerID})
  119. }
  120. if !opts.Private {
  121. cond = cond.And(builder.Eq{"is_private": false})
  122. }
  123. if opts.Searcher != nil {
  124. var ownerIds []int64
  125. ownerIds = append(ownerIds, opts.Searcher.ID)
  126. err = opts.Searcher.GetOrganizations(true)
  127. if err != nil {
  128. return nil, 0, fmt.Errorf("Organization: %v", err)
  129. }
  130. for _, org := range opts.Searcher.Orgs {
  131. ownerIds = append(ownerIds, org.ID)
  132. }
  133. cond = cond.Or(builder.And(builder.Like{"lower_name", opts.Keyword}, builder.In("owner_id", ownerIds)))
  134. }
  135. if len(opts.OrderBy) == 0 {
  136. opts.OrderBy = "name ASC"
  137. }
  138. if opts.Starred && opts.OwnerID > 0 {
  139. sess = x.
  140. Join("INNER", "star", "star.repo_id = repository.id").
  141. Where(cond)
  142. count, err = x.
  143. Join("INNER", "star", "star.repo_id = repository.id").
  144. Where(cond).
  145. Count(new(Repository))
  146. if err != nil {
  147. return nil, 0, fmt.Errorf("Count: %v", err)
  148. }
  149. } else {
  150. sess = x.Where(cond)
  151. count, err = x.
  152. Where(cond).
  153. Count(new(Repository))
  154. if err != nil {
  155. return nil, 0, fmt.Errorf("Count: %v", err)
  156. }
  157. }
  158. if err = sess.
  159. Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
  160. OrderBy(opts.OrderBy).
  161. Find(&repos); err != nil {
  162. return nil, 0, fmt.Errorf("Repo: %v", err)
  163. }
  164. if !opts.IsProfile {
  165. if err = repos.loadAttributes(x); err != nil {
  166. return nil, 0, fmt.Errorf("LoadAttributes: %v", err)
  167. }
  168. }
  169. return
  170. }
  171. // Repositories returns all repositories
  172. func Repositories(opts *SearchRepoOptions) (_ RepositoryList, count int64, err error) {
  173. if len(opts.OrderBy) == 0 {
  174. opts.OrderBy = "id ASC"
  175. }
  176. repos := make(RepositoryList, 0, opts.PageSize)
  177. if err = x.
  178. Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
  179. OrderBy(opts.OrderBy).
  180. Find(&repos); err != nil {
  181. return nil, 0, fmt.Errorf("Repo: %v", err)
  182. }
  183. if err = repos.loadAttributes(x); err != nil {
  184. return nil, 0, fmt.Errorf("LoadAttributes: %v", err)
  185. }
  186. count = countRepositories(-1, opts.Private)
  187. return repos, count, nil
  188. }
  189. // GetRecentUpdatedRepositories returns the list of repositories that are recently updated.
  190. func GetRecentUpdatedRepositories(opts *SearchRepoOptions) (repos RepositoryList, _ int64, _ error) {
  191. var cond = builder.NewCond()
  192. if len(opts.OrderBy) == 0 {
  193. opts.OrderBy = "updated_unix DESC"
  194. }
  195. if !opts.Private {
  196. cond = builder.Eq{
  197. "is_private": false,
  198. }
  199. }
  200. if opts.Searcher != nil && !opts.Searcher.IsAdmin {
  201. var ownerIds []int64
  202. ownerIds = append(ownerIds, opts.Searcher.ID)
  203. err := opts.Searcher.GetOrganizations(true)
  204. if err != nil {
  205. return nil, 0, fmt.Errorf("Organization: %v", err)
  206. }
  207. for _, org := range opts.Searcher.Orgs {
  208. ownerIds = append(ownerIds, org.ID)
  209. }
  210. cond = cond.Or(builder.In("owner_id", ownerIds))
  211. }
  212. count, err := x.Where(cond).Count(new(Repository))
  213. if err != nil {
  214. return nil, 0, fmt.Errorf("Count: %v", err)
  215. }
  216. if err = x.Where(cond).
  217. Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
  218. Limit(opts.PageSize).
  219. OrderBy(opts.OrderBy).
  220. Find(&repos); err != nil {
  221. return nil, 0, fmt.Errorf("Repo: %v", err)
  222. }
  223. if err = repos.loadAttributes(x); err != nil {
  224. return nil, 0, fmt.Errorf("LoadAttributes: %v", err)
  225. }
  226. return repos, count, nil
  227. }