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, pkg, branch string) (MaintainershipMap, error) { data, err := gitea.FetchMaintainershipFile(org, pkg, 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, org, branch string) ([]string, error) { maintainer, err := parseMaintainerhipData(gitea, org, common.DefaultGitPrj, branch) if err != nil { return nil, err } m, found := maintainer[ProjectKey] if !found { return nil, nil } return m, nil } func MaintainerListForPackage(gitea GiteaMaintainershipInterface, org, pkg, branch string) ([]string, error) { maintainer, err := parseMaintainerhipData(gitea, org, pkg, 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 }