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 7.0 kB

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