workflow-pr: maintainership code

This commit is contained in:
Adam Majer 2024-11-28 00:15:32 +01:00
parent 2916ec8da5
commit 21f7da2257
2 changed files with 110 additions and 5 deletions

View File

@ -1,20 +1,56 @@
package main
import "src.opensuse.org/autogits/common"
import (
"encoding/json"
"errors"
"src.opensuse.org/autogits/common"
)
//go:generate mockgen -source=maintainership.go -destination=mock/maintainership.go -typed
const ProjectKey = "*"
type MaintainershipMap map[string]interface{}
type GiteaMaintainershipInterface interface {
FetchMaintainershipFile(org, prj, branch string) ([]byte, error)
}
func MaintainerListForProject(gitea GiteaMaintainershipInterface, org, branch string) ([]string, error) {
_, err := gitea.FetchMaintainershipFile(org, common.DefaultGitPrj, branch)
if err != nil {
data, err := gitea.FetchMaintainershipFile(org, common.DefaultGitPrj, branch)
if err != nil || data == nil {
return nil, err
}
return nil, nil
maintainer := make(MaintainershipMap)
if err := json.Unmarshal(data, &maintainer); err != nil {
return nil, err
}
m, found := maintainer[ProjectKey]
if !found {
return nil, nil
}
invalidTypeErr := errors.New("Invalid type")
switch m.(type) {
case []interface{}:
maintainers := make([]string, 0)
for _, v := range m.([]interface{}) {
if _, ok := v.(string); !ok {
return nil, invalidTypeErr
}
maintainers = append(maintainers, v.(string))
}
return maintainers, nil
case string:
return []string{m.(string)}, nil
default:
return nil, invalidTypeErr
}
}
func MaintainerListForPackage(gitea GiteaMaintainershipInterface, org, pkg, branch string) ([]string, error) {

View File

@ -16,6 +16,24 @@ func TestMaintainership(t *testing.T) {
}
`)
SinglePrjMaintainershipFile := []byte(`
{
"*": "user"
}
`)
BrokenPrjMaintainershipFile := []byte(`
{
"*": ["user", 4]
}
`)
SingleBrokenPrjMaintainershipFile := []byte(`
{
"*": 4
}
`)
t.Run("No maintainer in empty package", func(t *testing.T) {
ctl := gomock.NewController(t)
mi := mock_main.NewMockGiteaMaintainershipInterface(ctl)
@ -38,6 +56,13 @@ func TestMaintainership(t *testing.T) {
if len(m) != 0 || err != nil {
t.Error("Invalid number of maintainers for project", err)
}
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte("{}"), nil)
m, err = MaintainerListForProject(mi, "foo", "bar")
if len(m) != 0 || err != nil {
t.Error("Invalid number of maintainers for project", err)
}
})
t.Run("Error in MaintainerListForPackage when remote has an error", func(t *testing.T) {
@ -75,7 +100,7 @@ func TestMaintainership(t *testing.T) {
}
})
t.Run("Project maintainers", func(t *testing.T) {
t.Run("Multiple project maintainers", func(t *testing.T) {
ctl := gomock.NewController(t)
mi := mock_main.NewMockGiteaMaintainershipInterface(ctl)
@ -85,5 +110,49 @@ func TestMaintainership(t *testing.T) {
if len(m) != 2 || err != nil {
t.Error("Invalid number of maintainers for project", err)
}
if m[0] != "user1" || m[1] != "user2" {
t.Error("Can't find expected users. Found", m)
}
})
t.Run("Single project maintainer", func(t *testing.T) {
ctl := gomock.NewController(t)
mi := mock_main.NewMockGiteaMaintainershipInterface(ctl)
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(SinglePrjMaintainershipFile, nil)
m, err := MaintainerListForProject(mi, "foo", "bar")
if len(m) != 1 || err != nil {
t.Error("Invalid number of maintainers for project", err)
}
if m[0] != "user" {
t.Error("Can't find expected users. Found", m)
}
})
t.Run("Invalid list of project maintainers", func(t *testing.T) {
ctl := gomock.NewController(t)
mi := mock_main.NewMockGiteaMaintainershipInterface(ctl)
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(BrokenPrjMaintainershipFile, nil)
m, err := MaintainerListForProject(mi, "foo", "bar")
if len(m) != 0 || err.Error() != "Invalid type" {
t.Error("Invalid number of maintainers for project", err)
}
})
t.Run("Invalid list of project maintainers", func(t *testing.T) {
ctl := gomock.NewController(t)
mi := mock_main.NewMockGiteaMaintainershipInterface(ctl)
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(SingleBrokenPrjMaintainershipFile, nil)
_, err := MaintainerListForProject(mi, "foo", "bar")
if err.Error() != "Invalid type" {
t.Error("Invalid number of maintainers for project", err)
}
})
}