Skip to content

Commit 51b8d96

Browse files
6543earl-warren
authored andcommitted
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* (cherry picked from commit f2d8ccc5bb2df25557cc0d4d23f2cdd029358274) Conflicts: models/repo/user_repo_test.go because there is one less fixture user compared to Gitea
1 parent 60e5825 commit 51b8d96

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

models/repo/user_repo.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ func GetRepoAssignees(ctx context.Context, repo *Repository) (_ []*user_model.Us
9595
// and just waste 1 unit is cheaper than re-allocate memory once.
9696
users := make([]*user_model.User, 0, len(uniqueUserIDs)+1)
9797
if len(userIDs) > 0 {
98-
if err = e.In("id", uniqueUserIDs.Values()).OrderBy(user_model.GetOrderByName()).Find(&users); err != nil {
98+
if err = e.In("id", uniqueUserIDs.Values()).
99+
Where(builder.Eq{"`user`.is_active": true}).
100+
OrderBy(user_model.GetOrderByName()).
101+
Find(&users); err != nil {
99102
return nil, err
100103
}
101104
}
@@ -117,7 +120,8 @@ func GetReviewers(ctx context.Context, repo *Repository, doerID, posterID int64)
117120
return nil, err
118121
}
119122

120-
cond := builder.And(builder.Neq{"`user`.id": posterID})
123+
cond := builder.And(builder.Neq{"`user`.id": posterID}).
124+
And(builder.Eq{"`user`.is_active": true})
121125

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

models/repo/user_repo_test.go

+16-7
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,17 @@ func TestRepoAssignees(t *testing.T) {
2626
repo21 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 21})
2727
users, err = repo_model.GetRepoAssignees(db.DefaultContext, repo21)
2828
assert.NoError(t, err)
29-
assert.Len(t, users, 3)
30-
assert.Equal(t, users[0].ID, int64(15))
31-
assert.Equal(t, users[1].ID, int64(18))
32-
assert.Equal(t, users[2].ID, int64(16))
29+
if assert.Len(t, users, 3) {
30+
assert.ElementsMatch(t, []int64{15, 16, 18}, []int64{users[0].ID, users[1].ID, users[2].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, 2) {
38+
assert.NotContains(t, []int64{users[0].ID, users[1].ID}, 15)
39+
}
3340
}
3441

3542
func TestRepoGetReviewers(t *testing.T) {
@@ -41,17 +48,19 @@ func TestRepoGetReviewers(t *testing.T) {
4148
ctx := db.DefaultContext
4249
reviewers, err := repo_model.GetReviewers(ctx, repo1, 2, 2)
4350
assert.NoError(t, err)
44-
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+
}
4554

4655
// should include doer if doer is not PR poster.
4756
reviewers, err = repo_model.GetReviewers(ctx, repo1, 11, 2)
4857
assert.NoError(t, err)
49-
assert.Len(t, reviewers, 4)
58+
assert.Len(t, reviewers, 3)
5059

5160
// should not include PR poster, if PR poster would be otherwise eligible
5261
reviewers, err = repo_model.GetReviewers(ctx, repo1, 11, 4)
5362
assert.NoError(t, err)
54-
assert.Len(t, reviewers, 3)
63+
assert.Len(t, reviewers, 2)
5564

5665
// test private user repo
5766
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)