aboutsummaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2024-11-22 07:44:48 -0800
committerGitHub <noreply@github.com>2024-11-22 10:44:48 -0500
commitfe49cb0243bed03f565269d84836bf21a0597665 (patch)
treebd8ea4fcb3d2201f2b294c6b8eddc7f6cc1de516 /models
parentbc7d599030cad2e69139b79ad5a878504fbf8ed9 (diff)
downloadgitea-fe49cb0243bed03f565269d84836bf21a0597665.tar.gz
gitea-fe49cb0243bed03f565269d84836bf21a0597665.zip
Fix get reviewers' bug (#32415)
This PR rewrites `GetReviewer` function and move it to service layer. Reviewers should not be watchers, so that this PR removed all watchers from reviewers. When the repository is under an organization, the pull request unit read permission will be checked to resolve the bug of #32394 Fix #32394
Diffstat (limited to 'models')
-rw-r--r--models/organization/team_repo.go14
-rw-r--r--models/organization/team_repo_test.go31
-rw-r--r--models/repo/user_repo.go52
-rw-r--r--models/repo/user_repo_test.go43
4 files changed, 45 insertions, 95 deletions
diff --git a/models/organization/team_repo.go b/models/organization/team_repo.go
index 1184e39263..c90dfdeda0 100644
--- a/models/organization/team_repo.go
+++ b/models/organization/team_repo.go
@@ -9,6 +9,7 @@ import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/perm"
repo_model "code.gitea.io/gitea/models/repo"
+ "code.gitea.io/gitea/models/unit"
"xorm.io/builder"
)
@@ -83,3 +84,16 @@ func GetTeamsWithAccessToRepo(ctx context.Context, orgID, repoID int64, mode per
OrderBy("name").
Find(&teams)
}
+
+// GetTeamsWithAccessToRepoUnit returns all teams in an organization that have given access level to the repository special unit.
+func GetTeamsWithAccessToRepoUnit(ctx context.Context, orgID, repoID int64, mode perm.AccessMode, unitType unit.Type) ([]*Team, error) {
+ teams := make([]*Team, 0, 5)
+ return teams, db.GetEngine(ctx).Where("team_unit.access_mode >= ?", mode).
+ Join("INNER", "team_repo", "team_repo.team_id = team.id").
+ Join("INNER", "team_unit", "team_unit.team_id = team.id").
+ And("team_repo.org_id = ?", orgID).
+ And("team_repo.repo_id = ?", repoID).
+ And("team_unit.type = ?", unitType).
+ OrderBy("name").
+ Find(&teams)
+}
diff --git a/models/organization/team_repo_test.go b/models/organization/team_repo_test.go
new file mode 100644
index 0000000000..c0d6750df9
--- /dev/null
+++ b/models/organization/team_repo_test.go
@@ -0,0 +1,31 @@
+// Copyright 2024 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package organization_test
+
+import (
+ "testing"
+
+ "code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/models/organization"
+ "code.gitea.io/gitea/models/perm"
+ "code.gitea.io/gitea/models/repo"
+ "code.gitea.io/gitea/models/unit"
+ "code.gitea.io/gitea/models/unittest"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestGetTeamsWithAccessToRepoUnit(t *testing.T) {
+ assert.NoError(t, unittest.PrepareTestDatabase())
+
+ org41 := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 41})
+ repo61 := unittest.AssertExistsAndLoadBean(t, &repo.Repository{ID: 61})
+
+ teams, err := organization.GetTeamsWithAccessToRepoUnit(db.DefaultContext, org41.ID, repo61.ID, perm.AccessModeRead, unit.TypePullRequests)
+ assert.NoError(t, err)
+ if assert.Len(t, teams, 2) {
+ assert.EqualValues(t, 21, teams[0].ID)
+ assert.EqualValues(t, 22, teams[1].ID)
+ }
+}
diff --git a/models/repo/user_repo.go b/models/repo/user_repo.go
index ecc9216950..a9b1360df1 100644
--- a/models/repo/user_repo.go
+++ b/models/repo/user_repo.go
@@ -11,7 +11,6 @@ import (
"code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/container"
- api "code.gitea.io/gitea/modules/structs"
"xorm.io/builder"
)
@@ -146,57 +145,6 @@ func GetRepoAssignees(ctx context.Context, repo *Repository) (_ []*user_model.Us
return users, nil
}
-// GetReviewers get all users can be requested to review:
-// * for private repositories this returns all users that have read access or higher to the repository.
-// * for public repositories this returns all users that have read access or higher to the repository,
-// all repo watchers and all organization members.
-// TODO: may be we should have a busy choice for users to block review request to them.
-func GetReviewers(ctx context.Context, repo *Repository, doerID, posterID int64) ([]*user_model.User, error) {
- // Get the owner of the repository - this often already pre-cached and if so saves complexity for the following queries
- if err := repo.LoadOwner(ctx); err != nil {
- return nil, err
- }
-
- cond := builder.And(builder.Neq{"`user`.id": posterID}).
- And(builder.Eq{"`user`.is_active": true})
-
- if repo.IsPrivate || repo.Owner.Visibility == api.VisibleTypePrivate {
- // This a private repository:
- // Anyone who can read the repository is a requestable reviewer
-
- cond = cond.And(builder.In("`user`.id",
- builder.Select("user_id").From("access").Where(
- builder.Eq{"repo_id": repo.ID}.
- And(builder.Gte{"mode": perm.AccessModeRead}),
- ),
- ))
-
- if repo.Owner.Type == user_model.UserTypeIndividual && repo.Owner.ID != posterID {
- // as private *user* repos don't generate an entry in the `access` table,
- // the owner of a private repo needs to be explicitly added.
- cond = cond.Or(builder.Eq{"`user`.id": repo.Owner.ID})
- }
- } else {
- // This is a "public" repository:
- // Any user that has read access, is a watcher or organization member can be requested to review
- cond = cond.And(builder.And(builder.In("`user`.id",
- builder.Select("user_id").From("access").
- Where(builder.Eq{"repo_id": repo.ID}.
- And(builder.Gte{"mode": perm.AccessModeRead})),
- ).Or(builder.In("`user`.id",
- builder.Select("user_id").From("watch").
- Where(builder.Eq{"repo_id": repo.ID}.
- And(builder.In("mode", WatchModeNormal, WatchModeAuto))),
- ).Or(builder.In("`user`.id",
- builder.Select("uid").From("org_user").
- Where(builder.Eq{"org_id": repo.OwnerID}),
- )))))
- }
-
- users := make([]*user_model.User, 0, 8)
- return users, db.GetEngine(ctx).Where(cond).OrderBy(user_model.GetOrderByName()).Find(&users)
-}
-
// GetIssuePostersWithSearch returns users with limit of 30 whose username started with prefix that have authored an issue/pull request for the given repository
// If isShowFullName is set to true, also include full name prefix search
func GetIssuePostersWithSearch(ctx context.Context, repo *Repository, isPull bool, search string, isShowFullName bool) ([]*user_model.User, error) {
diff --git a/models/repo/user_repo_test.go b/models/repo/user_repo_test.go
index d2bf6dc912..f2abc2ffa0 100644
--- a/models/repo/user_repo_test.go
+++ b/models/repo/user_repo_test.go
@@ -38,46 +38,3 @@ func TestRepoAssignees(t *testing.T) {
assert.NotContains(t, []int64{users[0].ID, users[1].ID, users[2].ID}, 15)
}
}
-
-func TestRepoGetReviewers(t *testing.T) {
- assert.NoError(t, unittest.PrepareTestDatabase())
-
- // test public repo
- repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
-
- ctx := db.DefaultContext
- reviewers, err := repo_model.GetReviewers(ctx, repo1, 2, 2)
- assert.NoError(t, err)
- if assert.Len(t, reviewers, 3) {
- assert.ElementsMatch(t, []int64{1, 4, 11}, []int64{reviewers[0].ID, reviewers[1].ID, reviewers[2].ID})
- }
-
- // should include doer if doer is not PR poster.
- reviewers, err = repo_model.GetReviewers(ctx, repo1, 11, 2)
- assert.NoError(t, err)
- assert.Len(t, reviewers, 3)
-
- // should not include PR poster, if PR poster would be otherwise eligible
- reviewers, err = repo_model.GetReviewers(ctx, repo1, 11, 4)
- assert.NoError(t, err)
- assert.Len(t, reviewers, 2)
-
- // test private user repo
- repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
-
- reviewers, err = repo_model.GetReviewers(ctx, repo2, 2, 4)
- assert.NoError(t, err)
- assert.Len(t, reviewers, 1)
- assert.EqualValues(t, reviewers[0].ID, 2)
-
- // test private org repo
- repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
-
- reviewers, err = repo_model.GetReviewers(ctx, repo3, 2, 1)
- assert.NoError(t, err)
- assert.Len(t, reviewers, 2)
-
- reviewers, err = repo_model.GetReviewers(ctx, repo3, 2, 2)
- assert.NoError(t, err)
- assert.Len(t, reviewers, 1)
-}