From 21f7da2257d48e8a863afc4456fe25c81f7288bd68357790533dfcc5c4c8cb25 Mon Sep 17 00:00:00 2001 From: Adam Majer Date: Thu, 28 Nov 2024 00:15:32 +0100 Subject: [PATCH] workflow-pr: maintainership code --- workflow-pr/maintainership.go | 44 ++++++++++++++++-- workflow-pr/maintainership_test.go | 71 +++++++++++++++++++++++++++++- 2 files changed, 110 insertions(+), 5 deletions(-) diff --git a/workflow-pr/maintainership.go b/workflow-pr/maintainership.go index ec429a0..5e7c623 100644 --- a/workflow-pr/maintainership.go +++ b/workflow-pr/maintainership.go @@ -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) { diff --git a/workflow-pr/maintainership_test.go b/workflow-pr/maintainership_test.go index 6dfd788..4852fc1 100644 --- a/workflow-pr/maintainership_test.go +++ b/workflow-pr/maintainership_test.go @@ -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) + } }) }