From 1a6f8d7b0780c05e97c805e16e49c0be6d6e743e Mon Sep 17 00:00:00 2001 From: Ryn Daniels Date: Wed, 8 May 2019 15:50:04 +0200 Subject: [PATCH] Schema validation to ensure that repo tags don't have capital letters --- github/resource_github_repository.go | 7 ++++++- github/resource_github_repository_test.go | 13 +++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/github/resource_github_repository.go b/github/resource_github_repository.go index 233396e37a..498484ecdd 100644 --- a/github/resource_github_repository.go +++ b/github/resource_github_repository.go @@ -5,9 +5,11 @@ import ( "fmt" "log" "net/http" + "regexp" "github.com/google/go-github/v25/github" "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/helper/validation" ) func resourceGithubRepository() *schema.Resource { @@ -100,8 +102,11 @@ func resourceGithubRepository() *schema.Resource { }, "topics": { Type: schema.TypeSet, - Elem: &schema.Schema{Type: schema.TypeString}, Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + ValidateFunc: validation.StringMatch(regexp.MustCompile(`^[a-z0-9][a-z0-9-]*$`), "must include only lowercase alphanumeric characters or hyphens and cannot start with a hyphen"), + }, }, "full_name": { diff --git a/github/resource_github_repository_test.go b/github/resource_github_repository_test.go index a03442c970..a7dcf9ded6 100644 --- a/github/resource_github_repository_test.go +++ b/github/resource_github_repository_test.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "os" + "regexp" "sort" "strings" "testing" @@ -277,6 +278,18 @@ func TestAccGithubRepository_templates(t *testing.T) { Providers: testAccProviders, CheckDestroy: testAccCheckGithubRepositoryDestroy, Steps: []resource.TestStep{ + { + Config: testAccGithubRepositoryConfigTopics(randString, `"TOPIC"`), + ExpectError: regexp.MustCompile(`must include only lowercase alphanumeric characters or hyphens and cannot start with a hyphen`), + }, + { + Config: testAccGithubRepositoryConfigTopics(randString, `"-topic"`), + ExpectError: regexp.MustCompile(`must include only lowercase alphanumeric characters or hyphens and cannot start with a hyphen`), + }, + { + Config: testAccGithubRepositoryConfigTopics(randString, `"töpic"`), + ExpectError: regexp.MustCompile(`must include only lowercase alphanumeric characters or hyphens and cannot start with a hyphen`), + }, { Config: testAccGithubRepositoryConfigTemplates(randString), Check: resource.ComposeTestCheckFunc(