Skip to content
Open
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
4 changes: 4 additions & 0 deletions github/resource_github_repository_autolink_reference.go
Copy link
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.

Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ 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.StringMatch(
regexp.MustCompile(`^[a-zA-Z0-9.=+:/#_-]*[a-zA-Z.=+:/#_-]$`),
"must only contain letters, numbers, or .-_+=:/# and must not end with a number",
)),
},
"target_url_template": {
Type: schema.TypeString,
Expand Down