Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 64d2a75

Browse files
committed
feat(coderd): increase group name limit to 255
1 parent 886dcbe commit 64d2a75

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

codersdk/name.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package codersdk
22

33
import (
4+
"fmt"
45
"regexp"
56
"strings"
67

@@ -98,9 +99,12 @@ func UserRealNameValid(str string) error {
9899

99100
// GroupNameValid returns whether the input string is a valid group name.
100101
func GroupNameValid(str string) error {
101-
// 36 is to support using UUIDs as the group name.
102-
if len(str) > 36 {
103-
return xerrors.New("must be <= 36 characters")
102+
// We want to support longer names for groups to allow users to sync their
103+
// group names with their identity providers without manual mapping. Related
104+
// to: https://github.com/coder/coder/issues/15184
105+
limit := 255
106+
if len(str) > limit {
107+
return xerrors.New(fmt.Sprintf("must be <= %d characters", limit))
104108
}
105109
// Avoid conflicts with routes like /groups/new and /groups/create.
106110
if str == "new" || str == "create" {

codersdk/name_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package codersdk_test
33
import (
44
"strings"
55
"testing"
6+
"time"
67

78
"github.com/stretchr/testify/assert"
89
"github.com/stretchr/testify/require"
10+
"golang.org/x/exp/rand"
911

1012
"github.com/coder/coder/v2/codersdk"
1113
"github.com/coder/coder/v2/testutil"
@@ -254,3 +256,40 @@ func TestUserRealNameValid(t *testing.T) {
254256
})
255257
}
256258
}
259+
260+
func TestGroupNameValid(t *testing.T) {
261+
t.Parallel()
262+
263+
testCases := []struct {
264+
Name string
265+
Valid bool
266+
}{
267+
{"", false},
268+
{"my-group", true},
269+
{"create", false},
270+
{"new", false},
271+
{"Lord Voldemort Team", false},
272+
{randomString(255), true},
273+
{randomString(256), false},
274+
}
275+
for _, testCase := range testCases {
276+
testCase := testCase
277+
t.Run(testCase.Name, func(t *testing.T) {
278+
t.Parallel()
279+
err := codersdk.GroupNameValid(testCase.Name)
280+
assert.Equal(t, testCase.Valid, err == nil)
281+
})
282+
}
283+
}
284+
285+
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
286+
287+
// RandomString generates a random string of a given length.
288+
func randomString(length int) string {
289+
seededRand := rand.New(rand.NewSource(uint64(time.Now().UnixNano())))
290+
result := make([]byte, length)
291+
for i := range result {
292+
result[i] = charset[seededRand.Intn(len(charset))]
293+
}
294+
return string(result)
295+
}

0 commit comments

Comments
 (0)