This commit is contained in:
2025-03-07 17:40:59 +01:00
parent c63a56bc4e
commit debbee17eb
3 changed files with 166 additions and 5 deletions

View File

@@ -1,6 +1,57 @@
package workflowdirect
package main
import (
"encoding/json"
"fmt"
"slices"
"strings"
"src.opensuse.org/autogits/common"
)
type OrgLinks struct {
Source string // org/pkg
Target string // pkg
SOid, TOid string // source and target oids
}
func FetchProjectLinksFile(org, prj string) ([]byte, error) {
return nil, nil
}
func ParseProjectLinks(data []byte) ([]*OrgLinks, error) {
values := make([]*OrgLinks, 0, 100)
if len(data) == 0 {
return values, nil
}
if err := json.Unmarshal(data, &values); err != nil {
return nil, err
}
slices.SortFunc(values, func(a, b *OrgLinks) int {
return strings.Compare(a.Target, b.Target)
})
for _, link := range values {
if len(link.Source) < 3 {
return nil, fmt.Errorf("Invalid Source: %s", link.Source)
}
source_link := strings.Split(link.Source, "/")
if len(source_link) != 2 || len(source_link[0]) < 1 || len(source_link[1]) < 1 {
return nil, fmt.Errorf("Invalid Source: %s", link.Source)
}
if len(link.Target) < 1 {
return nil, fmt.Errorf("Missing Target for Source link: %s", link.Source)
}
}
return values, nil
}
func (link *OrgLinks) StateUpdater(git common.Git) {
}

View File

@@ -1,3 +1,114 @@
package workflowdirect_test
package main
// TODO, like documentation :-)
import (
"strings"
"testing"
)
func TestLinkParsing(t *testing.T) {
tests := []struct {
name string
data []byte
links []*OrgLinks
expected_err string
}{
{
name: "No links file",
},
{
name: "Empty links file",
data: []byte("[]"),
},
{
name: "Single package linked",
data: []byte(`[{"Source": "pool/foo", "Target": "pkg"}]`),
links: []*OrgLinks{
&OrgLinks{
Source: "pool/foo",
Target: "pkg",
},
},
},
{
name: "Multiple packages linked, resorted",
data: []byte(`[{"Source": "pool/foo", "Target": "pkg"}, {"Source": "pool/abc", "Target": "abc"}, {"Source": "abc/aaa", "Target": "zzz"}]`),
links: []*OrgLinks{
&OrgLinks{
Source: "pool/abc",
Target: "abc",
},
&OrgLinks{
Source: "pool/foo",
Target: "pkg",
},
&OrgLinks{
Source: "abc/aaa",
Target: "zzz",
},
},
},
{
name: "Broken source file",
data: []byte(`[{"Source": "pool/foo"}, Source": "pool/abc", "Target": "abc"}, {"Source": "abc/aaa", "Target": "zzz"}]`),
expected_err: "invalid character 'S' looking for beginning of value",
},
{
name: "Missing target",
data: []byte(`[{"Source": "abc/aaa"}]`),
expected_err: "Missing Target for Source link: abc/aaa",
},
{
name: "Bad source link",
data: []byte(`[{"Source": "abc"}]`),
expected_err: "Invalid Source: abc",
},
{
name: "Bad source link",
data: []byte(`[{"Source": "/foo"}]`),
expected_err: "Invalid Source: /foo",
},
{
name: "Bad source link",
data: []byte(`[{"Source": "foo/"}]`),
expected_err: "Invalid Source: foo/",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
res, err := ParseProjectLinks(test.data)
if len(test.expected_err) > 0 && err == nil {
t.Error("Expected an error:", test.expected_err, "; but got nothing")
} else if err != nil && len(test.expected_err) == 0 {
t.Fatal("Unexpected error:", err)
} else if err != nil && !strings.Contains(err.Error(), test.expected_err) {
t.Fatal("Expected an error:", test.expected_err, "; but got:", err)
}
if len(res) != len(test.links) {
t.Fatal("len of result", len(res), "vs. expected", len(test.links))
}
// verify that returned data is present and in expected order
for i := range test.links {
if *test.links[i] != *res[i] {
t.Error("index", i, "expected", *test.links[i], "received", *res[i])
}
}
})
}
}
func TestLinkUpdater(t *testing.T) {
tests := []struct {
name string
mock_setup func()
}{
{
name: "no-op update",
},
}
for _, test := range tests {
}
}

View File

@@ -31,7 +31,6 @@ import (
"time"
"src.opensuse.org/autogits/common"
workflowdirect "src.opensuse.org/autogits/workflow-direct"
)
const (
@@ -43,7 +42,7 @@ const (
var configuredRepos map[string][]*common.AutogitConfig
var gitea common.Gitea
var orgLinks map[string]*workflowdirect.OrgLinks
var orgLinks map[string]*OrgLinks
func isConfiguredOrg(org *common.Organization) bool {
_, found := configuredRepos[org.Username]