Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions github/resource_github_repository_autolink_reference.go
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This validation looks right. I think the missing piece is a regression test that proves invalid prefixes now fail during terraform plan, since that’s the behavior this PR is fixing.

Because this PR only changes this file, I can’t attach a native GitHub suggestion directly to github/resource_github_repository_autolink_reference_test.go, but something along these lines would lock it in:

t.Run("rejects invalid key_prefix at plan time", func(t *testing.T) {
	randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
	repoName := fmt.Sprintf("%srepo-autolink-%s", testResourcePrefix, randomID)
	config := fmt.Sprintf(`
		resource "github_repository" "test" {
			name        = "%s"
			description = "Test autolink validation"
		}

		resource "github_repository_autolink_reference" "invalid" {
			repository = github_repository.test.name

			key_prefix          = "PTFY25"
			target_url_template = "https://example.com/PTFY-<num>"
		}
	`, repoName)

	resource.Test(t, resource.TestCase{
		PreCheck:          func() { skipUnauthenticated(t) },
		ProviderFactories: providerFactories,
		Steps: []resource.TestStep{
			{
				Config:      config,
				ExpectError: regexp.MustCompile(`must only contain letters, numbers, or .* must not end with a number`),
			},
		},
	})
})

That would make sure we don’t regress back to the current plan/apply mismatch later.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Added a resource.UnitTest sub-test that passes key_prefix = "PTFY25" (ends with a digit) and asserts a plan-time error matching must not end with a number. The test uses providerFactories and doesn't require a real GitHub account.

Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ func resourceGithubRepositoryAutolinkReference() *schema.Resource {
Required: true,
ForceNew: true,
Description: "This prefix appended by a number will generate a link any time it is found in an issue, pull request, or commit",
ValidateDiagFunc: validation.ToDiagFunc(validation.All(
validation.StringMatch(
regexp.MustCompile(`^[a-zA-Z0-9.=+:/#_-]+$`),
"must only contain letters, numbers, or the characters .-_+=:/#",
),
validation.StringMatch(
regexp.MustCompile(`\D$`),
"must not end with a number",
),
)),
},
"target_url_template": {
Type: schema.TypeString,
Expand Down
20 changes: 20 additions & 0 deletions github/resource_github_repository_autolink_reference_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,24 @@ func TestAccGithubRepositoryAutolinkReference(t *testing.T) {
},
})
})

t.Run("rejects key_prefix ending with a digit at plan time", func(t *testing.T) {
config := `
resource "github_repository_autolink_reference" "autolink_invalid" {
repository = "some-repo"
key_prefix = "PTFY25"
target_url_template = "https://example.com/<num>"
}
`

resource.UnitTest(t, resource.TestCase{
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: config,
ExpectError: regexp.MustCompile(`must not end with a number`),
},
},
})
})
}