Skip to content

Commit

Permalink
Enforce valid chars in repo name (#1806)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
btlogy and kfcampbell committed Jul 24, 2023
1 parent 64f123a commit b490535
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion github/resource_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func resourceGithubRepository() *schema.Resource {
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 100),
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"),
Description: "The name of the repository.",
},
"description": {
Expand Down
30 changes: 30 additions & 0 deletions github/resource_github_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1455,3 +1455,33 @@ func testCheckResourceAttrContains(resourceName, attributeName, substring string
return nil
}
}

func TestGithubRepositoryNameFailsValidationWhenOverMaxCharacters(t *testing.T) {
resource := resourceGithubRepository()
schema := resource.Schema["name"]

_, err := schema.ValidateFunc(strings.Repeat("a", 101), "name")
if len(err) != 1 {
t.Error(fmt.Errorf("unexpected number of name validation failures; expected=1; actual=%d", len(err)))
}
expectedFailure := "invalid value for name (must include only alphanumeric characters, underscores or hyphens and consist of 100 characters or less)"
actualFailure := err[0].Error()
if expectedFailure != actualFailure {
t.Error(fmt.Errorf("unexpected name validation failure; expected=%s; action=%s", expectedFailure, actualFailure))
}
}

func TestGithubRepositoryNameFailsValidationWithSpace(t *testing.T) {
resource := resourceGithubRepository()
schema := resource.Schema["name"]

_, err := schema.ValidateFunc("test space", "name")
if len(err) != 1 {
t.Error(fmt.Errorf("unexpected number of name validation failures; expected=1; actual=%d", len(err)))
}
expectedFailure := "invalid value for name (must include only alphanumeric characters, underscores or hyphens and consist of 100 characters or less)"
actualFailure := err[0].Error()
if expectedFailure != actualFailure {
t.Error(fmt.Errorf("unexpected name validation failure; expected=%s; action=%s", expectedFailure, actualFailure))
}
}

0 comments on commit b490535

Please sign in to comment.