workflow-pr: review processing
This commit is contained in:
parent
7ccbd1deb2
commit
e057cdf0d3
@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -10,77 +9,82 @@ import (
|
|||||||
"src.opensuse.org/autogits/common/gitea-generated/models"
|
"src.opensuse.org/autogits/common/gitea-generated/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PRReviewInfo struct {
|
type Review interface {
|
||||||
|
IsApproved() (bool, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type PRInfo struct {
|
||||||
pr *models.PullRequest
|
pr *models.PullRequest
|
||||||
reviews []*models.PullReview
|
reviews []*models.PullReview
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchPRandReviews(gitea GiteaPRInterface, org, repo string, prNum int64) (PRReviewInfo, error) {
|
type ReviewSet struct {
|
||||||
pr, reviews, err := gitea.GetPullRequestAndReviews(org, repo, prNum)
|
maintainers MaintainershipData
|
||||||
if err != nil {
|
prs []PRInfo
|
||||||
return PRReviewInfo{}, err
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return PRReviewInfo{
|
func fetchPRInfo(gitea GiteaPRInterface, pr common.BasicPR) PRInfo {
|
||||||
pr: pr,
|
data, reviews, _ := gitea.GetPullRequestAndReviews(pr.Org, pr.Repo, pr.Num)
|
||||||
|
return PRInfo{
|
||||||
|
pr: data,
|
||||||
reviews: reviews,
|
reviews: reviews,
|
||||||
}, nil
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func isMaintainerApprovedPR(pr PRReviewInfo, maintainers MaintainershipData) bool {
|
func (rs *ReviewSet) appendPR(gitea GiteaPRInterface, pr common.BasicPR) {
|
||||||
m := slices.Concat(maintainers.ListPackageMaintainers(pr.pr.Base.Name), maintainers.ListProjectMaintainers())
|
if slices.ContainsFunc(rs.prs, func(elem PRInfo) bool {
|
||||||
for _, review := range pr.reviews {
|
return pr.Org == elem.pr.Base.Repo.Owner.UserName &&
|
||||||
if review.Stale {
|
pr.Repo == elem.pr.Base.Repo.Name &&
|
||||||
continue
|
pr.Num == elem.pr.Index
|
||||||
}
|
}) {
|
||||||
|
return
|
||||||
if slices.Contains(m, review.User.UserName) {
|
|
||||||
if review.State == common.ReviewStateApproved {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
prinfo := fetchPRInfo(gitea, pr)
|
||||||
|
rs.prs = append(rs.prs, prinfo)
|
||||||
|
_, childPRs := common.ExtractDescriptionAndPRs(bufio.NewScanner(strings.NewReader(prinfo.pr.Body)))
|
||||||
|
for _, childPR := range childPRs {
|
||||||
|
rs.appendPR(gitea, childPR)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsPrjGitPRApproved(gitea common.GiteaMaintainershipInterface, giteapr GiteaPRInterface, config common.AutogitConfig, prjGitPRNumber int64) (bool, error) {
|
func NewReviewInstance(gitea GiteaPRInterface, maintainers MaintainershipData, org, repo string, prNum int) (*ReviewSet, error) {
|
||||||
prjPR, _ := fetchPRandReviews(giteapr, config.Organization, config.GitProjectName, prjGitPRNumber)
|
ret := &ReviewSet{
|
||||||
|
maintainers: maintainers,
|
||||||
|
prs: []PRInfo{},
|
||||||
|
}
|
||||||
|
|
||||||
maintainers, _ := FetchProjectMaintainershipData(gitea, config.Organization, config.GitProjectName, config.Branch)
|
ret.appendPR(gitea, common.BasicPR{Org: org, Repo: repo, Num: int64(prNum)})
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
|
||||||
_, prjAssociatedPRs := common.ExtractDescriptionAndPRs(bufio.NewScanner(strings.NewReader(prjPR.pr.Body)))
|
func isReviewMaintainerApproved(pkgMaintainersList []string, review *models.PullReview) bool {
|
||||||
|
if review.State != common.ReviewStateApproved || review.Stale {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
for _, PR := range prjAssociatedPRs {
|
if slices.Contains(pkgMaintainersList, review.User.UserName) {
|
||||||
prInfo, _ := fetchPRandReviews(giteapr, PR.Org, PR.Repo, PR.Num)
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
_, associatedPRs := common.ExtractDescriptionAndPRs(bufio.NewScanner(strings.NewReader(prInfo.pr.Body)))
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if len(associatedPRs) != 1 {
|
func (prinfo *PRInfo) isMaintainerApproved(pkgMaintainersList []string) bool {
|
||||||
return false, fmt.Errorf("Associated PR doesn't link only to the prjgit PR: %s/%s#%d",
|
for _, review := range prinfo.reviews {
|
||||||
associatedPRs[0].Org, associatedPRs[0].Repo, associatedPRs[0].Num)
|
if isReviewMaintainerApproved(pkgMaintainersList, review) {
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
if associatedPRs[0].Org != config.Organization || associatedPRs[0].Repo != config.GitProjectName || associatedPRs[0].Num != prjGitPRNumber {
|
func (rs *ReviewSet) IsMaintainerApproved() (bool, error) {
|
||||||
return false, fmt.Errorf("Associated PR (%s/%s#%d) not linking back to prj PR (%s/%s#%d)",
|
for _, prinfo := range rs.prs {
|
||||||
associatedPRs[0].Org, associatedPRs[0].Repo, associatedPRs[0].Num,
|
pkgMaintainerList := rs.maintainers.ListPackageMaintainers(prinfo.pr.Base.Repo.Name)
|
||||||
config.Organization, config.GitProjectName, prjGitPRNumber)
|
if !prinfo.isMaintainerApproved(pkgMaintainerList) {
|
||||||
}
|
|
||||||
|
|
||||||
if !isMaintainerApprovedPR(prInfo, maintainers) {
|
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return true, nil
|
||||||
requiredReviews := slices.Clone(config.Reviewers)
|
|
||||||
for _, r := range prjPR.reviews {
|
|
||||||
if !r.Stale && r.State == common.ReviewStateApproved && slices.Contains(requiredReviews, r.User.UserName) {
|
|
||||||
idx := slices.Index(requiredReviews, r.User.UserName)
|
|
||||||
requiredReviews = slices.Delete(requiredReviews, idx, idx+1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return len(requiredReviews) == 0, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,55 +1,257 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"go.uber.org/mock/gomock"
|
"go.uber.org/mock/gomock"
|
||||||
"src.opensuse.org/autogits/common"
|
"src.opensuse.org/autogits/common"
|
||||||
"src.opensuse.org/autogits/common/gitea-generated/models"
|
"src.opensuse.org/autogits/common/gitea-generated/models"
|
||||||
mock_common "src.opensuse.org/autogits/common/mock"
|
|
||||||
mock_main "src.opensuse.org/workflow-pr/mock"
|
mock_main "src.opensuse.org/workflow-pr/mock"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type pr_review map[string]struct {
|
||||||
|
pr *models.PullRequest
|
||||||
|
reviews []*models.PullReview
|
||||||
|
maintainers []string
|
||||||
|
}
|
||||||
|
|
||||||
func TestReviewApproval(t *testing.T) {
|
func TestReviewApproval(t *testing.T) {
|
||||||
config := common.AutogitConfig{
|
|
||||||
Branch: "bar",
|
|
||||||
Organization: "foo",
|
|
||||||
GitProjectName: common.DefaultGitPrj,
|
|
||||||
}
|
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
pr *models.PullRequest
|
prs pr_review
|
||||||
reviews []*models.PullReview
|
|
||||||
|
|
||||||
maintainerFile []byte
|
|
||||||
|
|
||||||
approved bool
|
approved bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Maintainer not approved",
|
name: "Maintainer not approved",
|
||||||
pr: &models.PullRequest{Body: "PR: foo/foo#10", Index: 10, RequestedReviewers: []*models.User{}},
|
prs: pr_review{
|
||||||
reviews: []*models.PullReview{},
|
"org/repo#42": {
|
||||||
|
pr: &models.PullRequest{
|
||||||
maintainerFile: []byte(`{"foo": ["bingo"]}`),
|
Body: "nothing PR: foo/foo#10", Index: 10,
|
||||||
|
RequestedReviewers: []*models.User{},
|
||||||
|
Base: &models.PRBranchInfo{
|
||||||
|
Repo: &models.Repository{
|
||||||
|
Owner: &models.User{UserName: "org"},
|
||||||
|
Name: "repo"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
reviews: []*models.PullReview{},
|
||||||
|
maintainers: []string{"bingo"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
approved: false,
|
approved: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Maintainer approved",
|
name: "Maintainer approved",
|
||||||
pr: &models.PullRequest{Body: "", Index: 10, RequestedReviewers: []*models.User{}},
|
prs: pr_review{
|
||||||
reviews: []*models.PullReview{
|
"org/repo#42": {
|
||||||
&models.PullReview{
|
pr: &models.PullRequest{
|
||||||
Body: "wow!",
|
Body: "", Index: 42,
|
||||||
Stale: false,
|
RequestedReviewers: []*models.User{},
|
||||||
State: common.ReviewStateApproved,
|
Base: &models.PRBranchInfo{
|
||||||
User: &models.User{
|
Repo: &models.Repository{
|
||||||
UserName: "king",
|
Owner: &models.User{UserName: "org"},
|
||||||
|
Name: "repo"},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
|
reviews: []*models.PullReview{
|
||||||
|
&models.PullReview{
|
||||||
|
Body: "wow!",
|
||||||
|
State: common.ReviewStateApproved,
|
||||||
|
User: &models.User{
|
||||||
|
UserName: "king",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
maintainers: []string{"king", "bingo"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
maintainerFile: []byte(`{"": ["king"], "foo": ["bingo"]}`),
|
approved: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Maintainer approval is missing",
|
||||||
|
prs: pr_review{
|
||||||
|
"org/repo#42": {
|
||||||
|
pr: &models.PullRequest{
|
||||||
|
Body: "", Index: 42,
|
||||||
|
RequestedReviewers: []*models.User{},
|
||||||
|
Base: &models.PRBranchInfo{
|
||||||
|
Repo: &models.Repository{
|
||||||
|
Owner: &models.User{UserName: "org"},
|
||||||
|
Name: "repo"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
reviews: []*models.PullReview{
|
||||||
|
&models.PullReview{
|
||||||
|
Body: "wow!",
|
||||||
|
State: common.ReviewStateApproved,
|
||||||
|
User: &models.User{
|
||||||
|
UserName: "king",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
maintainers: []string{"kong", "bingo"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
approved: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Maintainer dis-approved",
|
||||||
|
prs: pr_review{
|
||||||
|
"org/repo#42": {
|
||||||
|
pr: &models.PullRequest{
|
||||||
|
Body: "", Index: 42,
|
||||||
|
RequestedReviewers: []*models.User{},
|
||||||
|
Base: &models.PRBranchInfo{
|
||||||
|
Repo: &models.Repository{
|
||||||
|
Owner: &models.User{UserName: "org"},
|
||||||
|
Name: "repo"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
reviews: []*models.PullReview{
|
||||||
|
&models.PullReview{
|
||||||
|
Body: "wow!",
|
||||||
|
State: common.ReviewStateRequestChanges,
|
||||||
|
User: &models.User{
|
||||||
|
UserName: "king",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
maintainers: []string{"king", "bingo"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
approved: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Maintainer review is stale",
|
||||||
|
prs: pr_review{
|
||||||
|
"org/repo#42": {
|
||||||
|
pr: &models.PullRequest{
|
||||||
|
Body: "", Index: 42,
|
||||||
|
RequestedReviewers: []*models.User{},
|
||||||
|
Base: &models.PRBranchInfo{
|
||||||
|
Repo: &models.Repository{
|
||||||
|
Owner: &models.User{UserName: "org"},
|
||||||
|
Name: "repo"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
reviews: []*models.PullReview{
|
||||||
|
&models.PullReview{
|
||||||
|
Body: "wow!",
|
||||||
|
Stale: true,
|
||||||
|
State: common.ReviewStateApproved,
|
||||||
|
User: &models.User{
|
||||||
|
UserName: "king",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
maintainers: []string{"king", "bingo"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
approved: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Part of ReviewSet is not approved",
|
||||||
|
prs: pr_review{
|
||||||
|
"org/repo#42": {
|
||||||
|
pr: &models.PullRequest{
|
||||||
|
Body: "PR: foo/bar#10", Index: 42,
|
||||||
|
RequestedReviewers: []*models.User{},
|
||||||
|
Base: &models.PRBranchInfo{
|
||||||
|
Repo: &models.Repository{
|
||||||
|
Owner: &models.User{UserName: "org"},
|
||||||
|
Name: "repo"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
reviews: []*models.PullReview{
|
||||||
|
&models.PullReview{
|
||||||
|
Body: "wow!",
|
||||||
|
State: common.ReviewStateApproved,
|
||||||
|
User: &models.User{
|
||||||
|
UserName: "king",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
maintainers: []string{"king", "bingo"},
|
||||||
|
},
|
||||||
|
"foo/bar#10": {
|
||||||
|
pr: &models.PullRequest{
|
||||||
|
Body: "PR: org/repo#42", Index: 10,
|
||||||
|
RequestedReviewers: []*models.User{},
|
||||||
|
Base: &models.PRBranchInfo{
|
||||||
|
Repo: &models.Repository{
|
||||||
|
Owner: &models.User{UserName: "org"},
|
||||||
|
Name: "bar"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
reviews: []*models.PullReview{
|
||||||
|
&models.PullReview{
|
||||||
|
Body: "wow!",
|
||||||
|
State: common.ReviewStateRequestChanges,
|
||||||
|
User: &models.User{
|
||||||
|
UserName: "king",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
maintainers: []string{"king", "bingo"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
approved: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Review set approved",
|
||||||
|
prs: pr_review{
|
||||||
|
"org/repo#42": {
|
||||||
|
pr: &models.PullRequest{
|
||||||
|
Body: "PR: foo/bar#10", Index: 42,
|
||||||
|
RequestedReviewers: []*models.User{},
|
||||||
|
Base: &models.PRBranchInfo{
|
||||||
|
Repo: &models.Repository{
|
||||||
|
Owner: &models.User{UserName: "org"},
|
||||||
|
Name: "repo"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
reviews: []*models.PullReview{
|
||||||
|
&models.PullReview{
|
||||||
|
Body: "wow!",
|
||||||
|
State: common.ReviewStateApproved,
|
||||||
|
User: &models.User{
|
||||||
|
UserName: "king",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
maintainers: []string{"king", "bingo"},
|
||||||
|
},
|
||||||
|
"foo/bar#10": {
|
||||||
|
pr: &models.PullRequest{
|
||||||
|
Body: "PR: org/repo#42", Index: 10,
|
||||||
|
RequestedReviewers: []*models.User{},
|
||||||
|
Base: &models.PRBranchInfo{
|
||||||
|
Repo: &models.Repository{
|
||||||
|
Owner: &models.User{UserName: "org"},
|
||||||
|
Name: "bar"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
reviews: []*models.PullReview{
|
||||||
|
&models.PullReview{
|
||||||
|
Body: "wow!",
|
||||||
|
State: common.ReviewStateApproved,
|
||||||
|
User: &models.User{
|
||||||
|
UserName: "king",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
maintainers: []string{"king", "bingo"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
approved: true,
|
approved: true,
|
||||||
},
|
},
|
||||||
@ -58,21 +260,25 @@ func TestReviewApproval(t *testing.T) {
|
|||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
ctl := gomock.NewController(t)
|
ctl := gomock.NewController(t)
|
||||||
mi := mock_common.NewMockGiteaMaintainershipInterface(ctl)
|
pr := mock_main.NewMockGiteaPRInterface(ctl)
|
||||||
pri := mock_main.NewMockGiteaPRInterface(ctl)
|
maintainership := mock_main.NewMockMaintainershipData(ctl)
|
||||||
|
|
||||||
pri.EXPECT().GetPullRequestAndReviews("foo", common.DefaultGitPrj, int64(10)).
|
for params, prs := range test.prs {
|
||||||
Return(test.pr, test.reviews, nil)
|
_, data := common.ExtractDescriptionAndPRs(bufio.NewScanner(strings.NewReader("PR: " + params)))
|
||||||
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(test.maintainerFile, nil)
|
if len(data) != 1 {
|
||||||
|
t.Fatal("bad test setup, fix")
|
||||||
approved, err := IsPrjGitPRApproved(mi, pri, config, 10)
|
}
|
||||||
if approved != test.approved {
|
pr.EXPECT().GetPullRequestAndReviews(data[0].Org, data[0].Repo, data[0].Num).
|
||||||
t.Error("Unexpected approve state:", approved, "vs. expected", test.approved, ", or err:", err)
|
Return(prs.pr, prs.reviews, nil)
|
||||||
|
maintainership.EXPECT().ListPackageMaintainers(data[0].Repo).Return(prs.maintainers)
|
||||||
}
|
}
|
||||||
if err != nil {
|
|
||||||
t.Error("Unexpected error", err)
|
info, _ := NewReviewInstance(pr, maintainership, "org", "repo", 42)
|
||||||
|
|
||||||
|
approved, _ := info.IsMaintainerApproved()
|
||||||
|
if test.approved != approved {
|
||||||
|
t.Error("Unexpected approval state:", approved)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user