From 120f1a6ca040c40aedf0a1e390cb76bd8b20a9df Mon Sep 17 00:00:00 2001 From: Paul Horton Date: Wed, 11 Dec 2024 13:27:14 +0000 Subject: [PATCH 1/2] fix: better handling of accepted characters for App and Org Names and App Public IDs Signed-off-by: Paul Horton --- scm/types.go | 32 ++++++++++++++++---------------- scm/types_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 59 insertions(+), 18 deletions(-) diff --git a/scm/types.go b/scm/types.go index 9674682..75d7268 100644 --- a/scm/types.go +++ b/scm/types.go @@ -32,8 +32,9 @@ const ( ) var ( - INVALID_BRANCH_NAME = regexp.MustCompile(`^\.|([;$!*&|\(\)\[\]<>#?~%'])|[\./]$`) - MULTIPLE_SPACES = regexp.MustCompile(`\s(\s+)`) + INVALID_BRANCH_NAME = regexp.MustCompile(`^\.|([;$!*&|\(\)\[\]<>#?~%'])|[\./]$`) + INVALID_APP_ORG_NAME = regexp.MustCompile(`([^\pL\pN._,\-\s])`) + MULTIPLE_SPACES = regexp.MustCompile(`\s(\s+)`) ) type ScmConfiguration struct { @@ -53,13 +54,15 @@ func (a *Application) PrintTree(depth int) { } func (a *Application) SafeId() string { - return strings.ToLower(strings.Map(func(r rune) rune { - if strings.Contains(BANNED_CHARS_ID, string(r)) { - return '-' - } else { - return r - } - }, a.Name)) + return strings.ToLower(INVALID_APP_ORG_NAME.ReplaceAllString(a.Name, "-")) + + // strings.Map(func(r rune) rune { + // if strings.Contains(BANNED_CHARS_ID, string(r)) { + // return '-' + // } else { + // return r + // } + // }, a.Name)) } func (a *Application) SafeName() string { @@ -127,13 +130,10 @@ func (oc *OrgContents) PrintTree() { func safeName(in string) string { return MULTIPLE_SPACES.ReplaceAllString( - strings.Map(func(r rune) rune { - if strings.Contains(BANNED_CHARS_NAME, string(r)) { - return '-' - } else { - return r - } - }, strings.ReplaceAll(strings.TrimSpace(in), "\t", "-")), + INVALID_APP_ORG_NAME.ReplaceAllString( + strings.ReplaceAll(strings.TrimSpace(in), "\t", "-"), + "-", + ), "-", ) } diff --git a/scm/types_test.go b/scm/types_test.go index 7b292c8..dc55f6a 100644 --- a/scm/types_test.go +++ b/scm/types_test.go @@ -155,8 +155,16 @@ func TestScmSafeName(t *testing.T) { expected: "Name-WithTrailingSpaces", }, { - input: "Name WithTab", - expected: "Name-WithTab", + input: "Invalid—Characters", + expected: "Invalid-Characters", + }, + { + input: "Intérnätionål", + expected: "Intérnätionål", + }, + { + input: "Intérnätionål®", + expected: "Intérnätionål-", }, } @@ -166,3 +174,36 @@ func TestScmSafeName(t *testing.T) { }) } } + +func TestApplicationId(t *testing.T) { + cases := []struct { + input string + expected string + }{ + { + input: "Name", + expected: "name", + }, + { + input: "Invalid—Characters", + expected: "invalid-characters", + }, + { + input: "Intérnätionål", + expected: "intérnätionål", + }, + { + input: "Intérnätionål®", + expected: "intérnätionål-", + }, + } + + for i, tc := range cases { + t.Run(fmt.Sprintf("TestApplicationId-%d-%s", i, tc.input), func(t *testing.T) { + app := Application{ + Name: tc.input, + } + assert.Equal(t, tc.expected, app.SafeId()) + }) + } +} From 7169923590126144f9a1920585b74b1d584191b8 Mon Sep 17 00:00:00 2001 From: Paul Horton Date: Wed, 11 Dec 2024 13:33:39 +0000 Subject: [PATCH 2/2] app public-id cannot contain any spaces however Signed-off-by: Paul Horton --- scm/types.go | 10 +--------- scm/types_test.go | 12 ++++++++++++ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/scm/types.go b/scm/types.go index 75d7268..1371054 100644 --- a/scm/types.go +++ b/scm/types.go @@ -54,15 +54,7 @@ func (a *Application) PrintTree(depth int) { } func (a *Application) SafeId() string { - return strings.ToLower(INVALID_APP_ORG_NAME.ReplaceAllString(a.Name, "-")) - - // strings.Map(func(r rune) rune { - // if strings.Contains(BANNED_CHARS_ID, string(r)) { - // return '-' - // } else { - // return r - // } - // }, a.Name)) + return strings.ToLower(strings.ReplaceAll(safeName(a.Name), " ", "-")) } func (a *Application) SafeName() string { diff --git a/scm/types_test.go b/scm/types_test.go index dc55f6a..47c1184 100644 --- a/scm/types_test.go +++ b/scm/types_test.go @@ -196,6 +196,18 @@ func TestApplicationId(t *testing.T) { input: "Intérnätionål®", expected: "intérnätionål-", }, + { + input: " Leading Space", + expected: "leading-space", + }, + { + input: "Trailing Space ", + expected: "trailing-space", + }, + { + input: "Double Space", + expected: "double-space", + }, } for i, tc := range cases {