autogits/workflow-pr/maintainership.go

68 lines
1.6 KiB
Go

package main
import (
"encoding/json"
"src.opensuse.org/autogits/common"
)
//go:generate mockgen -source=maintainership.go -destination=mock/maintainership.go -typed
const ProjectKey = ""
type MaintainershipMap map[string][]string
type GiteaMaintainershipInterface interface {
FetchMaintainershipFile(org, prj, branch string) ([]byte, error)
}
func parseMaintainerhipData(gitea GiteaMaintainershipInterface, org, prjGit, branch string) (MaintainershipMap, error) {
data, err := gitea.FetchMaintainershipFile(org, prjGit, branch)
if err != nil || data == nil {
return nil, err
}
maintainer := make(MaintainershipMap)
if err := json.Unmarshal(data, &maintainer); err != nil {
return nil, err
}
return maintainer, nil
}
func MaintainerListForProject(gitea GiteaMaintainershipInterface, config common.AutogitConfig) ([]string, error) {
maintainer, err := parseMaintainerhipData(gitea, config.Organization, config.GitProjectName, config.Branch)
if err != nil {
return nil, err
}
m, found := maintainer[ProjectKey]
if !found {
return nil, nil
}
return m, nil
}
func MaintainerListForPackage(gitea GiteaMaintainershipInterface, config common.AutogitConfig, pkg string) ([]string, error) {
maintainer, err := parseMaintainerhipData(gitea, config.Organization, config.GitProjectName, config.Branch)
if err != nil {
return nil, err
}
pkgMaintainers := maintainer[pkg]
prjMaintainers := maintainer[ProjectKey]
prjMaintainer:
for _, prjm := range prjMaintainers {
for i := range pkgMaintainers {
if pkgMaintainers[i] == prjm {
continue prjMaintainer
}
}
pkgMaintainers = append(pkgMaintainers, prjm)
}
return pkgMaintainers, nil
}