autogits/workflow-pr/pr.go
2025-01-03 00:46:40 +01:00

103 lines
2.4 KiB
Go

package main
import (
"bufio"
"errors"
"slices"
"strings"
"src.opensuse.org/autogits/common"
"src.opensuse.org/autogits/common/gitea-generated/models"
)
type PRInfo struct {
pr *models.PullRequest
}
type PRSet struct {
prs []PRInfo
config *common.AutogitConfig
}
func readPRData(gitea common.GiteaPRFetcher, 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.GetPullRequest(org, repo, num)
if err != nil {
return nil, err
}
_, refPRs := common.ExtractDescriptionAndPRs(bufio.NewScanner(strings.NewReader(pr.Body)))
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 FetchPRSet(gitea common.GiteaPRFetcher, org, repo string, num int64, config *common.AutogitConfig) (*PRSet, error) {
prs, err := readPRData(gitea, org, repo, num, nil)
if err != nil {
return nil, err
}
return &PRSet{prs: prs, config: config}, nil
}
func (rs *PRSet) GetPrjGitPR() (*models.PullRequest, error) {
var ret *models.PullRequest
for _, prinfo := range rs.prs {
if prinfo.pr.Base.Repo.Name == rs.config.GitProjectName && prinfo.pr.Base.Repo.Owner.UserName == rs.config.Organization {
if ret == nil {
ret = prinfo.pr
} else {
return nil, errors.New("Multiple PrjGit PRs in one review set")
}
}
}
if ret != nil {
return ret, nil
}
return nil, errors.New("No PrjGit PR found")
}
func (rs *PRSet) IsConsistent() bool {
prjpr, err := rs.GetPrjGitPR()
if err != nil {
return false
}
_, prjpr_set := common.ExtractDescriptionAndPRs(bufio.NewScanner(strings.NewReader(prjpr.Body)))
if len(prjpr_set) != len(rs.prs)-1 { // 1 to many mapping
return false
}
for _, prinfo := range rs.prs {
if prjpr == prinfo.pr {
continue
}
_, prs := common.ExtractDescriptionAndPRs(bufio.NewScanner(strings.NewReader(prinfo.pr.Body)))
if len(prs) != 1 || prs[0].Repo != prjpr.Base.Repo.Name || prs[0].Org != prjpr.Base.Repo.Owner.UserName || prs[0].Num != prjpr.Index {
return false
}
}
return true
}
func (rs *PRSet) IsReviewed() bool {
return false
}