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 5.9KB

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