Skip to content
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

Allow users to self-request a PR review #19030

Merged
merged 4 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,21 @@ func getReviewers(ctx context.Context, repo *repo_model.Repository, doerID, post
// This a private repository:
// Anyone who can read the repository is a requestable reviewer
if err := e.
SQL("SELECT * FROM `user` WHERE id in (SELECT user_id FROM `access` WHERE repo_id = ? AND mode >= ? AND user_id NOT IN ( ?, ?)) ORDER BY name",
SQL("SELECT * FROM `user` WHERE id in ("+
6543 marked this conversation as resolved.
Show resolved Hide resolved
"SELECT user_id FROM `access` WHERE repo_id = ? AND mode >= ? AND user_id != ?"+ // private org repos
") ORDER BY name",
repo.ID, perm.AccessModeRead,
doerID, posterID).
posterID).
Find(&users); err != nil {
return nil, err
}

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.
users = append(users, repo.Owner)
}

return users, nil
}

Expand All @@ -244,11 +252,11 @@ func getReviewers(ctx context.Context, repo *repo_model.Repository, doerID, post
"SELECT user_id FROM `watch` WHERE repo_id = ? AND mode IN (?, ?) "+
"UNION "+
"SELECT uid AS user_id FROM `org_user` WHERE org_id = ? "+
") AND id NOT IN (?, ?) ORDER BY name",
") AND id != ? ORDER BY name",
repo.ID, perm.AccessModeRead,
repo.ID, repo_model.WatchModeNormal, repo_model.WatchModeAuto,
repo.OwnerID,
doerID, posterID).
posterID).
Find(&users); err != nil {
return nil, err
}
Expand Down
29 changes: 26 additions & 3 deletions models/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,34 @@ func TestRepoGetReviewers(t *testing.T) {
assert.NoError(t, err)
assert.Len(t, reviewers, 4)

// test private repo
// should include doer if doer is not PR poster.
reviewers, err = GetReviewers(repo1, 11, 2)
assert.NoError(t, err)
assert.Len(t, reviewers, 4)

// should not include PR poster, if PR poster would be otherwise eligible
reviewers, err = GetReviewers(repo1, 11, 4)
assert.NoError(t, err)
assert.Len(t, reviewers, 3)

// test private user repo
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}).(*repo_model.Repository)
reviewers, err = GetReviewers(repo2, 2, 2)

reviewers, err = GetReviewers(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}).(*repo_model.Repository)

reviewers, err = GetReviewers(repo3, 2, 1)
assert.NoError(t, err)
assert.Len(t, reviewers, 2)

reviewers, err = GetReviewers(repo3, 2, 2)
assert.NoError(t, err)
assert.Empty(t, reviewers)
assert.Len(t, reviewers, 1)
}

func TestRepoGetReviewerTeams(t *testing.T) {
Expand Down
8 changes: 0 additions & 8 deletions services/issue/assignee.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,6 @@ func IsValidReviewRequest(reviewer, doer *user_model.User, isAdd bool, issue *mo
}
}

if doer.ID == reviewer.ID {
return models.ErrNotValidReviewRequest{
Reason: "doer can't be reviewer",
UserID: doer.ID,
RepoID: issue.Repo.ID,
}
}

if reviewer.ID == issue.PosterID && issue.OriginalAuthorID == 0 {
return models.ErrNotValidReviewRequest{
Reason: "poster of pr can't be reviewer",
Expand Down