From bb2c5a2b3823b2239341e56810b6e092145e6275 Mon Sep 17 00:00:00 2001 From: Kent Rancourt Date: Thu, 1 Feb 2024 18:50:53 -0500 Subject: [PATCH] corresponding webbook changes Signed-off-by: Kent Rancourt --- internal/webhook/warehouse/webhook.go | 29 ++++++++++++++++- internal/webhook/warehouse/webhook_test.go | 31 ++++++++++++++++++- .../project-details/nodes/repo-node.tsx | 4 +-- 3 files changed, 60 insertions(+), 4 deletions(-) diff --git a/internal/webhook/warehouse/webhook.go b/internal/webhook/warehouse/webhook.go index 3dcdf850e8..1b3d97a65c 100644 --- a/internal/webhook/warehouse/webhook.go +++ b/internal/webhook/warehouse/webhook.go @@ -3,6 +3,7 @@ package warehouse import ( "context" "fmt" + "strings" "github.com/Masterminds/semver" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -179,15 +180,41 @@ func (w *webhook) validateImageSub( return errs } +// TODO: KR: This needs to be modified to make name required if the chart URL +// starts with http/s and forbidden if it starts with oci:// func (w *webhook) validateChartSub( f *field.Path, sub kargoapi.ChartSubscription, ) field.ErrorList { + errs := field.ErrorList{} if err := validateSemverConstraint( f.Child("semverConstraint"), sub.SemverConstraint, ); err != nil { - return field.ErrorList{err} + errs = append(errs, err) + } + if strings.HasPrefix(sub.RepoURL, "oci://") && sub.Name != "" { + errs = append( + errs, + field.Invalid( + f.Child("name"), + sub.Name, + "must be empty if repoURL starts with oci://", + ), + ) + } + if (strings.HasPrefix(sub.RepoURL, "http://") || strings.HasPrefix(sub.RepoURL, "https://")) && sub.Name == "" { + errs = append( + errs, + field.Invalid( + f.Child("name"), + sub.Name, + "must be non-empty if repoURL starts with http:// or https://", + ), + ) + } + if len(errs) > 0 { + return errs } return nil } diff --git a/internal/webhook/warehouse/webhook_test.go b/internal/webhook/warehouse/webhook_test.go index 03988ccd58..a8ec09bd4f 100644 --- a/internal/webhook/warehouse/webhook_test.go +++ b/internal/webhook/warehouse/webhook_test.go @@ -490,8 +490,10 @@ func TestValidateChartSub(t *testing.T) { assertions func(field.ErrorList) }{ { - name: "invalid", + name: "invalid semverConstraint and oci repoURL with name", sub: kargoapi.ChartSubscription{ + RepoURL: "oci://fake-url", + Name: "should-not-be-here", SemverConstraint: "bogus", }, assertions: func(errs field.ErrorList) { @@ -503,6 +505,33 @@ func TestValidateChartSub(t *testing.T) { Field: "chart.semverConstraint", BadValue: "bogus", }, + { + Type: field.ErrorTypeInvalid, + Field: "chart.name", + BadValue: "should-not-be-here", + Detail: "must be empty if repoURL starts with oci://", + }, + }, + errs, + ) + }, + }, + + { + name: "https repoURL without name", + sub: kargoapi.ChartSubscription{ + RepoURL: "https://fake-url", + }, + assertions: func(errs field.ErrorList) { + require.Equal( + t, + field.ErrorList{ + { + Type: field.ErrorTypeInvalid, + Field: "chart.name", + BadValue: "", + Detail: "must be non-empty if repoURL starts with http:// or https://", + }, }, errs, ) diff --git a/ui/src/features/project/project-details/nodes/repo-node.tsx b/ui/src/features/project/project-details/nodes/repo-node.tsx index 67e6e1343d..2abc8bacf2 100644 --- a/ui/src/features/project/project-details/nodes/repo-node.tsx +++ b/ui/src/features/project/project-details/nodes/repo-node.tsx @@ -25,7 +25,7 @@ export const RepoNode = ({ nodeData, children }: Props) => { const type = nodeData.type; const value = type === NodeType.REPO_CHART - ? nodeData.data.registryUrl + ? nodeData.data.repoUrl : type === NodeType.WAREHOUSE ? nodeData.data : nodeData.data.repoUrl; @@ -43,7 +43,7 @@ export const RepoNode = ({ nodeData, children }: Props) => {
{nodeData.type !== NodeType.WAREHOUSE && (
- {nodeData.type === NodeType.REPO_CHART ? 'Registry URL' : 'Repo URL'} + Repo URL