-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Fix get reviewers' bug #32415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
techknowlogick
merged 15 commits into
go-gitea:main
from
lunny:lunny/fix_get_reviewers_bug
Nov 22, 2024
Merged
Fix get reviewers' bug #32415
Changes from 6 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ab7cb95
Fix get reviewers' bug
lunny 672e98e
Fix test
lunny bcc78c3
Move getreviewerteams to pull service
lunny 1b496bd
Merge branch 'main' into lunny/fix_get_reviewers_bug
lunny 6fccf00
Fix test
lunny 6170212
Merge branch 'main' into lunny/fix_get_reviewers_bug
lunny fc1e61c
Use GetTeamsWithAccessToRepoUnit when get review teams and add test
lunny b665306
Merge branch 'main' into lunny/fix_get_reviewers_bug
lunny 697d238
Merge branch 'main' into lunny/fix_get_reviewers_bug
lunny 7e073ec
Move permission check to router layer
lunny e2cd24e
Merge branch 'main' into lunny/fix_get_reviewers_bug
lunny 623c0c7
Merge branch 'main' into lunny/fix_get_reviewers_bug
lunny d5a0c12
Remove unnecessary code and add some comments
lunny 7e2ab14
Merge branch 'lunny/fix_get_reviewers_bug' of github.com:lunny/gitea …
lunny 3a7376a
Merge branch 'main' into lunny/fix_get_reviewers_bug
lunny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package pull | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"code.gitea.io/gitea/models/db" | ||
"code.gitea.io/gitea/models/organization" | ||
"code.gitea.io/gitea/models/perm" | ||
repo_model "code.gitea.io/gitea/models/repo" | ||
"code.gitea.io/gitea/models/unit" | ||
user_model "code.gitea.io/gitea/models/user" | ||
"code.gitea.io/gitea/modules/container" | ||
|
||
"xorm.io/builder" | ||
) | ||
|
||
// GetReviewers get all users can be requested to review: | ||
// - Poster should not be listed | ||
// - For collaborator, all users that have read access or higher to the repository. | ||
// - For repository under organization, users under the teams which have read permission or higher of pull request unit | ||
// - Owner will be listed if it's not an organization, not the poster and not in the list of reviewers | ||
func GetReviewers(ctx context.Context, repo *repo_model.Repository, doerID, posterID int64) ([]*user_model.User, error) { | ||
if err := repo.LoadOwner(ctx); err != nil { | ||
return nil, err | ||
} | ||
|
||
e := db.GetEngine(ctx) | ||
uniqueUserIDs := make(container.Set[int64]) | ||
|
||
collaboratorIDs := make([]int64, 0, 10) | ||
if err := e.Table("collaboration").Where("repo_id=?", repo.ID). | ||
And("mode >= ?", perm.AccessModeRead). | ||
Select("user_id"). | ||
Find(&collaboratorIDs); err != nil { | ||
return nil, err | ||
} | ||
uniqueUserIDs.AddMultiple(collaboratorIDs...) | ||
|
||
if repo.Owner.IsOrganization() { | ||
additionalUserIDs := make([]int64, 0, 10) | ||
if err := e.Table("team_user"). | ||
Join("INNER", "team_repo", "`team_repo`.team_id = `team_user`.team_id"). | ||
Join("INNER", "team_unit", "`team_unit`.team_id = `team_user`.team_id"). | ||
Where("`team_repo`.repo_id = ? AND (`team_unit`.access_mode >= ? AND `team_unit`.`type` = ?)", | ||
repo.ID, perm.AccessModeRead, unit.TypePullRequests). | ||
Distinct("`team_user`.uid"). | ||
Select("`team_user`.uid"). | ||
Find(&additionalUserIDs); err != nil { | ||
return nil, err | ||
} | ||
uniqueUserIDs.AddMultiple(additionalUserIDs...) | ||
|
||
if repo.Owner.Visibility.IsLimited() && doerID == 0 { | ||
return nil, fmt.Errorf("permission denied") | ||
} | ||
|
||
if (repo.IsPrivate || repo.Owner.Visibility.IsPrivate()) && !uniqueUserIDs.Contains(doerID) { | ||
return nil, fmt.Errorf("permission denied") | ||
} | ||
} else { | ||
userIDs := make([]int64, 0, 10) | ||
if err := e.Table("access"). | ||
Where("repo_id = ? AND mode >= ?", repo.ID, perm.AccessModeRead). | ||
Select("user_id"). | ||
Find(&userIDs); err != nil { | ||
return nil, err | ||
} | ||
uniqueUserIDs.AddMultiple(userIDs...) | ||
if repo.IsPrivate && !uniqueUserIDs.Contains(doerID) && doerID != repo.OwnerID { | ||
return nil, fmt.Errorf("permission denied") | ||
} | ||
} | ||
|
||
uniqueUserIDs.Remove(posterID) // posterID should not be in the list of reviewers | ||
|
||
// Leave a seat for owner itself to append later, but if owner is an organization | ||
// and just waste 1 unit is cheaper than re-allocate memory once. | ||
users := make([]*user_model.User, 0, len(uniqueUserIDs)+1) | ||
if len(uniqueUserIDs) > 0 { | ||
if err := e.In("id", uniqueUserIDs.Values()). | ||
Where(builder.Eq{"`user`.is_active": true}). | ||
OrderBy(user_model.GetOrderByName()). | ||
Find(&users); err != nil { | ||
return nil, err | ||
} | ||
} | ||
if repo.OwnerID != posterID && !repo.Owner.IsOrganization() && !uniqueUserIDs.Contains(repo.OwnerID) { | ||
users = append(users, repo.Owner) | ||
} | ||
|
||
return users, nil | ||
} | ||
|
||
// GetReviewerTeams get all teams can be requested to review | ||
func GetReviewerTeams(ctx context.Context, repo *repo_model.Repository) ([]*organization.Team, error) { | ||
if err := repo.LoadOwner(ctx); err != nil { | ||
return nil, err | ||
} | ||
if !repo.Owner.IsOrganization() { | ||
return nil, nil | ||
} | ||
|
||
return organization.GetTeamsWithAccessToRepo(ctx, repo.OwnerID, repo.ID, perm.AccessModeRead) | ||
lunny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package pull_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"code.gitea.io/gitea/models/db" | ||
repo_model "code.gitea.io/gitea/models/repo" | ||
"code.gitea.io/gitea/models/unittest" | ||
pull_service "code.gitea.io/gitea/services/pull" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
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 := pull_service.GetReviewers(ctx, repo1, 2, 0) | ||
assert.NoError(t, err) | ||
if assert.Len(t, reviewers, 1) { | ||
assert.ElementsMatch(t, []int64{2}, []int64{reviewers[0].ID}) | ||
} | ||
|
||
// should not include doer and remove the poster | ||
reviewers, err = pull_service.GetReviewers(ctx, repo1, 11, 2) | ||
assert.NoError(t, err) | ||
assert.Len(t, reviewers, 0) | ||
|
||
// should not include PR poster, if PR poster would be otherwise eligible | ||
reviewers, err = pull_service.GetReviewers(ctx, repo1, 11, 4) | ||
assert.NoError(t, err) | ||
assert.Len(t, reviewers, 1) | ||
|
||
// test private user repo | ||
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}) | ||
|
||
reviewers, err = pull_service.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 = pull_service.GetReviewers(ctx, repo3, 2, 1) | ||
assert.NoError(t, err) | ||
assert.Len(t, reviewers, 2) | ||
|
||
reviewers, err = pull_service.GetReviewers(ctx, repo3, 2, 2) | ||
assert.NoError(t, err) | ||
assert.Len(t, reviewers, 1) | ||
} | ||
|
||
func TestRepoGetReviewerTeams(t *testing.T) { | ||
assert.NoError(t, unittest.PrepareTestDatabase()) | ||
|
||
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}) | ||
teams, err := pull_service.GetReviewerTeams(db.DefaultContext, repo2) | ||
assert.NoError(t, err) | ||
assert.Empty(t, teams) | ||
|
||
repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}) | ||
teams, err = pull_service.GetReviewerTeams(db.DefaultContext, repo3) | ||
assert.NoError(t, err) | ||
assert.Len(t, teams, 2) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.