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.

user_repo.go 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "context"
  6. "code.gitea.io/gitea/models/db"
  7. "code.gitea.io/gitea/models/perm"
  8. "code.gitea.io/gitea/models/unit"
  9. user_model "code.gitea.io/gitea/models/user"
  10. "code.gitea.io/gitea/modules/container"
  11. api "code.gitea.io/gitea/modules/structs"
  12. "xorm.io/builder"
  13. )
  14. // GetStarredRepos returns the repos starred by a particular user
  15. func GetStarredRepos(ctx context.Context, userID int64, private bool, listOptions db.ListOptions) ([]*Repository, error) {
  16. sess := db.GetEngine(ctx).
  17. Where("star.uid=?", userID).
  18. Join("LEFT", "star", "`repository`.id=`star`.repo_id")
  19. if !private {
  20. sess = sess.And("is_private=?", false)
  21. }
  22. if listOptions.Page != 0 {
  23. sess = db.SetSessionPagination(sess, &listOptions)
  24. repos := make([]*Repository, 0, listOptions.PageSize)
  25. return repos, sess.Find(&repos)
  26. }
  27. repos := make([]*Repository, 0, 10)
  28. return repos, sess.Find(&repos)
  29. }
  30. // GetWatchedRepos returns the repos watched by a particular user
  31. func GetWatchedRepos(ctx context.Context, userID int64, private bool, listOptions db.ListOptions) ([]*Repository, int64, error) {
  32. sess := db.GetEngine(ctx).
  33. Where("watch.user_id=?", userID).
  34. And("`watch`.mode<>?", WatchModeDont).
  35. Join("LEFT", "watch", "`repository`.id=`watch`.repo_id")
  36. if !private {
  37. sess = sess.And("is_private=?", false)
  38. }
  39. if listOptions.Page != 0 {
  40. sess = db.SetSessionPagination(sess, &listOptions)
  41. repos := make([]*Repository, 0, listOptions.PageSize)
  42. total, err := sess.FindAndCount(&repos)
  43. return repos, total, err
  44. }
  45. repos := make([]*Repository, 0, 10)
  46. total, err := sess.FindAndCount(&repos)
  47. return repos, total, err
  48. }
  49. // GetRepoAssignees returns all users that have write access and can be assigned to issues
  50. // of the repository,
  51. func GetRepoAssignees(ctx context.Context, repo *Repository) (_ []*user_model.User, err error) {
  52. if err = repo.LoadOwner(ctx); err != nil {
  53. return nil, err
  54. }
  55. e := db.GetEngine(ctx)
  56. userIDs := make([]int64, 0, 10)
  57. if err = e.Table("access").
  58. Where("repo_id = ? AND mode >= ?", repo.ID, perm.AccessModeWrite).
  59. Select("user_id").
  60. Find(&userIDs); err != nil {
  61. return nil, err
  62. }
  63. additionalUserIDs := make([]int64, 0, 10)
  64. if err = e.Table("team_user").
  65. Join("INNER", "team_repo", "`team_repo`.team_id = `team_user`.team_id").
  66. Join("INNER", "team_unit", "`team_unit`.team_id = `team_user`.team_id").
  67. Where("`team_repo`.repo_id = ? AND (`team_unit`.access_mode >= ? OR (`team_unit`.access_mode = ? AND `team_unit`.`type` = ?))",
  68. repo.ID, perm.AccessModeWrite, perm.AccessModeRead, unit.TypePullRequests).
  69. Distinct("`team_user`.uid").
  70. Select("`team_user`.uid").
  71. Find(&additionalUserIDs); err != nil {
  72. return nil, err
  73. }
  74. uniqueUserIDs := make(container.Set[int64])
  75. uniqueUserIDs.AddMultiple(userIDs...)
  76. uniqueUserIDs.AddMultiple(additionalUserIDs...)
  77. // Leave a seat for owner itself to append later, but if owner is an organization
  78. // and just waste 1 unit is cheaper than re-allocate memory once.
  79. users := make([]*user_model.User, 0, len(uniqueUserIDs)+1)
  80. if len(userIDs) > 0 {
  81. if err = e.In("id", uniqueUserIDs.Values()).OrderBy(user_model.GetOrderByName()).Find(&users); err != nil {
  82. return nil, err
  83. }
  84. }
  85. if !repo.Owner.IsOrganization() && !uniqueUserIDs.Contains(repo.OwnerID) {
  86. users = append(users, repo.Owner)
  87. }
  88. return users, nil
  89. }
  90. // GetReviewers get all users can be requested to review:
  91. // * for private repositories this returns all users that have read access or higher to the repository.
  92. // * for public repositories this returns all users that have read access or higher to the repository,
  93. // all repo watchers and all organization members.
  94. // TODO: may be we should have a busy choice for users to block review request to them.
  95. func GetReviewers(ctx context.Context, repo *Repository, doerID, posterID int64) ([]*user_model.User, error) {
  96. // Get the owner of the repository - this often already pre-cached and if so saves complexity for the following queries
  97. if err := repo.LoadOwner(ctx); err != nil {
  98. return nil, err
  99. }
  100. cond := builder.And(builder.Neq{"`user`.id": posterID})
  101. if repo.IsPrivate || repo.Owner.Visibility == api.VisibleTypePrivate {
  102. // This a private repository:
  103. // Anyone who can read the repository is a requestable reviewer
  104. cond = cond.And(builder.In("`user`.id",
  105. builder.Select("user_id").From("access").Where(
  106. builder.Eq{"repo_id": repo.ID}.
  107. And(builder.Gte{"mode": perm.AccessModeRead}),
  108. ),
  109. ))
  110. if repo.Owner.Type == user_model.UserTypeIndividual && repo.Owner.ID != posterID {
  111. // as private *user* repos don't generate an entry in the `access` table,
  112. // the owner of a private repo needs to be explicitly added.
  113. cond = cond.Or(builder.Eq{"`user`.id": repo.Owner.ID})
  114. }
  115. } else {
  116. // This is a "public" repository:
  117. // Any user that has read access, is a watcher or organization member can be requested to review
  118. cond = cond.And(builder.And(builder.In("`user`.id",
  119. builder.Select("user_id").From("access").
  120. Where(builder.Eq{"repo_id": repo.ID}.
  121. And(builder.Gte{"mode": perm.AccessModeRead})),
  122. ).Or(builder.In("`user`.id",
  123. builder.Select("user_id").From("watch").
  124. Where(builder.Eq{"repo_id": repo.ID}.
  125. And(builder.In("mode", WatchModeNormal, WatchModeAuto))),
  126. ).Or(builder.In("`user`.id",
  127. builder.Select("uid").From("org_user").
  128. Where(builder.Eq{"org_id": repo.OwnerID}),
  129. )))))
  130. }
  131. users := make([]*user_model.User, 0, 8)
  132. return users, db.GetEngine(ctx).Where(cond).OrderBy(user_model.GetOrderByName()).Find(&users)
  133. }
  134. // GetIssuePostersWithSearch returns users with limit of 30 whose username started with prefix that have authored an issue/pull request for the given repository
  135. // If isShowFullName is set to true, also include full name prefix search
  136. func GetIssuePostersWithSearch(ctx context.Context, repo *Repository, isPull bool, search string, isShowFullName bool) ([]*user_model.User, error) {
  137. users := make([]*user_model.User, 0, 30)
  138. var prefixCond builder.Cond = builder.Like{"name", search + "%"}
  139. if isShowFullName {
  140. prefixCond = prefixCond.Or(builder.Like{"full_name", "%" + search + "%"})
  141. }
  142. cond := builder.In("`user`.id",
  143. builder.Select("poster_id").From("issue").Where(
  144. builder.Eq{"repo_id": repo.ID}.
  145. And(builder.Eq{"is_pull": isPull}),
  146. ).GroupBy("poster_id")).And(prefixCond)
  147. return users, db.GetEngine(ctx).
  148. Where(cond).
  149. Cols("id", "name", "full_name", "avatar", "avatar_email", "use_custom_avatar").
  150. OrderBy("name").
  151. Limit(30).
  152. Find(&users)
  153. }