autogits/workflow-pr/maintainership.go
2024-12-11 14:41:51 +01:00

72 lines
1.5 KiB
Go

package main
import (
"encoding/json"
"src.opensuse.org/autogits/common"
)
//go:generate mockgen -source=maintainership.go -destination=mock/maintainership.go -typed
type MaintainershipData interface {
ListProjectMaintainers() []string
ListPackageMaintainers(pkg string) []string
}
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 FetchProjectMaintainershipData(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 (data *MaintainershipMap) ListProjectMaintainers() []string {
if data == nil {
return nil
}
m, found := (*data)[ProjectKey]
if !found {
return nil
}
return m
}
func (data *MaintainershipMap) ListPackageMaintainers(pkg string) []string {
if data == nil {
return nil
}
pkgMaintainers := (*data)[pkg]
prjMaintainers := data.ListProjectMaintainers()
prjMaintainer:
for _, prjm := range prjMaintainers {
for i := range pkgMaintainers {
if pkgMaintainers[i] == prjm {
continue prjMaintainer
}
}
pkgMaintainers = append(pkgMaintainers, prjm)
}
return pkgMaintainers
}