autogits/workflow-pr/maintainership.go

130 lines
3.5 KiB
Go
Raw Normal View History

2024-11-27 17:50:55 +01:00
package main
2024-11-28 00:15:32 +01:00
import (
2024-12-09 00:39:55 +01:00
"bufio"
2024-11-28 00:15:32 +01:00
"encoding/json"
2024-12-09 00:39:55 +01:00
"fmt"
2024-12-09 18:20:56 +01:00
"slices"
2024-12-09 00:39:55 +01:00
"strings"
2024-11-28 00:15:32 +01:00
"src.opensuse.org/autogits/common"
2024-11-29 17:33:01 +01:00
"src.opensuse.org/autogits/common/gitea-generated/models"
2024-11-28 00:15:32 +01:00
)
2024-11-27 17:50:55 +01:00
//go:generate mockgen -source=maintainership.go -destination=mock/maintainership.go -typed
const ProjectKey = ""
2024-11-28 00:15:32 +01:00
2024-11-28 17:10:26 +01:00
type MaintainershipMap map[string][]string
2024-11-28 00:15:32 +01:00
2024-11-29 17:33:01 +01:00
func parseMaintainershipData(data []byte) (MaintainershipMap, error) {
maintainers := make(MaintainershipMap)
if err := json.Unmarshal(data, &maintainers); err != nil {
2024-11-27 17:50:55 +01:00
return nil, err
}
2024-11-29 17:33:01 +01:00
return maintainers, nil
2024-11-28 17:10:26 +01:00
}
2024-12-09 00:39:55 +01:00
func ProjectMaintainershipData(gitea common.GiteaMaintainershipInterface, org, prjGit, branch string) (MaintainershipMap, error) {
2024-11-29 17:33:01 +01:00
data, err := gitea.FetchMaintainershipFile(org, prjGit, branch)
if err != nil || data == nil {
2024-11-28 17:10:26 +01:00
return nil, err
}
2024-11-29 17:33:01 +01:00
return parseMaintainershipData(data)
}
func MaintainerListForProject(maintainers MaintainershipMap) []string {
m, found := maintainers[ProjectKey]
2024-11-28 00:15:32 +01:00
if !found {
2024-11-29 17:33:01 +01:00
return nil
2024-11-28 00:15:32 +01:00
}
2024-11-29 17:33:01 +01:00
return m
2024-11-27 17:50:55 +01:00
}
2024-11-29 17:33:01 +01:00
func MaintainerListForPackage(maintainers MaintainershipMap, pkg string) []string {
pkgMaintainers := maintainers[pkg]
prjMaintainers := maintainers[ProjectKey]
2024-11-28 17:10:26 +01:00
prjMaintainer:
for _, prjm := range prjMaintainers {
for i := range pkgMaintainers {
if pkgMaintainers[i] == prjm {
continue prjMaintainer
}
}
pkgMaintainers = append(pkgMaintainers, prjm)
}
2024-11-29 17:33:01 +01:00
return pkgMaintainers
}
2024-12-09 00:39:55 +01:00
type PRReviewInfo struct {
pr *models.PullRequest
reviews []*models.PullReview
}
2024-12-09 18:20:56 +01:00
func fetchPRandReviews(gitea GiteaPRInterface, org, repo string, prNum int64) (PRReviewInfo, error) {
2024-12-09 00:39:55 +01:00
pr, reviews, err := gitea.GetPullRequestAndReviews(org, repo, prNum)
if err != nil {
2024-12-09 18:20:56 +01:00
return PRReviewInfo{}, err
2024-12-09 00:39:55 +01:00
}
2024-12-09 18:20:56 +01:00
return PRReviewInfo{
pr: pr,
reviews: reviews,
}, nil
}
func isMaintainerApprovedPR(pr PRReviewInfo, maintainers MaintainershipMap) bool {
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
}
}
2024-12-09 00:39:55 +01:00
2024-12-09 18:20:56 +01:00
return true
2024-12-09 00:39:55 +01:00
}
2024-12-09 18:20:56 +01:00
func IsPrjGitPRApproved(gitea common.GiteaMaintainershipInterface, giteapr GiteaPRInterface, config common.AutogitConfig, prjGitPRNumber int64) (bool, error) {
prjPR, _ := fetchPRandReviews(giteapr, config.Organization, config.GitProjectName, prjGitPRNumber)
2024-12-02 10:26:51 +01:00
data, _ := gitea.FetchMaintainershipFile(config.Organization, config.GitProjectName, config.Branch)
2024-12-09 18:20:56 +01:00
maintainers, _ := parseMaintainershipData(data)
2024-11-29 17:33:01 +01:00
2024-12-09 18:20:56 +01:00
_, prjAssociatedPRs := common.ExtractDescriptionAndPRs(bufio.NewScanner(strings.NewReader(prjPR.pr.Body)))
2024-12-09 00:39:55 +01:00
2024-12-09 18:20:56 +01:00
for _, PR := range prjAssociatedPRs {
prInfo, _ := fetchPRandReviews(giteapr, PR.Org, PR.Repo, PR.Num)
_, associatedPRs := common.ExtractDescriptionAndPRs(bufio.NewScanner(strings.NewReader(prInfo.pr.Body)))
2024-12-09 00:39:55 +01:00
if len(associatedPRs) != 1 {
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)
}
if associatedPRs[0].Org != config.Organization || associatedPRs[0].Repo != config.GitProjectName || associatedPRs[0].Num != prjGitPRNumber {
2024-12-09 18:20:56 +01:00
return false, fmt.Errorf("Associated PR (%s/%s#%d) not linking back to prj PR (%s/%s#%d)",
2024-12-09 00:39:55 +01:00
associatedPRs[0].Org, associatedPRs[0].Repo, associatedPRs[0].Num,
config.Organization, config.GitProjectName, prjGitPRNumber)
}
2024-12-09 18:20:56 +01:00
if !isMaintainerApprovedPR(prInfo, maintainers) {
return false, nil
}
}
2024-12-09 00:39:55 +01:00
2024-12-09 18:20:56 +01:00
return true, nil
2024-11-27 17:50:55 +01:00
}