autogits/workflow-pr/maintainership.go

72 lines
1.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 (
"encoding/json"
"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-27 17:50:55 +01:00
type GiteaMaintainershipInterface interface {
2024-11-29 17:33:01 +01:00
FetchMaintainershipFile(org, prjGit, branch string) ([]byte, error)
GetPullRequestAndReviews(org, pkg string, num int64) (*models.PullRequest, []*models.PullReview, error)
2024-11-27 17:50:55 +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-11-29 17:33:01 +01:00
func ProjectMaintainershipData(gitea GiteaMaintainershipInterface, org, prjGit, branch string) (MaintainershipMap, error) {
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
}
func CheckIfMaintainersApproved(gitea GiteaMaintainershipInterface, config common.AutogitConfig, prjGitPRNumber int64) (bool, error) {
2024-12-02 10:26:51 +01:00
pr, reviews, _ := gitea.GetPullRequestAndReviews(config.Organization, config.GitProjectName, prjGitPRNumber)
data, _ := gitea.FetchMaintainershipFile(config.Organization, config.GitProjectName, config.Branch)
2024-11-29 17:33:01 +01:00
2024-12-02 10:26:51 +01:00
maintainers, _ := parseMaintainershipData(data)
2024-11-29 17:33:01 +01:00
return false, nil
2024-11-27 17:50:55 +01:00
}