Skip to content
Snippets Groups Projects
Commit c58af0bf authored by Christoph Witzko's avatar Christoph Witzko
Browse files

feat: add slug to plugin info

parent feb827a3
No related branches found
No related tags found
No related merge requests found
...@@ -20,6 +20,7 @@ type PluginInfo struct { ...@@ -20,6 +20,7 @@ type PluginInfo struct {
ShortNormalizedName string ShortNormalizedName string
Constraint *semver.Constraints Constraint *semver.Constraints
Resolver string Resolver string
RepoSlug string
PluginPath string PluginPath string
BinPath string BinPath string
} }
...@@ -42,14 +43,17 @@ func normalizedPluginType(t string) string { ...@@ -42,14 +43,17 @@ func normalizedPluginType(t string) string {
return "" return ""
} }
var nameNormalizer = strings.NewReplacer(":", "-", "/", "-")
func GetPluginInfo(pluginType, pluginName string) (*PluginInfo, error) { func GetPluginInfo(pluginType, pluginName string) (*PluginInfo, error) {
nPluginType := normalizedPluginType(pluginType) nPluginType := normalizedPluginType(pluginType)
if nPluginType == "" { if nPluginType == "" {
return nil, fmt.Errorf("invalid plugin type: %s", pluginType) return nil, fmt.Errorf("invalid plugin type: %s", pluginType)
} }
resolver := "default" resolver := "default"
repoSlug := ""
name := pluginName name := pluginName
normalizedName := fmt.Sprintf("%s-%s", nPluginType, pluginName) normalizedName := nameNormalizer.Replace(fmt.Sprintf("%s-%s", nPluginType, pluginName))
if strings.Contains(name, ":") { if strings.Contains(name, ":") {
parts := strings.Split(name, ":") parts := strings.Split(name, ":")
...@@ -58,16 +62,20 @@ func GetPluginInfo(pluginType, pluginName string) (*PluginInfo, error) { ...@@ -58,16 +62,20 @@ func GetPluginInfo(pluginType, pluginName string) (*PluginInfo, error) {
} }
resolver = parts[0] resolver = parts[0]
name = parts[1] name = parts[1]
normalizedName = strings.ReplaceAll(normalizedName, ":", "-")
} }
if strings.Contains(name, "/") { if strings.Contains(name, "/") {
parts := strings.Split(name, "/") slashParts := strings.Split(name, "/")
if len(parts) != 2 { pName := slashParts[len(slashParts)-1]
return nil, fmt.Errorf("invalid plugin name format") // remove version constraint from the slug
if strings.Contains(pName, "@") {
nameWithoutVersion, _, _ := strings.Cut(pName, "@")
repoSlug = strings.Join(slashParts[:len(slashParts)-1], "/") + "/" + nameWithoutVersion
} else {
repoSlug = name
} }
name = parts[1] // the last part is the plugin name
normalizedName = strings.ReplaceAll(normalizedName, "/", "-") name = pName
} }
var constraint *semver.Constraints var constraint *semver.Constraints
...@@ -82,7 +90,10 @@ func GetPluginInfo(pluginType, pluginName string) (*PluginInfo, error) { ...@@ -82,7 +90,10 @@ func GetPluginInfo(pluginType, pluginName string) (*PluginInfo, error) {
} }
name = parts[0] name = parts[0]
constraint = v constraint = v
normalizedName, _, _ = strings.Cut(normalizedName, "@")
// remove version constraint from the normalized name
normalizedParts := strings.Split(normalizedName, "@")
normalizedName = strings.Join(normalizedParts[:len(normalizedParts)-1], "@")
} }
return &PluginInfo{ return &PluginInfo{
...@@ -92,5 +103,6 @@ func GetPluginInfo(pluginType, pluginName string) (*PluginInfo, error) { ...@@ -92,5 +103,6 @@ func GetPluginInfo(pluginType, pluginName string) (*PluginInfo, error) {
ShortNormalizedName: fmt.Sprintf("%s-%s", nPluginType, name), ShortNormalizedName: fmt.Sprintf("%s-%s", nPluginType, name),
Constraint: constraint, Constraint: constraint,
Resolver: resolver, Resolver: resolver,
RepoSlug: repoSlug,
}, nil }, nil
} }
...@@ -32,6 +32,7 @@ func TestGetPluginInfo(t *testing.T) { ...@@ -32,6 +32,7 @@ func TestGetPluginInfo(t *testing.T) {
NormalizedName: "provider-github-myorg-myplugin", NormalizedName: "provider-github-myorg-myplugin",
ShortNormalizedName: "provider-myplugin", ShortNormalizedName: "provider-myplugin",
Resolver: "github", Resolver: "github",
RepoSlug: "myorg/myplugin",
}}, }},
{"ci_condition", "github:myorg/myplugin", &PluginInfo{ {"ci_condition", "github:myorg/myplugin", &PluginInfo{
Type: "ci_condition", Type: "ci_condition",
...@@ -39,6 +40,7 @@ func TestGetPluginInfo(t *testing.T) { ...@@ -39,6 +40,7 @@ func TestGetPluginInfo(t *testing.T) {
NormalizedName: "condition-github-myorg-myplugin", NormalizedName: "condition-github-myorg-myplugin",
ShortNormalizedName: "condition-myplugin", ShortNormalizedName: "condition-myplugin",
Resolver: "github", Resolver: "github",
RepoSlug: "myorg/myplugin",
}}, }},
{"provider", "github:myorg/myplugin@^1.0.0", &PluginInfo{ {"provider", "github:myorg/myplugin@^1.0.0", &PluginInfo{
Type: "provider", Type: "provider",
...@@ -46,6 +48,7 @@ func TestGetPluginInfo(t *testing.T) { ...@@ -46,6 +48,7 @@ func TestGetPluginInfo(t *testing.T) {
NormalizedName: "provider-github-myorg-myplugin", NormalizedName: "provider-github-myorg-myplugin",
ShortNormalizedName: "provider-myplugin", ShortNormalizedName: "provider-myplugin",
Resolver: "github", Resolver: "github",
RepoSlug: "myorg/myplugin",
Constraint: parseConstraint("^1.0.0"), Constraint: parseConstraint("^1.0.0"),
}}, }},
{"provider", "git@2.0.0", &PluginInfo{ {"provider", "git@2.0.0", &PluginInfo{
...@@ -63,6 +66,23 @@ func TestGetPluginInfo(t *testing.T) { ...@@ -63,6 +66,23 @@ func TestGetPluginInfo(t *testing.T) {
ShortNormalizedName: "hooks-logger", ShortNormalizedName: "hooks-logger",
Resolver: "registry", Resolver: "registry",
}}, }},
{"hooks", "myresolver:@myorg/myplugin", &PluginInfo{
Type: "hooks",
Name: "myplugin",
NormalizedName: "hooks-myresolver-@myorg-myplugin",
ShortNormalizedName: "hooks-myplugin",
Resolver: "myresolver",
RepoSlug: "@myorg/myplugin",
}},
{"hooks", "myresolver:@myorg/myplugin@1.2.3", &PluginInfo{
Type: "hooks",
Name: "myplugin",
NormalizedName: "hooks-myresolver-@myorg-myplugin",
ShortNormalizedName: "hooks-myplugin",
Resolver: "myresolver",
RepoSlug: "@myorg/myplugin",
Constraint: parseConstraint("1.2.3"),
}},
} }
for _, testCase := range testCases { for _, testCase := range testCases {
results, err := GetPluginInfo(testCase.t, testCase.input) results, err := GetPluginInfo(testCase.t, testCase.input)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment