2024-11-27 17:50:55 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2024-12-10 19:03:54 +01:00
|
|
|
"slices"
|
2024-11-27 17:50:55 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"go.uber.org/mock/gomock"
|
|
|
|
"src.opensuse.org/autogits/common"
|
2024-11-29 17:33:01 +01:00
|
|
|
"src.opensuse.org/autogits/common/gitea-generated/models"
|
2024-12-09 00:39:55 +01:00
|
|
|
mock_common "src.opensuse.org/autogits/common/mock"
|
2024-11-27 17:50:55 +01:00
|
|
|
mock_main "src.opensuse.org/workflow-pr/mock"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMaintainership(t *testing.T) {
|
2024-11-29 17:33:01 +01:00
|
|
|
config := common.AutogitConfig{
|
|
|
|
Branch: "bar",
|
|
|
|
Organization: "foo",
|
2024-11-28 18:16:14 +01:00
|
|
|
GitProjectName: common.DefaultGitPrj,
|
|
|
|
}
|
|
|
|
|
2024-12-09 18:20:56 +01:00
|
|
|
packageTests := []struct {
|
2024-12-10 19:03:54 +01:00
|
|
|
name string
|
|
|
|
maintainersFile []byte
|
|
|
|
maintainersFileErr error
|
|
|
|
maintainers []string
|
|
|
|
otherError bool
|
2024-12-09 18:20:56 +01:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "No maintainer in empty package",
|
|
|
|
},
|
2024-12-10 19:03:54 +01:00
|
|
|
{
|
|
|
|
name: "Error in MaintainerListForPackage when remote has an error",
|
|
|
|
maintainersFileErr: errors.New("some error here"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Multiple package maintainers",
|
|
|
|
maintainersFile: []byte(`{"pkg": ["user1", "user2"], "": ["user1", "user3"]}`),
|
|
|
|
maintainers: []string{"user1", "user2", "user3"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "No package maintainers and only project maintainer",
|
|
|
|
maintainersFile: []byte(`{"pkg2": ["user1", "user2"], "": ["user1", "user3"]}`),
|
|
|
|
maintainers: []string{"user1", "user3"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid list of package maintainers",
|
|
|
|
maintainersFile: []byte(`{"pkg": 3,"": ["user", 4]}`),
|
|
|
|
otherError: true,
|
|
|
|
},
|
2024-12-09 18:20:56 +01:00
|
|
|
}
|
2024-11-27 17:50:55 +01:00
|
|
|
|
2024-12-09 18:20:56 +01:00
|
|
|
for _, test := range packageTests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2024-12-10 19:03:54 +01:00
|
|
|
ctl := gomock.NewController(t)
|
|
|
|
mi := mock_common.NewMockGiteaMaintainershipInterface(ctl)
|
|
|
|
|
|
|
|
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").
|
|
|
|
Return(test.maintainersFile, test.maintainersFileErr)
|
|
|
|
|
2024-12-09 18:20:56 +01:00
|
|
|
maintainers, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
|
2024-12-10 19:03:54 +01:00
|
|
|
if err != nil && !test.otherError {
|
|
|
|
if test.maintainersFileErr == nil {
|
|
|
|
t.Fatal("Unexpected error recieved", err)
|
|
|
|
} else if err != test.maintainersFileErr {
|
|
|
|
t.Error("Unexpected error recieved", err)
|
|
|
|
}
|
|
|
|
} else if test.maintainersFileErr != nil {
|
|
|
|
t.Fatal("Expected an error...")
|
|
|
|
} else if test.otherError && err == nil {
|
|
|
|
t.Fatal("Expected an error...")
|
2024-12-09 18:20:56 +01:00
|
|
|
}
|
2024-11-27 17:50:55 +01:00
|
|
|
|
2024-12-10 19:03:54 +01:00
|
|
|
m := MaintainerListForPackage(maintainers, "pkg")
|
|
|
|
if len(m) != len(test.maintainers) {
|
2024-12-09 18:20:56 +01:00
|
|
|
t.Error("Invalid number of maintainers for package", err)
|
|
|
|
}
|
2024-12-10 19:03:54 +01:00
|
|
|
for i := range m {
|
|
|
|
if !slices.Contains(test.maintainers, m[i]) {
|
|
|
|
t.Fatal("Can't find expected users. Found:", m)
|
|
|
|
}
|
|
|
|
}
|
2024-12-09 18:20:56 +01:00
|
|
|
})
|
|
|
|
}
|
2024-11-27 17:50:55 +01:00
|
|
|
|
2024-12-09 18:20:56 +01:00
|
|
|
projectTests := []struct {
|
2024-12-10 19:03:54 +01:00
|
|
|
name string
|
|
|
|
maintainersFile []byte
|
|
|
|
maintainersFileErr error
|
|
|
|
numMaintainers int
|
|
|
|
maintainers []string
|
|
|
|
otherError bool
|
2024-12-09 18:20:56 +01:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "No maintainer for empty project",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "No maintainer for empty project maintainer file",
|
|
|
|
maintainersFile: []byte("{}"),
|
|
|
|
},
|
2024-12-10 19:03:54 +01:00
|
|
|
{
|
|
|
|
name: "Error in MaintainerListForProject when remote has an error",
|
|
|
|
maintainersFileErr: errors.New("some error here"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Multiple project maintainers",
|
|
|
|
maintainersFile: []byte(`{"": ["user1", "user2"]}`),
|
|
|
|
numMaintainers: 2,
|
|
|
|
maintainers: []string{"user1", "user2"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Single project maintainer",
|
|
|
|
maintainersFile: []byte(`{"": ["user"]}`),
|
|
|
|
numMaintainers: 1,
|
|
|
|
maintainers: []string{"user"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid list of project maintainers",
|
|
|
|
maintainersFile: []byte(`{"": ["user", 4]}`),
|
|
|
|
otherError: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid list of project maintainers",
|
|
|
|
maintainersFile: []byte(`{"": 4}`),
|
|
|
|
otherError: true,
|
|
|
|
},
|
2024-12-09 18:20:56 +01:00
|
|
|
}
|
2024-11-28 00:15:32 +01:00
|
|
|
|
2024-12-09 18:20:56 +01:00
|
|
|
for _, test := range projectTests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2024-12-10 19:03:54 +01:00
|
|
|
ctl := gomock.NewController(t)
|
|
|
|
mi := mock_common.NewMockGiteaMaintainershipInterface(ctl)
|
2024-11-28 00:15:32 +01:00
|
|
|
|
2024-12-10 19:03:54 +01:00
|
|
|
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").
|
|
|
|
Return(test.maintainersFile, test.maintainersFileErr)
|
2024-11-28 00:15:32 +01:00
|
|
|
|
2024-12-10 19:03:54 +01:00
|
|
|
maintainers, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
|
|
|
|
if err != nil && !test.otherError {
|
|
|
|
if test.maintainersFileErr == nil {
|
|
|
|
t.Fatal("Unexpected error recieved", err)
|
|
|
|
} else if err != test.maintainersFileErr {
|
|
|
|
t.Error("Unexpected error recieved", err)
|
|
|
|
}
|
|
|
|
} else if test.maintainersFileErr != nil {
|
|
|
|
t.Fatal("Expected an error...")
|
|
|
|
} else if test.otherError && err == nil {
|
|
|
|
t.Fatal("Expected an error...")
|
2024-11-28 17:10:26 +01:00
|
|
|
}
|
|
|
|
|
2024-12-10 19:03:54 +01:00
|
|
|
m := MaintainerListForProject(maintainers)
|
|
|
|
if len(m) != test.numMaintainers {
|
|
|
|
t.Error("Invalid number of maintainers", err)
|
2024-11-28 17:10:26 +01:00
|
|
|
}
|
|
|
|
|
2024-12-10 19:03:54 +01:00
|
|
|
for i := range m {
|
|
|
|
if i >= len(test.maintainers) || test.maintainers[i] != m[i] {
|
|
|
|
t.Error("Can't find expected users. Found:", m)
|
|
|
|
}
|
2024-11-28 17:10:26 +01:00
|
|
|
}
|
2024-12-10 19:03:54 +01:00
|
|
|
})
|
|
|
|
}
|
2024-12-09 18:20:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReviewApproval(t *testing.T) {
|
|
|
|
config := common.AutogitConfig{
|
|
|
|
Branch: "bar",
|
|
|
|
Organization: "foo",
|
|
|
|
GitProjectName: common.DefaultGitPrj,
|
|
|
|
}
|
2024-11-28 17:10:26 +01:00
|
|
|
|
2024-12-10 19:03:54 +01:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
pr *models.PullRequest
|
|
|
|
reviews []*models.PullReview
|
2024-11-29 17:33:01 +01:00
|
|
|
|
2024-12-10 19:03:54 +01:00
|
|
|
maintainerFile []byte
|
2024-11-29 17:33:01 +01:00
|
|
|
|
2024-12-10 19:03:54 +01:00
|
|
|
approved bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Maintainer not approved",
|
2024-12-11 01:12:59 +01:00
|
|
|
pr: &models.PullRequest{Body: "PR: foo/foo#10", Index: 10, RequestedReviewers: []*models.User{}},
|
2024-12-10 19:03:54 +01:00
|
|
|
reviews: []*models.PullReview{},
|
2024-11-29 17:33:01 +01:00
|
|
|
|
2024-12-10 19:03:54 +01:00
|
|
|
maintainerFile: []byte(`{"foo": ["bingo"]}`),
|
2024-11-29 17:33:01 +01:00
|
|
|
|
2024-12-10 19:03:54 +01:00
|
|
|
approved: false,
|
|
|
|
},
|
|
|
|
{
|
2024-12-11 01:12:59 +01:00
|
|
|
name: "Maintainer approved",
|
|
|
|
pr: &models.PullRequest{Body: "", Index: 10, RequestedReviewers: []*models.User{}},
|
2024-12-10 19:03:54 +01:00
|
|
|
reviews: []*models.PullReview{
|
|
|
|
&models.PullReview{
|
2024-12-11 01:12:59 +01:00
|
|
|
Body: "wow!",
|
2024-12-10 19:03:54 +01:00
|
|
|
Stale: false,
|
|
|
|
State: common.ReviewStateApproved,
|
|
|
|
User: &models.User{
|
|
|
|
UserName: "king",
|
|
|
|
},
|
|
|
|
},
|
2024-11-29 17:33:01 +01:00
|
|
|
},
|
|
|
|
|
2024-12-10 19:03:54 +01:00
|
|
|
maintainerFile: []byte(`{"": ["king"], "foo": ["bingo"]}`),
|
|
|
|
|
|
|
|
approved: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
ctl := gomock.NewController(t)
|
|
|
|
mi := mock_common.NewMockGiteaMaintainershipInterface(ctl)
|
|
|
|
pri := mock_main.NewMockGiteaPRInterface(ctl)
|
|
|
|
|
|
|
|
pri.EXPECT().GetPullRequestAndReviews("foo", common.DefaultGitPrj, int64(10)).
|
|
|
|
Return(test.pr, test.reviews, nil)
|
|
|
|
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(test.maintainerFile, nil)
|
|
|
|
|
|
|
|
approved, err := IsPrjGitPRApproved(mi, pri, config, 10)
|
2024-12-11 01:12:59 +01:00
|
|
|
if approved != test.approved {
|
|
|
|
t.Error("Unexpected approve state:", approved, "vs. expected", test.approved, ", or err:", err)
|
2024-12-10 19:03:54 +01:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Unexpected error", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2024-11-27 17:50:55 +01:00
|
|
|
}
|