autogits/workflow-pr/maintainership.go

68 lines
1.6 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-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 {
FetchMaintainershipFile(org, prj, branch string) ([]byte, error)
}
func parseMaintainerhipData(gitea GiteaMaintainershipInterface, org, prjGit, branch string) (MaintainershipMap, error) {
data, err := gitea.FetchMaintainershipFile(org, prjGit, branch)
2024-11-28 00:15:32 +01:00
if err != nil || data == nil {
return nil, err
}
maintainer := make(MaintainershipMap)
if err := json.Unmarshal(data, &maintainer); err != nil {
2024-11-27 17:50:55 +01:00
return nil, err
}
2024-11-28 17:10:26 +01:00
return maintainer, nil
}
func MaintainerListForProject(gitea GiteaMaintainershipInterface, config common.AutogitConfig) ([]string, error) {
maintainer, err := parseMaintainerhipData(gitea, config.Organization, config.GitProjectName, config.Branch)
2024-11-28 17:10:26 +01:00
if err != nil {
return nil, err
}
2024-11-28 00:15:32 +01:00
m, found := maintainer[ProjectKey]
if !found {
return nil, nil
}
2024-11-28 17:10:26 +01:00
return m, nil
2024-11-27 17:50:55 +01:00
}
func MaintainerListForPackage(gitea GiteaMaintainershipInterface, config common.AutogitConfig, pkg string) ([]string, error) {
maintainer, err := parseMaintainerhipData(gitea, config.Organization, config.GitProjectName, config.Branch)
2024-11-27 17:50:55 +01:00
if err != nil {
return nil, err
}
2024-11-28 17:10:26 +01:00
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
2024-11-27 17:50:55 +01:00
}