2022-06-06 10:01:49 +02:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-06-06 10:01:49 +02:00
|
|
|
|
|
|
|
package repo_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2024-04-30 17:36:28 +02:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2022-06-06 10:01:49 +02:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRepoAssignees(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
2022-08-16 04:22:25 +02:00
|
|
|
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
|
2022-06-06 10:01:49 +02:00
|
|
|
users, err := repo_model.GetRepoAssignees(db.DefaultContext, repo2)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, users, 1)
|
|
|
|
assert.Equal(t, users[0].ID, int64(2))
|
|
|
|
|
2022-08-16 04:22:25 +02:00
|
|
|
repo21 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 21})
|
2022-06-06 10:01:49 +02:00
|
|
|
users, err = repo_model.GetRepoAssignees(db.DefaultContext, repo21)
|
|
|
|
assert.NoError(t, err)
|
2024-04-30 17:36:28 +02:00
|
|
|
if assert.Len(t, users, 4) {
|
|
|
|
assert.ElementsMatch(t, []int64{10, 15, 16, 18}, []int64{users[0].ID, users[1].ID, users[2].ID, users[3].ID})
|
|
|
|
}
|
|
|
|
|
|
|
|
// do not return deactivated users
|
|
|
|
assert.NoError(t, user_model.UpdateUserCols(db.DefaultContext, &user_model.User{ID: 15, IsActive: false}, "is_active"))
|
|
|
|
users, err = repo_model.GetRepoAssignees(db.DefaultContext, repo21)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, users, 3) {
|
|
|
|
assert.NotContains(t, []int64{users[0].ID, users[1].ID, users[2].ID}, 15)
|
|
|
|
}
|
2022-06-06 10:01:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepoGetReviewers(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
|
|
|
// test public repo
|
2022-08-16 04:22:25 +02:00
|
|
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2022-06-06 10:01:49 +02:00
|
|
|
|
|
|
|
ctx := db.DefaultContext
|
|
|
|
reviewers, err := repo_model.GetReviewers(ctx, repo1, 2, 2)
|
|
|
|
assert.NoError(t, err)
|
2024-04-30 17:36:28 +02:00
|
|
|
if assert.Len(t, reviewers, 3) {
|
|
|
|
assert.ElementsMatch(t, []int64{1, 4, 11}, []int64{reviewers[0].ID, reviewers[1].ID, reviewers[2].ID})
|
|
|
|
}
|
2022-06-06 10:01:49 +02:00
|
|
|
|
|
|
|
// should include doer if doer is not PR poster.
|
|
|
|
reviewers, err = repo_model.GetReviewers(ctx, repo1, 11, 2)
|
|
|
|
assert.NoError(t, err)
|
2024-04-30 17:36:28 +02:00
|
|
|
assert.Len(t, reviewers, 3)
|
2022-06-06 10:01:49 +02:00
|
|
|
|
|
|
|
// 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)
|
2024-04-30 17:36:28 +02:00
|
|
|
assert.Len(t, reviewers, 2)
|
2022-06-06 10:01:49 +02:00
|
|
|
|
|
|
|
// test private user repo
|
2022-08-16 04:22:25 +02:00
|
|
|
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
|
2022-06-06 10:01:49 +02:00
|
|
|
|
|
|
|
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
|
2022-08-16 04:22:25 +02:00
|
|
|
repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
|
2022-06-06 10:01:49 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|