Skip to content

Commit b490535

Browse files
btlogykfcampbell
andauthored
Enforce valid chars in repo name (#1806)
* Enforce valid chars in repo name Signed-off-by: Benoit Donneaux <ben@tergology.com> * Add dash to validate repo name Signed-off-by: Benoit Donneaux <ben@tergology.com> * Better name validation message Signed-off-by: Benoit Donneaux <ben@tergology.com> * Test repo name max length Signed-off-by: Benoit Donneaux <ben@tergology.com> * Test space in repo name Signed-off-by: Benoit Donneaux <ben@tergology.com> --------- Signed-off-by: Benoit Donneaux <ben@tergology.com> Co-authored-by: Keegan Campbell <me@kfcampbell.com>
1 parent 64f123a commit b490535

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

github/resource_github_repository.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func resourceGithubRepository() *schema.Resource {
3434
"name": {
3535
Type: schema.TypeString,
3636
Required: true,
37-
ValidateFunc: validation.StringLenBetween(1, 100),
37+
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^[-a-zA-Z0-9_.]{1,100}$`), "must include only alphanumeric characters, underscores or hyphens and consist of 100 characters or less"),
3838
Description: "The name of the repository.",
3939
},
4040
"description": {

github/resource_github_repository_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -1455,3 +1455,33 @@ func testCheckResourceAttrContains(resourceName, attributeName, substring string
14551455
return nil
14561456
}
14571457
}
1458+
1459+
func TestGithubRepositoryNameFailsValidationWhenOverMaxCharacters(t *testing.T) {
1460+
resource := resourceGithubRepository()
1461+
schema := resource.Schema["name"]
1462+
1463+
_, err := schema.ValidateFunc(strings.Repeat("a", 101), "name")
1464+
if len(err) != 1 {
1465+
t.Error(fmt.Errorf("unexpected number of name validation failures; expected=1; actual=%d", len(err)))
1466+
}
1467+
expectedFailure := "invalid value for name (must include only alphanumeric characters, underscores or hyphens and consist of 100 characters or less)"
1468+
actualFailure := err[0].Error()
1469+
if expectedFailure != actualFailure {
1470+
t.Error(fmt.Errorf("unexpected name validation failure; expected=%s; action=%s", expectedFailure, actualFailure))
1471+
}
1472+
}
1473+
1474+
func TestGithubRepositoryNameFailsValidationWithSpace(t *testing.T) {
1475+
resource := resourceGithubRepository()
1476+
schema := resource.Schema["name"]
1477+
1478+
_, err := schema.ValidateFunc("test space", "name")
1479+
if len(err) != 1 {
1480+
t.Error(fmt.Errorf("unexpected number of name validation failures; expected=1; actual=%d", len(err)))
1481+
}
1482+
expectedFailure := "invalid value for name (must include only alphanumeric characters, underscores or hyphens and consist of 100 characters or less)"
1483+
actualFailure := err[0].Error()
1484+
if expectedFailure != actualFailure {
1485+
t.Error(fmt.Errorf("unexpected name validation failure; expected=%s; action=%s", expectedFailure, actualFailure))
1486+
}
1487+
}

0 commit comments

Comments
 (0)