From fd41fdd9c9e741edaf93155f0ff300c206ee4957 Mon Sep 17 00:00:00 2001 From: Scott Rigby Date: Mon, 25 Aug 2025 11:19:02 -0400 Subject: [PATCH] New registry plugin func GetPluginName. Re-use regsitry.reference Signed-off-by: Scott Rigby --- internal/plugin/installer/oci_installer.go | 20 +---- pkg/registry/plugin.go | 25 ++++++ pkg/registry/plugin_test.go | 93 ++++++++++++++++++++++ 3 files changed, 122 insertions(+), 16 deletions(-) create mode 100644 pkg/registry/plugin_test.go diff --git a/internal/plugin/installer/oci_installer.go b/internal/plugin/installer/oci_installer.go index acb28ccf9..89dd44056 100644 --- a/internal/plugin/installer/oci_installer.go +++ b/internal/plugin/installer/oci_installer.go @@ -24,7 +24,6 @@ import ( "log/slog" "os" "path/filepath" - "strings" "helm.sh/helm/v4/internal/plugin/cache" "helm.sh/helm/v4/internal/third_party/dep/fs" @@ -45,21 +44,10 @@ type OCIInstaller struct { // NewOCIInstaller creates a new OCIInstaller with optional getter options func NewOCIInstaller(source string, options ...getter.Option) (*OCIInstaller, error) { - ref := strings.TrimPrefix(source, fmt.Sprintf("%s://", registry.OCIScheme)) - - // Extract plugin name from OCI reference - // e.g., "ghcr.io/user/plugin-name:v1.0.0" -> "plugin-name" - parts := strings.Split(ref, "/") - if len(parts) < 2 { - return nil, fmt.Errorf("invalid OCI reference: %s", source) - } - lastPart := parts[len(parts)-1] - pluginName := lastPart - if idx := strings.LastIndex(lastPart, ":"); idx > 0 { - pluginName = lastPart[:idx] - } - if idx := strings.LastIndex(lastPart, "@"); idx > 0 { - pluginName = lastPart[:idx] + // Extract plugin name from OCI reference using robust registry parsing + pluginName, err := registry.GetPluginName(source) + if err != nil { + return nil, err } key, err := cache.Key(source) diff --git a/pkg/registry/plugin.go b/pkg/registry/plugin.go index a92aaf452..5d22a99ee 100644 --- a/pkg/registry/plugin.go +++ b/pkg/registry/plugin.go @@ -174,3 +174,28 @@ func PluginPullOptWithPluginName(name string) PluginPullOption { operation.pluginName = name } } + +// GetPluginName extracts the plugin name from an OCI reference using proper reference parsing +func GetPluginName(source string) (string, error) { + ref, err := newReference(source) + if err != nil { + return "", fmt.Errorf("invalid OCI reference: %w", err) + } + + // Extract plugin name from the repository path + // e.g., "ghcr.io/user/plugin-name:v1.0.0" -> Repository: "user/plugin-name" + repository := ref.Repository + if repository == "" { + return "", fmt.Errorf("invalid OCI reference: missing repository") + } + + // Get the last part of the repository path as the plugin name + parts := strings.Split(repository, "/") + pluginName := parts[len(parts)-1] + + if pluginName == "" { + return "", fmt.Errorf("invalid OCI reference: cannot determine plugin name from repository %s", repository) + } + + return pluginName, nil +} diff --git a/pkg/registry/plugin_test.go b/pkg/registry/plugin_test.go new file mode 100644 index 000000000..f8525829c --- /dev/null +++ b/pkg/registry/plugin_test.go @@ -0,0 +1,93 @@ +/* +Copyright The Helm Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package registry + +import ( + "testing" +) + +func TestGetPluginName(t *testing.T) { + tests := []struct { + name string + source string + expected string + expectErr bool + }{ + { + name: "valid OCI reference with tag", + source: "oci://ghcr.io/user/plugin-name:v1.0.0", + expected: "plugin-name", + }, + { + name: "valid OCI reference with digest", + source: "oci://ghcr.io/user/plugin-name@sha256:1234567890abcdef", + expected: "plugin-name", + }, + { + name: "valid OCI reference without tag", + source: "oci://ghcr.io/user/plugin-name", + expected: "plugin-name", + }, + { + name: "valid OCI reference with multiple path segments", + source: "oci://registry.example.com/org/team/plugin-name:latest", + expected: "plugin-name", + }, + { + name: "valid OCI reference with plus signs in tag", + source: "oci://registry.example.com/user/plugin-name:v1.0.0+build.1", + expected: "plugin-name", + }, + { + name: "valid OCI reference - single path segment", + source: "oci://registry.example.com/plugin", + expected: "plugin", + }, + { + name: "invalid OCI reference - no repository", + source: "oci://registry.example.com", + expectErr: true, + }, + { + name: "invalid OCI reference - malformed", + source: "not-an-oci-reference", + expectErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + pluginName, err := GetPluginName(tt.source) + + if tt.expectErr { + if err == nil { + t.Errorf("expected error but got none") + } + return + } + + if err != nil { + t.Errorf("unexpected error: %v", err) + return + } + + if pluginName != tt.expected { + t.Errorf("expected plugin name %q, got %q", tt.expected, pluginName) + } + }) + } +}