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

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