fix: repo/owner parsing for gitlab (#4255)

Co-authored-by: 6543 <6543@obermui.de>
This commit is contained in:
Patrick Schratz 2024-10-26 23:20:15 +02:00 committed by GitHub
parent 3d4f9241f5
commit d37c138738
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 68 additions and 1 deletions

View file

@ -303,7 +303,9 @@ func extractFromPath(str string) (string, string, error) {
if len(s) < minPathComponents {
return "", "", fmt.Errorf("minimum match not found")
}
return s[0], s[1], nil
owner := strings.Join(s[:len(s)-1], "/")
name := s[len(s)-1]
return owner, name, nil
}
func convertLabels(from []*gitlab.EventLabel) []string {

View file

@ -300,3 +300,68 @@ func Test_GitLab(t *testing.T) {
})
})
}
func TestExtractFromPath(t *testing.T) {
type testCase struct {
name string
input string
wantOwner string
wantName string
errContains string
}
tests := []testCase{
{
name: "basic two components",
input: "owner/repo",
wantOwner: "owner",
wantName: "repo",
},
{
name: "three components",
input: "owner/group/repo",
wantOwner: "owner/group",
wantName: "repo",
},
{
name: "many components",
input: "owner/group/subgroup/deep/repo",
wantOwner: "owner/group/subgroup/deep",
wantName: "repo",
},
{
name: "empty string",
input: "",
errContains: "minimum match not found",
},
{
name: "single component",
input: "onlyrepo",
errContains: "minimum match not found",
},
{
name: "trailing slash",
input: "owner/repo/",
wantOwner: "owner/repo",
wantName: "",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
owner, name, err := extractFromPath(tc.input)
// Check error expectations
if tc.errContains != "" {
if assert.Error(t, err) {
assert.Contains(t, err.Error(), tc.errContains)
}
return
}
assert.NoError(t, err)
assert.EqualValues(t, tc.wantOwner, owner)
assert.EqualValues(t, tc.wantName, name)
})
}
}