Skip to content

Commit

Permalink
fix: handle Organisation names containing a double space
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Horton <[email protected]>
  • Loading branch information
madpah committed Dec 10, 2024
1 parent d85f135 commit d74461b
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ jobs:
run: |
go build -ldflags='-s -w'
- name: Run Tests
run: |
go test -v ./...
sonatype:
name: Sonatype Lifecycle
runs-on: ubuntu-latest
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ toolchain go1.22.8
require github.com/sirupsen/logrus v1.9.3

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/uuid v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.10.0 // indirect
golang.org/x/term v0.27.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ github.com/sonatype-nexus-community/nexus-iq-api-client-go v0.184.3/go.mod h1:0l
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
Expand All @@ -24,3 +26,5 @@ golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
17 changes: 10 additions & 7 deletions scm/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,16 @@ func (o *Organization) PrintTree(depth int) {
}

func (o *Organization) SafeName() string {
return strings.Map(func(r rune) rune {
if strings.Contains(BANNED_CHARS_NAME, string(r)) {
return '-'
} else {
return r
}
}, o.Name)
return strings.ReplaceAll(
strings.Map(func(r rune) rune {
if strings.Contains(BANNED_CHARS_NAME, string(r)) {
return '-'
} else {
return r
}
}, o.Name),
" ", "--",
)
}

type OrgContents struct {
Expand Down
56 changes: 56 additions & 0 deletions scm/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Copyright (c) 2019-present Sonatype, Inc.
*
* 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 scm

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestScmOrganisationSafeName(t *testing.T) {

cases := []struct {
input string
expected string
}{
{
input: "Name",
expected: "Name",
},
{
input: "Name<",
expected: "Name-",
},
{
input: "Name[something]",
expected: "Name-something-",
},
{
input: "Name WithDoubleSpace",
expected: "Name--WithDoubleSpace",
},
}

for i, tc := range cases {
t.Run(fmt.Sprintf("TestScmOrganisationSafeName-%d-%s", i, tc.input), func(t *testing.T) {
o := Organization{Name: tc.input}
assert.Equal(t, tc.expected, o.SafeName())
})
}
}

0 comments on commit d74461b

Please sign in to comment.