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
|
|
|
|
|
2024-12-11 14:41:51 +01:00
|
|
|
type MaintainershipData interface {
|
|
|
|
ListProjectMaintainers() []string
|
|
|
|
ListPackageMaintainers(pkg string) []string
|
|
|
|
}
|
|
|
|
|
2024-11-28 17:25:32 +01:00
|
|
|
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-12-11 14:41:51 +01:00
|
|
|
func parseMaintainershipData(data []byte) (*MaintainershipMap, error) {
|
2024-11-29 17:33:01 +01:00
|
|
|
maintainers := make(MaintainershipMap)
|
|
|
|
if err := json.Unmarshal(data, &maintainers); err != nil {
|
2024-11-27 17:50:55 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-12-11 14:41:51 +01:00
|
|
|
return &maintainers, nil
|
2024-11-28 17:10:26 +01:00
|
|
|
}
|
|
|
|
|
2024-12-11 14:41:51 +01:00
|
|
|
func FetchProjectMaintainershipData(gitea common.GiteaMaintainershipInterface, org, prjGit, branch string) (*MaintainershipMap, error) {
|
2024-11-29 17:33:01 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-12-11 14:41:51 +01:00
|
|
|
func (data *MaintainershipMap) ListProjectMaintainers() []string {
|
|
|
|
if data == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
m, found := (*data)[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-12-11 14:41:51 +01:00
|
|
|
func (data *MaintainershipMap) ListPackageMaintainers(pkg string) []string {
|
|
|
|
if data == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pkgMaintainers := (*data)[pkg]
|
|
|
|
prjMaintainers := data.ListProjectMaintainers()
|
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
|
|
|
|
}
|
|
|
|
|