autogits/workflow-pr/maintainership.go

110 lines
2.9 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"
"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
}
func fetchAllAssociatedPRs(gitea GiteaPRInterface, org, repo string, prNum int64) ([]PRReviewInfo, error) {
pr, reviews, err := gitea.GetPullRequestAndReviews(org, repo, prNum)
if err != nil {
return nil, err
}
ret := make([]PRReviewInfo, 1, 2)
ret[0].pr = pr
ret[0].reviews = reviews
return ret, nil
}
func CheckIfMaintainersApproved(gitea common.GiteaMaintainershipInterface, giteapr GiteaPRInterface, config common.AutogitConfig, prjGitPRNumber int64) (bool, error) {
prs, _ := fetchAllAssociatedPRs(giteapr, config.Organization, config.GitProjectName, prjGitPRNumber)
2024-12-02 10:26:51 +01:00
data, _ := gitea.FetchMaintainershipFile(config.Organization, config.GitProjectName, config.Branch)
2024-11-29 17:33:01 +01:00
2024-12-09 00:39:55 +01:00
for _, pr := range prs {
_, associatedPRs := common.ExtractPRsFromDescription(bufio.NewScanner(strings.NewReader(pr.pr.Body)))
if len(associatedPRs) == 0 {
// no associated packages with this PR
break
}
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 {
return false, fmt.Errorf("Associated PR (%s/%s#%d) not linking back to prj PR (%s/%s#%d)",
associatedPRs[0].Org, associatedPRs[0].Repo, associatedPRs[0].Num,
config.Organization, config.GitProjectName, prjGitPRNumber)
}
}
parseMaintainershipData(data)
2024-11-29 17:33:01 +01:00
return false, nil
2024-11-27 17:50:55 +01:00
}