Skip to content

Commit

Permalink
Merge pull request #14 from sonatype-nexus-community/fix/org-name-cha…
Browse files Browse the repository at this point in the history
…racter-restrictions

fix: better handling of accepted characters for App and Org Names and App Public IDs
  • Loading branch information
madpah authored Dec 11, 2024
2 parents e89ff43 + 7169923 commit 24e99a2
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 18 deletions.
24 changes: 8 additions & 16 deletions scm/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -53,13 +54,7 @@ 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(strings.ReplaceAll(safeName(a.Name), " ", "-"))
}

func (a *Application) SafeName() string {
Expand Down Expand Up @@ -127,13 +122,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", "-"),
"-",
),
"-",
)
}
57 changes: 55 additions & 2 deletions scm/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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-",
},
}

Expand All @@ -166,3 +174,48 @@ 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-",
},
{
input: " Leading Space",
expected: "leading-space",
},
{
input: "Trailing Space ",
expected: "trailing-space",
},
{
input: "Double Space",
expected: "double-space",
},
}

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())
})
}
}

0 comments on commit 24e99a2

Please sign in to comment.