110 lines
2.9 KiB
Go
110 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"src.opensuse.org/autogits/common"
|
|
"src.opensuse.org/autogits/common/gitea-generated/models"
|
|
)
|
|
|
|
//go:generate mockgen -source=maintainership.go -destination=mock/maintainership.go -typed
|
|
|
|
const ProjectKey = ""
|
|
|
|
type MaintainershipMap map[string][]string
|
|
|
|
func parseMaintainershipData(data []byte) (MaintainershipMap, error) {
|
|
maintainers := make(MaintainershipMap)
|
|
if err := json.Unmarshal(data, &maintainers); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return maintainers, nil
|
|
}
|
|
|
|
func ProjectMaintainershipData(gitea common.GiteaMaintainershipInterface, org, prjGit, branch string) (MaintainershipMap, error) {
|
|
data, err := gitea.FetchMaintainershipFile(org, prjGit, branch)
|
|
if err != nil || data == nil {
|
|
return nil, err
|
|
}
|
|
|
|
return parseMaintainershipData(data)
|
|
}
|
|
|
|
func MaintainerListForProject(maintainers MaintainershipMap) []string {
|
|
m, found := maintainers[ProjectKey]
|
|
if !found {
|
|
return nil
|
|
}
|
|
|
|
return m
|
|
}
|
|
|
|
func MaintainerListForPackage(maintainers MaintainershipMap, pkg string) []string {
|
|
pkgMaintainers := maintainers[pkg]
|
|
prjMaintainers := maintainers[ProjectKey]
|
|
|
|
prjMaintainer:
|
|
for _, prjm := range prjMaintainers {
|
|
for i := range pkgMaintainers {
|
|
if pkgMaintainers[i] == prjm {
|
|
continue prjMaintainer
|
|
}
|
|
}
|
|
pkgMaintainers = append(pkgMaintainers, prjm)
|
|
}
|
|
|
|
return pkgMaintainers
|
|
}
|
|
|
|
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)
|
|
data, _ := gitea.FetchMaintainershipFile(config.Organization, config.GitProjectName, config.Branch)
|
|
|
|
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)
|
|
|
|
return false, nil
|
|
}
|