autogits/workflow-pr/review_old.go

77 lines
1.9 KiB
Go
Raw Normal View History

2024-12-11 14:41:51 +01:00
package main
import (
"bufio"
"slices"
"strings"
"src.opensuse.org/autogits/common"
"src.opensuse.org/autogits/common/gitea-generated/models"
)
2024-12-12 19:16:32 +01:00
func fetchPRInfo(gitea GiteaPRInterface, pr common.BasicPR) PRInfo {
data, reviews, _ := gitea.GetPullRequestAndReviews(pr.Org, pr.Repo, pr.Num)
return PRInfo{
pr: data,
2024-12-11 14:41:51 +01:00
reviews: reviews,
2024-12-12 19:16:32 +01:00
}
2024-12-11 14:41:51 +01:00
}
2024-12-12 19:16:32 +01:00
func (rs *ReviewSet) appendPR(gitea GiteaPRInterface, pr common.BasicPR) {
if slices.ContainsFunc(rs.prs, func(elem PRInfo) bool {
return pr.Org == elem.pr.Base.Repo.Owner.UserName &&
pr.Repo == elem.pr.Base.Repo.Name &&
pr.Num == elem.pr.Index
}) {
return
2024-12-11 14:41:51 +01:00
}
2024-12-12 19:16:32 +01:00
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)
}
2024-12-11 14:41:51 +01:00
}
2024-12-12 19:16:32 +01:00
func NewReviewInstance(gitea GiteaPRInterface, maintainers MaintainershipData, org, repo string, prNum int) (*ReviewSet, error) {
ret := &ReviewSet{
maintainers: maintainers,
prs: []PRInfo{},
}
2024-12-11 14:41:51 +01:00
2024-12-12 19:16:32 +01:00
ret.appendPR(gitea, common.BasicPR{Org: org, Repo: repo, Num: int64(prNum)})
return ret, nil
}
2024-12-11 14:41:51 +01:00
2024-12-12 19:16:32 +01:00
func isReviewMaintainerApproved(pkgMaintainersList []string, review *models.PullReview) bool {
if review.State != common.ReviewStateApproved || review.Stale {
return false;
}
2024-12-11 14:41:51 +01:00
2024-12-12 19:16:32 +01:00
if slices.Contains(pkgMaintainersList, review.User.UserName) {
return true
}
2024-12-11 14:41:51 +01:00
2024-12-12 19:16:32 +01:00
return false;
}
2024-12-11 14:41:51 +01:00
2024-12-12 19:16:32 +01:00
func (prinfo *PRInfo) isMaintainerApproved(pkgMaintainersList []string) bool {
for _, review := range prinfo.reviews {
if isReviewMaintainerApproved(pkgMaintainersList, review) {
return true
2024-12-11 14:41:51 +01:00
}
}
2024-12-12 19:16:32 +01:00
return false
}
2024-12-11 14:41:51 +01:00
2024-12-12 19:16:32 +01:00
func (rs *ReviewSet) IsMaintainerApproved() (bool, error) {
for _, prinfo := range rs.prs {
pkgMaintainerList := rs.maintainers.ListPackageMaintainers(prinfo.pr.Base.Repo.Name)
if !prinfo.isMaintainerApproved(pkgMaintainerList) {
return false, nil
2024-12-11 14:41:51 +01:00
}
}
2024-12-12 19:16:32 +01:00
return true, nil
2024-12-11 14:41:51 +01:00
}