autogits/workflow-pr/review.go
2024-12-17 18:19:04 +01:00

64 lines
1.4 KiB
Go

package main
import (
"bufio"
"errors"
"slices"
"strings"
"src.opensuse.org/autogits/common"
"src.opensuse.org/autogits/common/gitea-generated/models"
)
type Review interface {
IsApproved() (bool, error)
}
type PRInfo struct {
pr *models.PullRequest
reviews []*models.PullReview
}
type ReviewSet struct {
maintainers MaintainershipData
prs []PRInfo
}
func readPRData(gitea common.GiteaPRReviewFetcher, org, repo string, num int64, currentSet []PRInfo) ([]PRInfo, error) {
for _, p := range currentSet {
if num == p.pr.Index && repo == p.pr.Base.Repo.Name && org == p.pr.Base.Repo.Owner.UserName {
return nil, nil
}
}
pr, _, err := gitea.GetPullRequestAndReviews(org, repo, num)
if err != nil {
return nil, err
}
_, refPRs := common.ExtractDescriptionAndPRs(bufio.NewScanner(strings.NewReader(pr.Body)))
if len(refPRs) < 1 {
return nil, errors.New("missing links")
}
retSet := []PRInfo{PRInfo{pr: pr}}
for _, prdata := range refPRs {
data, err := readPRData(gitea, prdata.Org, prdata.Repo, prdata.Num, slices.Concat(currentSet, retSet))
if err != nil {
return nil, err
}
retSet = slices.Concat(retSet, data)
}
return retSet, nil
}
func FetchReviewSet(gitea common.GiteaPRReviewFetcher, org, repo string, num int64, config *common.AutogitConfig) (*ReviewSet, error) {
prs, err := readPRData(gitea, org, repo, num, nil)
if err != nil {
return nil, err
}
return &ReviewSet{prs: prs}, nil
}