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

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