wip
This commit is contained in:
parent
7433ac1d3a
commit
5f00b10f35
@ -55,7 +55,7 @@ func parsePrLine(line string) (BasicPR, error) {
|
|||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExtractPRsFromDescription(data *bufio.Scanner) (string, []BasicPR) {
|
func ExtractDescriptionAndPRs(data *bufio.Scanner) (string, []BasicPR) {
|
||||||
prs := make([]BasicPR, 0, 1)
|
prs := make([]BasicPR, 0, 1)
|
||||||
var desc strings.Builder
|
var desc strings.Builder
|
||||||
|
|
||||||
|
@ -73,7 +73,7 @@ func TestAssociatedPRScanner(t *testing.T) {
|
|||||||
|
|
||||||
for _, test := range testTable {
|
for _, test := range testTable {
|
||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
desc, prs := ExtractPRsFromDescription(newStringScanner(test.input))
|
desc, prs := ExtractDescriptionAndPRs(newStringScanner(test.input))
|
||||||
if len(prs) != len(test.prs) {
|
if len(prs) != len(test.prs) {
|
||||||
t.Error("Unexpected length:", len(prs), "expected:", len(test.prs))
|
t.Error("Unexpected length:", len(prs), "expected:", len(test.prs))
|
||||||
return
|
return
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"src.opensuse.org/autogits/common"
|
"src.opensuse.org/autogits/common"
|
||||||
@ -65,35 +66,52 @@ type PRReviewInfo struct {
|
|||||||
reviews []*models.PullReview
|
reviews []*models.PullReview
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchAllAssociatedPRs(gitea GiteaPRInterface, org, repo string, prNum int64) ([]PRReviewInfo, error) {
|
func fetchPRandReviews(gitea GiteaPRInterface, org, repo string, prNum int64) (PRReviewInfo, error) {
|
||||||
pr, reviews, err := gitea.GetPullRequestAndReviews(org, repo, prNum)
|
pr, reviews, err := gitea.GetPullRequestAndReviews(org, repo, prNum)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return PRReviewInfo{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
ret := make([]PRReviewInfo, 1, 2)
|
return PRReviewInfo{
|
||||||
ret[0].pr = pr
|
pr: pr,
|
||||||
ret[0].reviews = reviews
|
reviews: reviews,
|
||||||
|
}, nil
|
||||||
return ret, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func CheckIfMaintainersApproved(gitea common.GiteaMaintainershipInterface, giteapr GiteaPRInterface, config common.AutogitConfig, prjGitPRNumber int64) (bool, error) {
|
func isMaintainerApprovedPR(pr PRReviewInfo, maintainers MaintainershipMap) bool {
|
||||||
prs, _ := fetchAllAssociatedPRs(giteapr, config.Organization, config.GitProjectName, prjGitPRNumber)
|
m := append(MaintainerListForPackage(maintainers, pr.pr.Base.Name), MaintainerListForProject(maintainers)...)
|
||||||
|
for _, review := range pr.reviews {
|
||||||
|
if review.Stale {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if slices.Contains(m, review.User.UserName) {
|
||||||
|
if review.State == common.ReviewStateApproved {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsPrjGitPRApproved(gitea common.GiteaMaintainershipInterface, giteapr GiteaPRInterface, config common.AutogitConfig, prjGitPRNumber int64) (bool, error) {
|
||||||
|
prjPR, _ := fetchPRandReviews(giteapr, config.Organization, config.GitProjectName, prjGitPRNumber)
|
||||||
|
|
||||||
data, _ := gitea.FetchMaintainershipFile(config.Organization, config.GitProjectName, config.Branch)
|
data, _ := gitea.FetchMaintainershipFile(config.Organization, config.GitProjectName, config.Branch)
|
||||||
|
maintainers, _ := parseMaintainershipData(data)
|
||||||
|
|
||||||
for _, pr := range prs {
|
_, prjAssociatedPRs := common.ExtractDescriptionAndPRs(bufio.NewScanner(strings.NewReader(prjPR.pr.Body)))
|
||||||
_, associatedPRs := common.ExtractPRsFromDescription(bufio.NewScanner(strings.NewReader(pr.pr.Body)))
|
|
||||||
|
|
||||||
if len(associatedPRs) == 0 {
|
for _, PR := range prjAssociatedPRs {
|
||||||
// no associated packages with this PR
|
prInfo, _ := fetchPRandReviews(giteapr, PR.Org, PR.Repo, PR.Num)
|
||||||
break
|
|
||||||
}
|
_, associatedPRs := common.ExtractDescriptionAndPRs(bufio.NewScanner(strings.NewReader(prInfo.pr.Body)))
|
||||||
|
|
||||||
if len(associatedPRs) != 1 {
|
if len(associatedPRs) != 1 {
|
||||||
return false, fmt.Errorf("Associated PR doesn't link only to the prjgit PR: %s/%s#%d",
|
return false, fmt.Errorf("Associated PR doesn't link only to the prjgit PR: %s/%s#%d",
|
||||||
associatedPRs[0].Org, associatedPRs[0].Repo, associatedPRs[0].Num)
|
associatedPRs[0].Org, associatedPRs[0].Repo, associatedPRs[0].Num)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if associatedPRs[0].Org != config.Organization || associatedPRs[0].Repo != config.GitProjectName || associatedPRs[0].Num != prjGitPRNumber {
|
if associatedPRs[0].Org != config.Organization || associatedPRs[0].Repo != config.GitProjectName || associatedPRs[0].Num != prjGitPRNumber {
|
||||||
@ -101,9 +119,11 @@ func CheckIfMaintainersApproved(gitea common.GiteaMaintainershipInterface, gitea
|
|||||||
associatedPRs[0].Org, associatedPRs[0].Repo, associatedPRs[0].Num,
|
associatedPRs[0].Org, associatedPRs[0].Repo, associatedPRs[0].Num,
|
||||||
config.Organization, config.GitProjectName, prjGitPRNumber)
|
config.Organization, config.GitProjectName, prjGitPRNumber)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
parseMaintainershipData(data)
|
|
||||||
|
|
||||||
|
if !isMaintainerApprovedPR(prInfo, maintainers) {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
@ -12,7 +12,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestMaintainership(t *testing.T) {
|
func TestMaintainership(t *testing.T) {
|
||||||
|
|
||||||
allocateMaintainershipInterface := func(t *testing.T) (*mock_common.MockGiteaMaintainershipInterface, *mock_main.MockGiteaPRInterface) {
|
allocateMaintainershipInterface := func(t *testing.T) (*mock_common.MockGiteaMaintainershipInterface, *mock_main.MockGiteaPRInterface) {
|
||||||
ctl := gomock.NewController(t)
|
ctl := gomock.NewController(t)
|
||||||
mi := mock_common.NewMockGiteaMaintainershipInterface(ctl)
|
mi := mock_common.NewMockGiteaMaintainershipInterface(ctl)
|
||||||
@ -27,11 +26,18 @@ func TestMaintainership(t *testing.T) {
|
|||||||
GitProjectName: common.DefaultGitPrj,
|
GitProjectName: common.DefaultGitPrj,
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("No maintainer in empty package", func(t *testing.T) {
|
packageTests := []struct {
|
||||||
|
name string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "No maintainer in empty package",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range packageTests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
mi, _ := allocateMaintainershipInterface(t)
|
mi, _ := allocateMaintainershipInterface(t)
|
||||||
|
|
||||||
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(nil, nil)
|
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(nil, nil)
|
||||||
|
|
||||||
maintainers, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
|
maintainers, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("Invalid number of maintainers for package", err)
|
t.Error("Invalid number of maintainers for package", err)
|
||||||
@ -42,26 +48,36 @@ func TestMaintainership(t *testing.T) {
|
|||||||
t.Error("Invalid number of maintainers for package", err)
|
t.Error("Invalid number of maintainers for package", err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("No maintainer for empty projects", func(t *testing.T) {
|
|
||||||
mi, _ := allocateMaintainershipInterface(t)
|
|
||||||
|
|
||||||
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(nil, nil)
|
|
||||||
|
|
||||||
maintainers, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
|
|
||||||
m := MaintainerListForProject(maintainers)
|
|
||||||
if len(m) != 0 || err != nil {
|
|
||||||
t.Error("Invalid number of maintainers for project", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte("{}"), nil)
|
projectTests := []struct {
|
||||||
|
name string
|
||||||
|
maintainersFile []byte
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "No maintainer for empty project",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "No maintainer for empty project maintainer file",
|
||||||
|
maintainersFile: []byte("{}"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
maintainers, err = ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
|
for _, test := range projectTests {
|
||||||
m = MaintainerListForProject(maintainers)
|
t.Run(test.name, func(t *testing.T) {
|
||||||
if len(m) != 0 || err != nil {
|
mi, _ := allocateMaintainershipInterface(t)
|
||||||
|
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(test.maintainersFile, nil)
|
||||||
|
maintainers, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("Invalid number of maintainers for package", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
m := MaintainerListForProject(maintainers)
|
||||||
|
if len(m) != 0 {
|
||||||
t.Error("Invalid number of maintainers for project", err)
|
t.Error("Invalid number of maintainers for project", err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
t.Run("Error in MaintainerListForPackage when remote has an error", func(t *testing.T) {
|
t.Run("Error in MaintainerListForPackage when remote has an error", func(t *testing.T) {
|
||||||
mi, _ := allocateMaintainershipInterface(t)
|
mi, _ := allocateMaintainershipInterface(t)
|
||||||
@ -237,6 +253,22 @@ func TestMaintainership(t *testing.T) {
|
|||||||
t.Error("Invalid number of maintainers for project", err)
|
t.Error("Invalid number of maintainers for project", err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReviewApproval(t *testing.T) {
|
||||||
|
allocateMaintainershipInterface := func(t *testing.T) (*mock_common.MockGiteaMaintainershipInterface, *mock_main.MockGiteaPRInterface) {
|
||||||
|
ctl := gomock.NewController(t)
|
||||||
|
mi := mock_common.NewMockGiteaMaintainershipInterface(ctl)
|
||||||
|
pri := mock_main.NewMockGiteaPRInterface(ctl)
|
||||||
|
|
||||||
|
return mi, pri
|
||||||
|
}
|
||||||
|
|
||||||
|
config := common.AutogitConfig{
|
||||||
|
Branch: "bar",
|
||||||
|
Organization: "foo",
|
||||||
|
GitProjectName: common.DefaultGitPrj,
|
||||||
|
}
|
||||||
|
|
||||||
t.Run("Maintainers not appoved", func(t *testing.T) {
|
t.Run("Maintainers not appoved", func(t *testing.T) {
|
||||||
mi, pri := allocateMaintainershipInterface(t)
|
mi, pri := allocateMaintainershipInterface(t)
|
||||||
@ -252,7 +284,7 @@ func TestMaintainership(t *testing.T) {
|
|||||||
)
|
)
|
||||||
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(`{"foo": ["bingo"]}`), nil)
|
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(`{"foo": ["bingo"]}`), nil)
|
||||||
|
|
||||||
approved, err := CheckIfMaintainersApproved(mi, pri, config, 10)
|
approved, err := IsPrjGitPRApproved(mi, pri, config, 10)
|
||||||
if approved || err != nil {
|
if approved || err != nil {
|
||||||
t.Error("Unexpected approved or err:", err)
|
t.Error("Unexpected approved or err:", err)
|
||||||
}
|
}
|
||||||
@ -274,7 +306,7 @@ func TestMaintainership(t *testing.T) {
|
|||||||
"": ["test"]
|
"": ["test"]
|
||||||
`), nil)
|
`), nil)
|
||||||
|
|
||||||
approved, err := CheckIfMaintainersApproved(mi, pri, config, 10)
|
approved, err := IsPrjGitPRApproved(mi, pri, config, 10)
|
||||||
if !approved || err != nil {
|
if !approved || err != nil {
|
||||||
t.Error("Unexpected disapproval or err:", err)
|
t.Error("Unexpected disapproval or err:", err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user