From bb953a03eb89f54bfca688cce0f3a39f91a41aea Mon Sep 17 00:00:00 2001 From: JonasHiltl Date: Sun, 20 Oct 2024 16:08:11 +0200 Subject: [PATCH] fix: use random words for subdomain --- go.mod | 3 +++ go.sum | 6 ++++++ internal/store/domain.go | 11 ++++++++--- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index af2f383..43e05ca 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/a-h/templ v0.2.771 github.com/bradleyfalzon/ghinstallation/v2 v2.10.0 github.com/google/go-github/v62 v62.0.0 + github.com/gosimple/slug v1.14.0 github.com/guregu/null/v5 v5.0.0 github.com/jonashiltl/openchangelog/apitypes v0.0.0-20241013200840-98b510d4c198 github.com/mattn/go-sqlite3 v1.14.16 @@ -13,6 +14,7 @@ require ( github.com/peterbourgon/diskv v2.0.1+incompatible github.com/quail-ink/goldmark-enclave v0.0.8 github.com/rs/xid v1.5.0 + github.com/sio/coolname v0.1.0 github.com/sourcegraph/s3cache v0.0.0-20141202193749-4150cc6b0465 github.com/spf13/viper v1.18.2 github.com/yuin/goldmark v1.7.1 @@ -29,6 +31,7 @@ require ( github.com/google/btree v1.0.0 // indirect github.com/google/go-github/v60 v60.0.0 // indirect github.com/google/go-querystring v1.1.0 // indirect + github.com/gosimple/unidecode v1.0.1 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/kr/http v0.0.0-20150505212737-77bd98b60462 // indirect github.com/magiconair/properties v1.8.7 // indirect diff --git a/go.sum b/go.sum index 034b30f..9ff08c1 100644 --- a/go.sum +++ b/go.sum @@ -30,6 +30,10 @@ github.com/google/go-github/v62 v62.0.0 h1:/6mGCaRywZz9MuHyw9gD1CwsbmBX8GWsbFkwM github.com/google/go-github/v62 v62.0.0/go.mod h1:EMxeUqGJq2xRu9DYBMwel/mr7kZrzUOfQmmpYrZn2a4= github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= +github.com/gosimple/slug v1.14.0 h1:RtTL/71mJNDfpUbCOmnf/XFkzKRtD6wL6Uy+3akm4Es= +github.com/gosimple/slug v1.14.0/go.mod h1:UiRaFH+GEilHstLUmcBgWcI42viBN7mAb818JrYOeFQ= +github.com/gosimple/unidecode v1.0.1 h1:hZzFTMMqSswvf0LBJZCZgThIZrpDHFXux9KeGmn6T/o= +github.com/gosimple/unidecode v1.0.1/go.mod h1:CP0Cr1Y1kogOtx0bJblKzsVWrqYaqfNOnHzpgWw4Awc= github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 h1:+ngKgrYPPJrOjhax5N+uePQ0Fh1Z7PheYoUI/0nzkPA= github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA= github.com/guregu/null/v5 v5.0.0 h1:PRxjqyOekS11W+w/7Vfz6jgJE/BCwELWtgvOJzddimw= @@ -76,6 +80,8 @@ github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6ke github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= +github.com/sio/coolname v0.1.0 h1:Hha2+NQ4dRb9pmWqGlPgu0PktZYuWPaoCJbGcbYSAsA= +github.com/sio/coolname v0.1.0/go.mod h1:3Z0yllmTmmNnicHY0vr2mUpIw31Ts755TO/6rocFXeo= github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= github.com/sourcegraph/s3cache v0.0.0-20141202193749-4150cc6b0465 h1:YwSzrOdV58EJMej5zdLFYUuJp6zHaE4KshMxiSaqbXY= diff --git a/internal/store/domain.go b/internal/store/domain.go index 76c6aaa..c351c8a 100644 --- a/internal/store/domain.go +++ b/internal/store/domain.go @@ -7,8 +7,10 @@ import ( "regexp" "strings" + "github.com/gosimple/slug" "github.com/jonashiltl/openchangelog/apitypes" "github.com/jonashiltl/openchangelog/internal/errs" + "github.com/sio/coolname" "golang.org/x/exp/rand" ) @@ -61,10 +63,13 @@ func (s Subdomain) NullString() apitypes.NullString { } func NewSubdomain(workspaceName string) Subdomain { - wsName := strings.ReplaceAll(strings.ToLower(workspaceName), " ", "-") - rnd := rand.Intn(100000) + suffix, err := coolname.SlugN(2) + if err != nil { + suffix = fmt.Sprint(rand.Intn(100000)) + } - return Subdomain(fmt.Sprintf("%s-%d", wsName, rnd)) + subdomain := slug.Make(fmt.Sprintf("%s %s", workspaceName, suffix)) + return Subdomain(subdomain) } var subdomainRegex = regexp.MustCompile("^[a-z0-9-]*$")