Skip to content

Commit 022eac4

Browse files
authored
Get repo assignees and reviewers should ignore deactivated users (#30770) (#30782)
Backport #30770 If an user is deactivated, it should not be in the list of users who are suggested to be assigned or review-requested. old assignees or reviewers are not affected. --- *Sponsored by Kithara Software GmbH*
1 parent a75b0d2 commit 022eac4

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

models/repo/user_repo.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ func GetRepoAssignees(ctx context.Context, repo *Repository) (_ []*user_model.Us
130130
// and just waste 1 unit is cheaper than re-allocate memory once.
131131
users := make([]*user_model.User, 0, len(uniqueUserIDs)+1)
132132
if len(userIDs) > 0 {
133-
if err = e.In("id", uniqueUserIDs.Values()).OrderBy(user_model.GetOrderByName()).Find(&users); err != nil {
133+
if err = e.In("id", uniqueUserIDs.Values()).
134+
Where(builder.Eq{"`user`.is_active": true}).
135+
OrderBy(user_model.GetOrderByName()).
136+
Find(&users); err != nil {
134137
return nil, err
135138
}
136139
}
@@ -152,7 +155,8 @@ func GetReviewers(ctx context.Context, repo *Repository, doerID, posterID int64)
152155
return nil, err
153156
}
154157

155-
cond := builder.And(builder.Neq{"`user`.id": posterID})
158+
cond := builder.And(builder.Neq{"`user`.id": posterID}).
159+
And(builder.Eq{"`user`.is_active": true})
156160

157161
if repo.IsPrivate || repo.Owner.Visibility == api.VisibleTypePrivate {
158162
// This a private repository:

models/repo/user_repo_test.go

+17-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"code.gitea.io/gitea/models/db"
1010
repo_model "code.gitea.io/gitea/models/repo"
1111
"code.gitea.io/gitea/models/unittest"
12+
user_model "code.gitea.io/gitea/models/user"
1213

1314
"github.com/stretchr/testify/assert"
1415
)
@@ -25,8 +26,17 @@ func TestRepoAssignees(t *testing.T) {
2526
repo21 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 21})
2627
users, err = repo_model.GetRepoAssignees(db.DefaultContext, repo21)
2728
assert.NoError(t, err)
28-
assert.Len(t, users, 4)
29-
assert.ElementsMatch(t, []int64{10, 15, 16, 18}, []int64{users[0].ID, users[1].ID, users[2].ID, users[3].ID})
29+
if assert.Len(t, users, 4) {
30+
assert.ElementsMatch(t, []int64{10, 15, 16, 18}, []int64{users[0].ID, users[1].ID, users[2].ID, users[3].ID})
31+
}
32+
33+
// do not return deactivated users
34+
assert.NoError(t, user_model.UpdateUserCols(db.DefaultContext, &user_model.User{ID: 15, IsActive: false}, "is_active"))
35+
users, err = repo_model.GetRepoAssignees(db.DefaultContext, repo21)
36+
assert.NoError(t, err)
37+
if assert.Len(t, users, 3) {
38+
assert.NotContains(t, []int64{users[0].ID, users[1].ID, users[2].ID}, 15)
39+
}
3040
}
3141

3242
func TestRepoGetReviewers(t *testing.T) {
@@ -38,17 +48,19 @@ func TestRepoGetReviewers(t *testing.T) {
3848
ctx := db.DefaultContext
3949
reviewers, err := repo_model.GetReviewers(ctx, repo1, 2, 2)
4050
assert.NoError(t, err)
41-
assert.Len(t, reviewers, 4)
51+
if assert.Len(t, reviewers, 3) {
52+
assert.ElementsMatch(t, []int64{1, 4, 11}, []int64{reviewers[0].ID, reviewers[1].ID, reviewers[2].ID})
53+
}
4254

4355
// should include doer if doer is not PR poster.
4456
reviewers, err = repo_model.GetReviewers(ctx, repo1, 11, 2)
4557
assert.NoError(t, err)
46-
assert.Len(t, reviewers, 4)
58+
assert.Len(t, reviewers, 3)
4759

4860
// should not include PR poster, if PR poster would be otherwise eligible
4961
reviewers, err = repo_model.GetReviewers(ctx, repo1, 11, 4)
5062
assert.NoError(t, err)
51-
assert.Len(t, reviewers, 3)
63+
assert.Len(t, reviewers, 2)
5264

5365
// test private user repo
5466
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})

tests/integration/api_repo_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,9 @@ func TestAPIRepoGetReviewers(t *testing.T) {
684684
resp := MakeRequest(t, req, http.StatusOK)
685685
var reviewers []*api.User
686686
DecodeJSON(t, resp, &reviewers)
687-
assert.Len(t, reviewers, 4)
687+
if assert.Len(t, reviewers, 3) {
688+
assert.ElementsMatch(t, []int64{1, 4, 11}, []int64{reviewers[0].ID, reviewers[1].ID, reviewers[2].ID})
689+
}
688690
}
689691

690692
func TestAPIRepoGetAssignees(t *testing.T) {

0 commit comments

Comments
 (0)