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

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