You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

repo_list.go 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "context"
  6. "fmt"
  7. "strings"
  8. "code.gitea.io/gitea/models/db"
  9. "code.gitea.io/gitea/models/perm"
  10. "code.gitea.io/gitea/models/unit"
  11. user_model "code.gitea.io/gitea/models/user"
  12. "code.gitea.io/gitea/modules/container"
  13. "code.gitea.io/gitea/modules/setting"
  14. "code.gitea.io/gitea/modules/structs"
  15. "code.gitea.io/gitea/modules/util"
  16. "xorm.io/builder"
  17. )
  18. // FindReposMapByIDs find repos as map
  19. func FindReposMapByIDs(repoIDs []int64, res map[int64]*Repository) error {
  20. return db.GetEngine(db.DefaultContext).In("id", repoIDs).Find(&res)
  21. }
  22. // RepositoryListDefaultPageSize is the default number of repositories
  23. // to load in memory when running administrative tasks on all (or almost
  24. // all) of them.
  25. // The number should be low enough to avoid filling up all RAM with
  26. // repository data...
  27. const RepositoryListDefaultPageSize = 64
  28. // RepositoryList contains a list of repositories
  29. type RepositoryList []*Repository
  30. func (repos RepositoryList) Len() int {
  31. return len(repos)
  32. }
  33. func (repos RepositoryList) Less(i, j int) bool {
  34. return repos[i].FullName() < repos[j].FullName()
  35. }
  36. func (repos RepositoryList) Swap(i, j int) {
  37. repos[i], repos[j] = repos[j], repos[i]
  38. }
  39. // ValuesRepository converts a repository map to a list
  40. // FIXME: Remove in favor of maps.values when MIN_GO_VERSION >= 1.18
  41. func ValuesRepository(m map[int64]*Repository) []*Repository {
  42. values := make([]*Repository, 0, len(m))
  43. for _, v := range m {
  44. values = append(values, v)
  45. }
  46. return values
  47. }
  48. // RepositoryListOfMap make list from values of map
  49. func RepositoryListOfMap(repoMap map[int64]*Repository) RepositoryList {
  50. return RepositoryList(ValuesRepository(repoMap))
  51. }
  52. func (repos RepositoryList) loadAttributes(ctx context.Context) error {
  53. if len(repos) == 0 {
  54. return nil
  55. }
  56. set := make(container.Set[int64])
  57. repoIDs := make([]int64, len(repos))
  58. for i := range repos {
  59. set.Add(repos[i].OwnerID)
  60. repoIDs[i] = repos[i].ID
  61. }
  62. // Load owners.
  63. users := make(map[int64]*user_model.User, len(set))
  64. if err := db.GetEngine(ctx).
  65. Where("id > 0").
  66. In("id", set.Values()).
  67. Find(&users); err != nil {
  68. return fmt.Errorf("find users: %w", err)
  69. }
  70. for i := range repos {
  71. repos[i].Owner = users[repos[i].OwnerID]
  72. }
  73. // Load primary language.
  74. stats := make(LanguageStatList, 0, len(repos))
  75. if err := db.GetEngine(ctx).
  76. Where("`is_primary` = ? AND `language` != ?", true, "other").
  77. In("`repo_id`", repoIDs).
  78. Find(&stats); err != nil {
  79. return fmt.Errorf("find primary languages: %w", err)
  80. }
  81. stats.LoadAttributes()
  82. for i := range repos {
  83. for _, st := range stats {
  84. if st.RepoID == repos[i].ID {
  85. repos[i].PrimaryLanguage = st
  86. break
  87. }
  88. }
  89. }
  90. return nil
  91. }
  92. // LoadAttributes loads the attributes for the given RepositoryList
  93. func (repos RepositoryList) LoadAttributes() error {
  94. return repos.loadAttributes(db.DefaultContext)
  95. }
  96. // SearchRepoOptions holds the search options
  97. type SearchRepoOptions struct {
  98. db.ListOptions
  99. Actor *user_model.User
  100. Keyword string
  101. OwnerID int64
  102. PriorityOwnerID int64
  103. TeamID int64
  104. OrderBy db.SearchOrderBy
  105. Private bool // Include private repositories in results
  106. StarredByID int64
  107. WatchedByID int64
  108. AllPublic bool // Include also all public repositories of users and public organisations
  109. AllLimited bool // Include also all public repositories of limited organisations
  110. // None -> include public and private
  111. // True -> include just private
  112. // False -> include just public
  113. IsPrivate util.OptionalBool
  114. // None -> include collaborative AND non-collaborative
  115. // True -> include just collaborative
  116. // False -> include just non-collaborative
  117. Collaborate util.OptionalBool
  118. // None -> include forks AND non-forks
  119. // True -> include just forks
  120. // False -> include just non-forks
  121. Fork util.OptionalBool
  122. // None -> include templates AND non-templates
  123. // True -> include just templates
  124. // False -> include just non-templates
  125. Template util.OptionalBool
  126. // None -> include mirrors AND non-mirrors
  127. // True -> include just mirrors
  128. // False -> include just non-mirrors
  129. Mirror util.OptionalBool
  130. // None -> include archived AND non-archived
  131. // True -> include just archived
  132. // False -> include just non-archived
  133. Archived util.OptionalBool
  134. // only search topic name
  135. TopicOnly bool
  136. // only search repositories with specified primary language
  137. Language string
  138. // include description in keyword search
  139. IncludeDescription bool
  140. // None -> include has milestones AND has no milestone
  141. // True -> include just has milestones
  142. // False -> include just has no milestone
  143. HasMilestones util.OptionalBool
  144. // LowerNames represents valid lower names to restrict to
  145. LowerNames []string
  146. // When specified true, apply some filters over the conditions:
  147. // - Don't show forks, when opts.Fork is OptionalBoolNone.
  148. // - Do not display repositories that don't have a description, an icon and topics.
  149. OnlyShowRelevant bool
  150. }
  151. // SearchOrderBy is used to sort the result
  152. type SearchOrderBy string
  153. func (s SearchOrderBy) String() string {
  154. return string(s)
  155. }
  156. // Strings for sorting result
  157. const (
  158. SearchOrderByAlphabetically SearchOrderBy = "name ASC"
  159. SearchOrderByAlphabeticallyReverse SearchOrderBy = "name DESC"
  160. SearchOrderByLeastUpdated SearchOrderBy = "updated_unix ASC"
  161. SearchOrderByRecentUpdated SearchOrderBy = "updated_unix DESC"
  162. SearchOrderByOldest SearchOrderBy = "created_unix ASC"
  163. SearchOrderByNewest SearchOrderBy = "created_unix DESC"
  164. SearchOrderBySize SearchOrderBy = "size ASC"
  165. SearchOrderBySizeReverse SearchOrderBy = "size DESC"
  166. SearchOrderByID SearchOrderBy = "id ASC"
  167. SearchOrderByIDReverse SearchOrderBy = "id DESC"
  168. SearchOrderByStars SearchOrderBy = "num_stars ASC"
  169. SearchOrderByStarsReverse SearchOrderBy = "num_stars DESC"
  170. SearchOrderByForks SearchOrderBy = "num_forks ASC"
  171. SearchOrderByForksReverse SearchOrderBy = "num_forks DESC"
  172. )
  173. // UserOwnedRepoCond returns user ownered repositories
  174. func UserOwnedRepoCond(userID int64) builder.Cond {
  175. return builder.Eq{
  176. "repository.owner_id": userID,
  177. }
  178. }
  179. // UserAssignedRepoCond return user as assignee repositories list
  180. func UserAssignedRepoCond(id string, userID int64) builder.Cond {
  181. return builder.And(
  182. builder.Eq{
  183. "repository.is_private": false,
  184. },
  185. builder.In(id,
  186. builder.Select("issue.repo_id").From("issue_assignees").
  187. InnerJoin("issue", "issue.id = issue_assignees.issue_id").
  188. Where(builder.Eq{
  189. "issue_assignees.assignee_id": userID,
  190. }),
  191. ),
  192. )
  193. }
  194. // UserCreateIssueRepoCond return user created issues repositories list
  195. func UserCreateIssueRepoCond(id string, userID int64, isPull bool) builder.Cond {
  196. return builder.And(
  197. builder.Eq{
  198. "repository.is_private": false,
  199. },
  200. builder.In(id,
  201. builder.Select("issue.repo_id").From("issue").
  202. Where(builder.Eq{
  203. "issue.poster_id": userID,
  204. "issue.is_pull": isPull,
  205. }),
  206. ),
  207. )
  208. }
  209. // UserMentionedRepoCond return user metinoed repositories list
  210. func UserMentionedRepoCond(id string, userID int64) builder.Cond {
  211. return builder.And(
  212. builder.Eq{
  213. "repository.is_private": false,
  214. },
  215. builder.In(id,
  216. builder.Select("issue.repo_id").From("issue_user").
  217. InnerJoin("issue", "issue.id = issue_user.issue_id").
  218. Where(builder.Eq{
  219. "issue_user.is_mentioned": true,
  220. "issue_user.uid": userID,
  221. }),
  222. ),
  223. )
  224. }
  225. // UserAccessRepoCond returns a condition for selecting all repositories a user has unit independent access to
  226. func UserAccessRepoCond(idStr string, userID int64) builder.Cond {
  227. return builder.In(idStr, builder.Select("repo_id").
  228. From("`access`").
  229. Where(builder.And(
  230. builder.Eq{"`access`.user_id": userID},
  231. builder.Gt{"`access`.mode": int(perm.AccessModeNone)},
  232. )),
  233. )
  234. }
  235. // userCollaborationRepoCond returns a condition for selecting all repositories a user is collaborator in
  236. func UserCollaborationRepoCond(idStr string, userID int64) builder.Cond {
  237. return builder.In(idStr, builder.Select("repo_id").
  238. From("`collaboration`").
  239. Where(builder.And(
  240. builder.Eq{"`collaboration`.user_id": userID},
  241. )),
  242. )
  243. }
  244. // UserOrgTeamRepoCond selects repos that the given user has access to through team membership
  245. func UserOrgTeamRepoCond(idStr string, userID int64) builder.Cond {
  246. return builder.In(idStr, userOrgTeamRepoBuilder(userID))
  247. }
  248. // userOrgTeamRepoBuilder returns repo ids where user's teams can access.
  249. func userOrgTeamRepoBuilder(userID int64) *builder.Builder {
  250. return builder.Select("`team_repo`.repo_id").
  251. From("team_repo").
  252. Join("INNER", "team_user", "`team_user`.team_id = `team_repo`.team_id").
  253. Where(builder.Eq{"`team_user`.uid": userID})
  254. }
  255. // userOrgTeamUnitRepoBuilder returns repo ids where user's teams can access the special unit.
  256. func userOrgTeamUnitRepoBuilder(userID int64, unitType unit.Type) *builder.Builder {
  257. return userOrgTeamRepoBuilder(userID).
  258. Join("INNER", "team_unit", "`team_unit`.team_id = `team_repo`.team_id").
  259. Where(builder.Eq{"`team_unit`.`type`": unitType}).
  260. And(builder.Gt{"`team_unit`.`access_mode`": int(perm.AccessModeNone)})
  261. }
  262. // userOrgTeamUnitRepoCond returns a condition to select repo ids where user's teams can access the special unit.
  263. func userOrgTeamUnitRepoCond(idStr string, userID int64, unitType unit.Type) builder.Cond {
  264. return builder.In(idStr, userOrgTeamUnitRepoBuilder(userID, unitType))
  265. }
  266. // UserOrgUnitRepoCond selects repos that the given user has access to through org and the special unit
  267. func UserOrgUnitRepoCond(idStr string, userID, orgID int64, unitType unit.Type) builder.Cond {
  268. return builder.In(idStr,
  269. userOrgTeamUnitRepoBuilder(userID, unitType).
  270. And(builder.Eq{"`team_unit`.org_id": orgID}),
  271. )
  272. }
  273. // userOrgPublicRepoCond returns the condition that one user could access all public repositories in organizations
  274. func userOrgPublicRepoCond(userID int64) builder.Cond {
  275. return builder.And(
  276. builder.Eq{"`repository`.is_private": false},
  277. builder.In("`repository`.owner_id",
  278. builder.Select("`org_user`.org_id").
  279. From("org_user").
  280. Where(builder.Eq{"`org_user`.uid": userID}),
  281. ),
  282. )
  283. }
  284. // userOrgPublicRepoCondPrivate returns the condition that one user could access all public repositories in private organizations
  285. func userOrgPublicRepoCondPrivate(userID int64) builder.Cond {
  286. return builder.And(
  287. builder.Eq{"`repository`.is_private": false},
  288. builder.In("`repository`.owner_id",
  289. builder.Select("`org_user`.org_id").
  290. From("org_user").
  291. Join("INNER", "`user`", "`user`.id = `org_user`.org_id").
  292. Where(builder.Eq{
  293. "`org_user`.uid": userID,
  294. "`user`.`type`": user_model.UserTypeOrganization,
  295. "`user`.visibility": structs.VisibleTypePrivate,
  296. }),
  297. ),
  298. )
  299. }
  300. // UserOrgPublicUnitRepoCond returns the condition that one user could access all public repositories in the special organization
  301. func UserOrgPublicUnitRepoCond(userID, orgID int64) builder.Cond {
  302. return userOrgPublicRepoCond(userID).
  303. And(builder.Eq{"`repository`.owner_id": orgID})
  304. }
  305. // SearchRepositoryCondition creates a query condition according search repository options
  306. func SearchRepositoryCondition(opts *SearchRepoOptions) builder.Cond {
  307. cond := builder.NewCond()
  308. if opts.Private {
  309. if opts.Actor != nil && !opts.Actor.IsAdmin && opts.Actor.ID != opts.OwnerID {
  310. // OK we're in the context of a User
  311. cond = cond.And(AccessibleRepositoryCondition(opts.Actor, unit.TypeInvalid))
  312. }
  313. } else {
  314. // Not looking at private organisations and users
  315. // We should be able to see all non-private repositories that
  316. // isn't in a private or limited organisation.
  317. cond = cond.And(
  318. builder.Eq{"is_private": false},
  319. builder.NotIn("owner_id", builder.Select("id").From("`user`").Where(
  320. builder.Or(builder.Eq{"visibility": structs.VisibleTypeLimited}, builder.Eq{"visibility": structs.VisibleTypePrivate}),
  321. )))
  322. }
  323. if opts.IsPrivate != util.OptionalBoolNone {
  324. cond = cond.And(builder.Eq{"is_private": opts.IsPrivate.IsTrue()})
  325. }
  326. if opts.Template != util.OptionalBoolNone {
  327. cond = cond.And(builder.Eq{"is_template": opts.Template == util.OptionalBoolTrue})
  328. }
  329. // Restrict to starred repositories
  330. if opts.StarredByID > 0 {
  331. cond = cond.And(builder.In("id", builder.Select("repo_id").From("star").Where(builder.Eq{"uid": opts.StarredByID})))
  332. }
  333. // Restrict to watched repositories
  334. if opts.WatchedByID > 0 {
  335. cond = cond.And(builder.In("id", builder.Select("repo_id").From("watch").Where(builder.Eq{"user_id": opts.WatchedByID})))
  336. }
  337. // Restrict repositories to those the OwnerID owns or contributes to as per opts.Collaborate
  338. if opts.OwnerID > 0 {
  339. accessCond := builder.NewCond()
  340. if opts.Collaborate != util.OptionalBoolTrue {
  341. accessCond = builder.Eq{"owner_id": opts.OwnerID}
  342. }
  343. if opts.Collaborate != util.OptionalBoolFalse {
  344. // A Collaboration is:
  345. collaborateCond := builder.And(
  346. // 1. Repository we don't own
  347. builder.Neq{"owner_id": opts.OwnerID},
  348. // 2. But we can see because of:
  349. builder.Or(
  350. // A. We have unit independent access
  351. UserAccessRepoCond("`repository`.id", opts.OwnerID),
  352. // B. We are in a team for
  353. UserOrgTeamRepoCond("`repository`.id", opts.OwnerID),
  354. // C. Public repositories in organizations that we are member of
  355. userOrgPublicRepoCondPrivate(opts.OwnerID),
  356. ),
  357. )
  358. if !opts.Private {
  359. collaborateCond = collaborateCond.And(builder.Expr("owner_id NOT IN (SELECT org_id FROM org_user WHERE org_user.uid = ? AND org_user.is_public = ?)", opts.OwnerID, false))
  360. }
  361. accessCond = accessCond.Or(collaborateCond)
  362. }
  363. if opts.AllPublic {
  364. accessCond = accessCond.Or(builder.Eq{"is_private": false}.And(builder.In("owner_id", builder.Select("`user`.id").From("`user`").Where(builder.Eq{"`user`.visibility": structs.VisibleTypePublic}))))
  365. }
  366. if opts.AllLimited {
  367. accessCond = accessCond.Or(builder.Eq{"is_private": false}.And(builder.In("owner_id", builder.Select("`user`.id").From("`user`").Where(builder.Eq{"`user`.visibility": structs.VisibleTypeLimited}))))
  368. }
  369. cond = cond.And(accessCond)
  370. }
  371. if opts.TeamID > 0 {
  372. cond = cond.And(builder.In("`repository`.id", builder.Select("`team_repo`.repo_id").From("team_repo").Where(builder.Eq{"`team_repo`.team_id": opts.TeamID})))
  373. }
  374. if opts.Keyword != "" {
  375. // separate keyword
  376. subQueryCond := builder.NewCond()
  377. for _, v := range strings.Split(opts.Keyword, ",") {
  378. if opts.TopicOnly {
  379. subQueryCond = subQueryCond.Or(builder.Eq{"topic.name": strings.ToLower(v)})
  380. } else {
  381. subQueryCond = subQueryCond.Or(builder.Like{"topic.name", strings.ToLower(v)})
  382. }
  383. }
  384. subQuery := builder.Select("repo_topic.repo_id").From("repo_topic").
  385. Join("INNER", "topic", "topic.id = repo_topic.topic_id").
  386. Where(subQueryCond).
  387. GroupBy("repo_topic.repo_id")
  388. keywordCond := builder.In("id", subQuery)
  389. if !opts.TopicOnly {
  390. likes := builder.NewCond()
  391. for _, v := range strings.Split(opts.Keyword, ",") {
  392. likes = likes.Or(builder.Like{"lower_name", strings.ToLower(v)})
  393. // If the string looks like "org/repo", match against that pattern too
  394. if opts.TeamID == 0 && strings.Count(opts.Keyword, "/") == 1 {
  395. pieces := strings.Split(opts.Keyword, "/")
  396. ownerName := pieces[0]
  397. repoName := pieces[1]
  398. likes = likes.Or(builder.And(builder.Like{"owner_name", strings.ToLower(ownerName)}, builder.Like{"lower_name", strings.ToLower(repoName)}))
  399. }
  400. if opts.IncludeDescription {
  401. likes = likes.Or(builder.Like{"LOWER(description)", strings.ToLower(v)})
  402. }
  403. }
  404. keywordCond = keywordCond.Or(likes)
  405. }
  406. cond = cond.And(keywordCond)
  407. }
  408. if opts.Language != "" {
  409. cond = cond.And(builder.In("id", builder.
  410. Select("repo_id").
  411. From("language_stat").
  412. Where(builder.Eq{"language": opts.Language}).And(builder.Eq{"is_primary": true})))
  413. }
  414. if opts.Fork != util.OptionalBoolNone || opts.OnlyShowRelevant {
  415. if opts.OnlyShowRelevant && opts.Fork == util.OptionalBoolNone {
  416. cond = cond.And(builder.Eq{"is_fork": false})
  417. } else {
  418. cond = cond.And(builder.Eq{"is_fork": opts.Fork == util.OptionalBoolTrue})
  419. }
  420. }
  421. if opts.Mirror != util.OptionalBoolNone {
  422. cond = cond.And(builder.Eq{"is_mirror": opts.Mirror == util.OptionalBoolTrue})
  423. }
  424. if opts.Actor != nil && opts.Actor.IsRestricted {
  425. cond = cond.And(AccessibleRepositoryCondition(opts.Actor, unit.TypeInvalid))
  426. }
  427. if opts.Archived != util.OptionalBoolNone {
  428. cond = cond.And(builder.Eq{"is_archived": opts.Archived == util.OptionalBoolTrue})
  429. }
  430. switch opts.HasMilestones {
  431. case util.OptionalBoolTrue:
  432. cond = cond.And(builder.Gt{"num_milestones": 0})
  433. case util.OptionalBoolFalse:
  434. cond = cond.And(builder.Eq{"num_milestones": 0}.Or(builder.IsNull{"num_milestones"}))
  435. }
  436. if opts.OnlyShowRelevant {
  437. // Only show a repo that either has a topic or description.
  438. subQueryCond := builder.NewCond()
  439. // Topic checking. Topics are present.
  440. if setting.Database.UsePostgreSQL { // postgres stores the topics as json and not as text
  441. subQueryCond = subQueryCond.Or(builder.And(builder.NotNull{"topics"}, builder.Neq{"(topics)::text": "[]"}))
  442. } else {
  443. subQueryCond = subQueryCond.Or(builder.And(builder.Neq{"topics": "null"}, builder.Neq{"topics": "[]"}))
  444. }
  445. // Description checking. Description not empty.
  446. subQueryCond = subQueryCond.Or(builder.Neq{"description": ""})
  447. // Repo has a avatar.
  448. subQueryCond = subQueryCond.Or(builder.Neq{"avatar": ""})
  449. // Always hide repo's that are empty.
  450. subQueryCond = subQueryCond.And(builder.Eq{"is_empty": false})
  451. cond = cond.And(subQueryCond)
  452. }
  453. return cond
  454. }
  455. // SearchRepository returns repositories based on search options,
  456. // it returns results in given range and number of total results.
  457. func SearchRepository(ctx context.Context, opts *SearchRepoOptions) (RepositoryList, int64, error) {
  458. cond := SearchRepositoryCondition(opts)
  459. return SearchRepositoryByCondition(ctx, opts, cond, true)
  460. }
  461. // SearchRepositoryByCondition search repositories by condition
  462. func SearchRepositoryByCondition(ctx context.Context, opts *SearchRepoOptions, cond builder.Cond, loadAttributes bool) (RepositoryList, int64, error) {
  463. sess, count, err := searchRepositoryByCondition(ctx, opts, cond)
  464. if err != nil {
  465. return nil, 0, err
  466. }
  467. defaultSize := 50
  468. if opts.PageSize > 0 {
  469. defaultSize = opts.PageSize
  470. }
  471. repos := make(RepositoryList, 0, defaultSize)
  472. if err := sess.Find(&repos); err != nil {
  473. return nil, 0, fmt.Errorf("Repo: %w", err)
  474. }
  475. if opts.PageSize <= 0 {
  476. count = int64(len(repos))
  477. }
  478. if loadAttributes {
  479. if err := repos.loadAttributes(ctx); err != nil {
  480. return nil, 0, fmt.Errorf("LoadAttributes: %w", err)
  481. }
  482. }
  483. return repos, count, nil
  484. }
  485. func searchRepositoryByCondition(ctx context.Context, opts *SearchRepoOptions, cond builder.Cond) (db.Engine, int64, error) {
  486. if opts.Page <= 0 {
  487. opts.Page = 1
  488. }
  489. if len(opts.OrderBy) == 0 {
  490. opts.OrderBy = db.SearchOrderByAlphabetically
  491. }
  492. args := make([]interface{}, 0)
  493. if opts.PriorityOwnerID > 0 {
  494. opts.OrderBy = db.SearchOrderBy(fmt.Sprintf("CASE WHEN owner_id = ? THEN 0 ELSE owner_id END, %s", opts.OrderBy))
  495. args = append(args, opts.PriorityOwnerID)
  496. } else if strings.Count(opts.Keyword, "/") == 1 {
  497. // With "owner/repo" search times, prioritise results which match the owner field
  498. orgName := strings.Split(opts.Keyword, "/")[0]
  499. opts.OrderBy = db.SearchOrderBy(fmt.Sprintf("CASE WHEN owner_name LIKE ? THEN 0 ELSE 1 END, %s", opts.OrderBy))
  500. args = append(args, orgName)
  501. }
  502. sess := db.GetEngine(ctx)
  503. var count int64
  504. if opts.PageSize > 0 {
  505. var err error
  506. count, err = sess.
  507. Where(cond).
  508. Count(new(Repository))
  509. if err != nil {
  510. return nil, 0, fmt.Errorf("Count: %w", err)
  511. }
  512. }
  513. sess = sess.Where(cond).OrderBy(opts.OrderBy.String(), args...)
  514. if opts.PageSize > 0 {
  515. sess = sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
  516. }
  517. return sess, count, nil
  518. }
  519. // SearchRepositoryIDsByCondition search repository IDs by given condition.
  520. func SearchRepositoryIDsByCondition(ctx context.Context, cond builder.Cond) ([]int64, error) {
  521. repoIDs := make([]int64, 0, 10)
  522. return repoIDs, db.GetEngine(ctx).
  523. Table("repository").
  524. Cols("id").
  525. Where(cond).
  526. Find(&repoIDs)
  527. }
  528. // AccessibleRepositoryCondition takes a user a returns a condition for checking if a repository is accessible
  529. func AccessibleRepositoryCondition(user *user_model.User, unitType unit.Type) builder.Cond {
  530. cond := builder.NewCond()
  531. if user == nil || !user.IsRestricted || user.ID <= 0 {
  532. orgVisibilityLimit := []structs.VisibleType{structs.VisibleTypePrivate}
  533. if user == nil || user.ID <= 0 {
  534. orgVisibilityLimit = append(orgVisibilityLimit, structs.VisibleTypeLimited)
  535. }
  536. // 1. Be able to see all non-private repositories that either:
  537. cond = cond.Or(builder.And(
  538. builder.Eq{"`repository`.is_private": false},
  539. // 2. Aren't in an private organisation or limited organisation if we're not logged in
  540. builder.NotIn("`repository`.owner_id", builder.Select("id").From("`user`").Where(
  541. builder.And(
  542. builder.Eq{"type": user_model.UserTypeOrganization},
  543. builder.In("visibility", orgVisibilityLimit)),
  544. ))))
  545. }
  546. if user != nil {
  547. // 2. Be able to see all repositories that we have unit independent access to
  548. // 3. Be able to see all repositories through team membership(s)
  549. if unitType == unit.TypeInvalid {
  550. // Regardless of UnitType
  551. cond = cond.Or(
  552. UserAccessRepoCond("`repository`.id", user.ID),
  553. UserOrgTeamRepoCond("`repository`.id", user.ID),
  554. )
  555. } else {
  556. // For a specific UnitType
  557. cond = cond.Or(
  558. UserCollaborationRepoCond("`repository`.id", user.ID),
  559. userOrgTeamUnitRepoCond("`repository`.id", user.ID, unitType),
  560. )
  561. }
  562. cond = cond.Or(
  563. // 4. Repositories that we directly own
  564. builder.Eq{"`repository`.owner_id": user.ID},
  565. // 5. Be able to see all public repos in private organizations that we are an org_user of
  566. userOrgPublicRepoCond(user.ID),
  567. )
  568. }
  569. return cond
  570. }
  571. // SearchRepositoryByName takes keyword and part of repository name to search,
  572. // it returns results in given range and number of total results.
  573. func SearchRepositoryByName(ctx context.Context, opts *SearchRepoOptions) (RepositoryList, int64, error) {
  574. opts.IncludeDescription = false
  575. return SearchRepository(ctx, opts)
  576. }
  577. // SearchRepositoryIDs takes keyword and part of repository name to search,
  578. // it returns results in given range and number of total results.
  579. func SearchRepositoryIDs(opts *SearchRepoOptions) ([]int64, int64, error) {
  580. opts.IncludeDescription = false
  581. cond := SearchRepositoryCondition(opts)
  582. sess, count, err := searchRepositoryByCondition(db.DefaultContext, opts, cond)
  583. if err != nil {
  584. return nil, 0, err
  585. }
  586. defaultSize := 50
  587. if opts.PageSize > 0 {
  588. defaultSize = opts.PageSize
  589. }
  590. ids := make([]int64, 0, defaultSize)
  591. err = sess.Select("id").Table("repository").Find(&ids)
  592. if opts.PageSize <= 0 {
  593. count = int64(len(ids))
  594. }
  595. return ids, count, err
  596. }
  597. // AccessibleRepoIDsQuery queries accessible repository ids. Usable as a subquery wherever repo ids need to be filtered.
  598. func AccessibleRepoIDsQuery(user *user_model.User) *builder.Builder {
  599. // NB: Please note this code needs to still work if user is nil
  600. return builder.Select("id").From("repository").Where(AccessibleRepositoryCondition(user, unit.TypeInvalid))
  601. }
  602. // FindUserCodeAccessibleRepoIDs finds all at Code level accessible repositories' ID by the user's id
  603. func FindUserCodeAccessibleRepoIDs(ctx context.Context, user *user_model.User) ([]int64, error) {
  604. return SearchRepositoryIDsByCondition(ctx, AccessibleRepositoryCondition(user, unit.TypeCode))
  605. }
  606. // FindUserCodeAccessibleOwnerRepoIDs finds all repository IDs for the given owner whose code the user can see.
  607. func FindUserCodeAccessibleOwnerRepoIDs(ctx context.Context, ownerID int64, user *user_model.User) ([]int64, error) {
  608. return SearchRepositoryIDsByCondition(ctx, builder.NewCond().And(
  609. builder.Eq{"owner_id": ownerID},
  610. AccessibleRepositoryCondition(user, unit.TypeCode),
  611. ))
  612. }
  613. // GetUserRepositories returns a list of repositories of given user.
  614. func GetUserRepositories(opts *SearchRepoOptions) (RepositoryList, int64, error) {
  615. if len(opts.OrderBy) == 0 {
  616. opts.OrderBy = "updated_unix DESC"
  617. }
  618. cond := builder.NewCond()
  619. if opts.Actor == nil {
  620. return nil, 0, util.NewInvalidArgumentErrorf("GetUserRepositories: Actor is needed but not given")
  621. }
  622. cond = cond.And(builder.Eq{"owner_id": opts.Actor.ID})
  623. if !opts.Private {
  624. cond = cond.And(builder.Eq{"is_private": false})
  625. }
  626. if opts.LowerNames != nil && len(opts.LowerNames) > 0 {
  627. cond = cond.And(builder.In("lower_name", opts.LowerNames))
  628. }
  629. sess := db.GetEngine(db.DefaultContext)
  630. count, err := sess.Where(cond).Count(new(Repository))
  631. if err != nil {
  632. return nil, 0, fmt.Errorf("Count: %w", err)
  633. }
  634. sess = sess.Where(cond).OrderBy(opts.OrderBy.String())
  635. repos := make(RepositoryList, 0, opts.PageSize)
  636. return repos, count, db.SetSessionPagination(sess, opts).Find(&repos)
  637. }