From fbfc01936bd49f1bd6b2d93c20f7bc05a004194f Mon Sep 17 00:00:00 2001 From: Doug Ayers Date: Sun, 2 Oct 2022 14:10:45 -0500 Subject: [PATCH 1/7] Add `is_alphanumeric` for repo autolinks --- ...rce_github_repository_autolink_reference.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/github/resource_github_repository_autolink_reference.go b/github/resource_github_repository_autolink_reference.go index 9b55ec47d3..3db3c3df5a 100644 --- a/github/resource_github_repository_autolink_reference.go +++ b/github/resource_github_repository_autolink_reference.go @@ -41,15 +41,22 @@ func resourceGithubRepositoryAutolinkReference() *schema.Resource { "key_prefix": { Type: schema.TypeString, Required: true, - Description: "This prefix appended by a number will generate a link any time it is found in an issue, pull request, or commit", 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", }, "target_url_template": { Type: schema.TypeString, Required: true, + ForceNew: true, Description: "The template of the target URL used for the links; must be a valid URL and contain `` for the reference number", ValidateFunc: validation.StringMatch(regexp.MustCompile(`^http[s]?:\/\/[a-z0-9-.]*\/.*?.*?$`), "must be a valid URL and contain token"), - ForceNew: true, + }, + "is_alphanumeric": { + Type: schema.TypeBool, + Optional: true, + ForceNew: true, + Default: true, + Description: "Whether this autolink reference matches alphanumeric characters. If false, this autolink reference only matches numeric characters.", }, "etag": { Type: schema.TypeString, @@ -66,11 +73,13 @@ func resourceGithubRepositoryAutolinkReferenceCreate(d *schema.ResourceData, met repoName := d.Get("repository").(string) keyPrefix := d.Get("key_prefix").(string) targetURLTemplate := d.Get("target_url_template").(string) + isAlphanumeric := d.Get("is_alphanumeric").(bool) ctx := context.Background() opts := &github.AutolinkOptions{ - KeyPrefix: &keyPrefix, - URLTemplate: &targetURLTemplate, + KeyPrefix: &keyPrefix, + URLTemplate: &targetURLTemplate, + IsAlphanumeric: &isAlphanumeric, } autolinkRef, _, err := client.Repositories.AddAutolink(ctx, owner, repoName, opts) @@ -106,6 +115,7 @@ func resourceGithubRepositoryAutolinkReferenceRead(d *schema.ResourceData, meta d.Set("repository", repoName) d.Set("key_prefix", autolinkRef.KeyPrefix) d.Set("target_url_template", autolinkRef.URLTemplate) + d.Set("is_alphanumeric", autolinkRef.IsAlphanumeric) return nil } From ba191886c3eb1df47d25438204cb6487afd400b1 Mon Sep 17 00:00:00 2001 From: Doug Ayers Date: Sun, 2 Oct 2022 14:11:08 -0500 Subject: [PATCH 2/7] Test `is_alphanumeric` for repo autolinks --- ...thub_repository_autolink_reference_test.go | 155 +++++++++++++++--- 1 file changed, 130 insertions(+), 25 deletions(-) diff --git a/github/resource_github_repository_autolink_reference_test.go b/github/resource_github_repository_autolink_reference_test.go index 4d7cadd22a..b220dae965 100644 --- a/github/resource_github_repository_autolink_reference_test.go +++ b/github/resource_github_repository_autolink_reference_test.go @@ -15,25 +15,65 @@ func TestAccGithubRepositoryAutolinkReference(t *testing.T) { t.Run("creates repository autolink reference without error", func(t *testing.T) { config := fmt.Sprintf(` - resource "github_repository" "oof" { - name = "oof-%s" - description = "Test autolink creation" + resource "github_repository" "test" { + name = "test-%s" + description = "Test autolink creation" } - resource "github_repository_autolink_reference" "autolink" { - repository = github_repository.oof.name + resource "github_repository_autolink_reference" "autolink_default" { + repository = github_repository.test.name - key_prefix = "OOF-" - target_url_template = "https://awesome.com/find/OOF-" + key_prefix = "TEST1-" + target_url_template = "https://example.com/TEST-" + } + + resource "github_repository_autolink_reference" "autolink_alphanumeric" { + repository = github_repository.test.name + + key_prefix = "TEST2-" + target_url_template = "https://example.com/TEST-" + is_alphanumeric = true + } + + resource "github_repository_autolink_reference" "autolink_numeric" { + repository = github_repository.test.name + + key_prefix = "TEST3-" + target_url_template = "https://example.com/TEST-" + is_alphanumeric = false } `, randomID) check := resource.ComposeTestCheckFunc( + // autolink_default + resource.TestCheckResourceAttr( + "github_repository_autolink_reference.autolink_default", "key_prefix", "TEST1-", + ), + resource.TestCheckResourceAttr( + "github_repository_autolink_reference.autolink_default", "target_url_template", "https://example.com/TEST-", + ), + resource.TestCheckResourceAttr( + "github_repository_autolink_reference.autolink_default", "is_alphanumeric", "true", + ), + // autolink_alphanumeric + resource.TestCheckResourceAttr( + "github_repository_autolink_reference.autolink_alphanumeric", "key_prefix", "TEST2-", + ), resource.TestCheckResourceAttr( - "github_repository_autolink_reference.autolink", "key_prefix", "OOF-", + "github_repository_autolink_reference.autolink_alphanumeric", "target_url_template", "https://example.com/TEST-", ), resource.TestCheckResourceAttr( - "github_repository_autolink_reference.autolink", "target_url_template", "https://awesome.com/find/OOF-", + "github_repository_autolink_reference.autolink_alphanumeric", "is_alphanumeric", "true", + ), + // autolink_numeric + resource.TestCheckResourceAttr( + "github_repository_autolink_reference.autolink_numeric", "key_prefix", "TEST3-", + ), + resource.TestCheckResourceAttr( + "github_repository_autolink_reference.autolink_numeric", "target_url_template", "https://example.com/TEST-", + ), + resource.TestCheckResourceAttr( + "github_repository_autolink_reference.autolink_numeric", "is_alphanumeric", "false", ), ) @@ -67,19 +107,68 @@ func TestAccGithubRepositoryAutolinkReference(t *testing.T) { t.Run("imports repository autolink reference without error", func(t *testing.T) { config := fmt.Sprintf(` - resource "github_repository" "oof" { - name = "oof-%s" - description = "Test autolink creation" + resource "github_repository" "test" { + name = "test-%s" + description = "Test autolink creation" + } + + resource "github_repository_autolink_reference" "autolink_default" { + repository = github_repository.test.name + + key_prefix = "TEST1-" + target_url_template = "https://example.com/TEST-" } - resource "github_repository_autolink_reference" "autolink" { - repository = github_repository.oof.name + resource "github_repository_autolink_reference" "autolink_alphanumeric" { + repository = github_repository.test.name - key_prefix = "OOF-" - target_url_template = "https://awesome.com/find/OOF-" + key_prefix = "TEST2-" + target_url_template = "https://example.com/TEST-" + is_alphanumeric = true + } + + resource "github_repository_autolink_reference" "autolink_numeric" { + repository = github_repository.test.name + + key_prefix = "TEST3-" + target_url_template = "https://example.com/TEST-" + is_alphanumeric = false } `, randomID) + check := resource.ComposeTestCheckFunc( + // autolink_default + resource.TestCheckResourceAttr( + "github_repository_autolink_reference.autolink_default", "key_prefix", "TEST1-", + ), + resource.TestCheckResourceAttr( + "github_repository_autolink_reference.autolink_default", "target_url_template", "https://example.com/TEST-", + ), + resource.TestCheckResourceAttr( + "github_repository_autolink_reference.autolink_default", "is_alphanumeric", "true", + ), + // autolink_alphanumeric + resource.TestCheckResourceAttr( + "github_repository_autolink_reference.autolink_alphanumeric", "key_prefix", "TEST2-", + ), + resource.TestCheckResourceAttr( + "github_repository_autolink_reference.autolink_alphanumeric", "target_url_template", "https://example.com/TEST-", + ), + resource.TestCheckResourceAttr( + "github_repository_autolink_reference.autolink_alphanumeric", "is_alphanumeric", "true", + ), + // autolink_numeric + resource.TestCheckResourceAttr( + "github_repository_autolink_reference.autolink_numeric", "key_prefix", "TEST3-", + ), + resource.TestCheckResourceAttr( + "github_repository_autolink_reference.autolink_numeric", "target_url_template", "https://example.com/TEST-", + ), + resource.TestCheckResourceAttr( + "github_repository_autolink_reference.autolink_numeric", "is_alphanumeric", "false", + ), + ) + testCase := func(t *testing.T, mode string) { resource.Test(t, resource.TestCase{ PreCheck: func() { skipUnlessMode(t, mode) }, @@ -87,12 +176,28 @@ func TestAccGithubRepositoryAutolinkReference(t *testing.T) { Steps: []resource.TestStep{ { Config: config, + Check: check, + }, + // autolink_default + { + ResourceName: "github_repository_autolink_reference.autolink_default", + ImportState: true, + ImportStateVerify: true, + ImportStateIdPrefix: fmt.Sprintf("test-%s/", randomID), + }, + // autolink_alphanumeric + { + ResourceName: "github_repository_autolink_reference.autolink_alphanumeric", + ImportState: true, + ImportStateVerify: true, + ImportStateIdPrefix: fmt.Sprintf("test-%s/", randomID), }, + // autolink_numeric { - ResourceName: "github_repository_autolink_reference.autolink", + ResourceName: "github_repository_autolink_reference.autolink_numeric", ImportState: true, ImportStateVerify: true, - ImportStateIdPrefix: fmt.Sprintf("oof-%s/", randomID), + ImportStateIdPrefix: fmt.Sprintf("test-%s/", randomID), }, }, }) @@ -115,16 +220,16 @@ func TestAccGithubRepositoryAutolinkReference(t *testing.T) { t.Run("deletes repository autolink reference without error", func(t *testing.T) { config := fmt.Sprintf(` - resource "github_repository" "oof" { - name = "oof-%s" - description = "Test autolink creation" + resource "github_repository" "test" { + name = "test-%s" + description = "Test autolink creation" } - resource "github_repository_autolink_reference" "autolink" { - repository = github_repository.oof.name + resource "github_repository_autolink_reference" "autolink_default" { + repository = github_repository.test.name - key_prefix = "OOF-" - target_url_template = "https://awesome.com/find/OOF-" + key_prefix = "TEST1-" + target_url_template = "https://example.com/TEST-" } `, randomID) From a081869d9eb25734468f9196c5fba5d423a5fed8 Mon Sep 17 00:00:00 2001 From: Doug Ayers Date: Sun, 2 Oct 2022 15:12:52 -0500 Subject: [PATCH 3/7] Use parseTwoPartID util func for consistent import ids --- .../resource_github_repository_autolink_reference.go | 12 +++++------- ...urce_github_repository_autolink_reference_test.go | 6 +++--- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/github/resource_github_repository_autolink_reference.go b/github/resource_github_repository_autolink_reference.go index 3db3c3df5a..d7f896d0a8 100644 --- a/github/resource_github_repository_autolink_reference.go +++ b/github/resource_github_repository_autolink_reference.go @@ -2,10 +2,8 @@ package github import ( "context" - "fmt" "regexp" "strconv" - "strings" "github.com/google/go-github/v47/github" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" @@ -20,12 +18,12 @@ func resourceGithubRepositoryAutolinkReference() *schema.Resource { Importer: &schema.ResourceImporter{ State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { - parts := strings.Split(d.Id(), "/") - if len(parts) != 2 { - return nil, fmt.Errorf("Invalid ID specified. Supplied ID must be written as /") + repoName, autolinkRefID, err := parseTwoPartID(d.Id(), "repository", "autolink_reference_id") + if err != nil { + return nil, err } - d.Set("repository", parts[0]) - d.SetId(parts[1]) + d.Set("repository", repoName) + d.SetId(autolinkRefID) return []*schema.ResourceData{d}, nil }, }, diff --git a/github/resource_github_repository_autolink_reference_test.go b/github/resource_github_repository_autolink_reference_test.go index b220dae965..2e84d91f95 100644 --- a/github/resource_github_repository_autolink_reference_test.go +++ b/github/resource_github_repository_autolink_reference_test.go @@ -183,21 +183,21 @@ func TestAccGithubRepositoryAutolinkReference(t *testing.T) { ResourceName: "github_repository_autolink_reference.autolink_default", ImportState: true, ImportStateVerify: true, - ImportStateIdPrefix: fmt.Sprintf("test-%s/", randomID), + ImportStateIdPrefix: fmt.Sprintf("test-%s:", randomID), }, // autolink_alphanumeric { ResourceName: "github_repository_autolink_reference.autolink_alphanumeric", ImportState: true, ImportStateVerify: true, - ImportStateIdPrefix: fmt.Sprintf("test-%s/", randomID), + ImportStateIdPrefix: fmt.Sprintf("test-%s:", randomID), }, // autolink_numeric { ResourceName: "github_repository_autolink_reference.autolink_numeric", ImportState: true, ImportStateVerify: true, - ImportStateIdPrefix: fmt.Sprintf("test-%s/", randomID), + ImportStateIdPrefix: fmt.Sprintf("test-%s:", randomID), }, }, }) From ee3e7efca5bd2575e27cd0c542d8d9b020bcf78e Mon Sep 17 00:00:00 2001 From: Doug Ayers Date: Sun, 2 Oct 2022 15:13:25 -0500 Subject: [PATCH 4/7] Update docs about `is_alphanumeric` and import format --- .../r/repository_autolink_reference.html.markdown | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/website/docs/r/repository_autolink_reference.html.markdown b/website/docs/r/repository_autolink_reference.html.markdown index 8f9ed6870a..a647653b2e 100644 --- a/website/docs/r/repository_autolink_reference.html.markdown +++ b/website/docs/r/repository_autolink_reference.html.markdown @@ -13,18 +13,18 @@ This resource allows you to create and manage an autolink reference for a single ```hcl resource "github_repository" "repo" { - name = "oof" + name = "my-repo" description = "GitHub repo managed by Terraform" private = false } -resource "github_repository_autolink_reference" "auto" { +resource "github_repository_autolink_reference" "autolink" { repository = github_repository.repo.name key_prefix = "TICKET-" - target_url_template = "https://hello.there/TICKET?query=" + target_url_template = "https://example.com/TICKET?query=" } ``` @@ -38,6 +38,8 @@ The following arguments are supported: * `target_url_template` - (Required) The template of the target URL used for the links; must be a valid URL and contain `` for the reference number +* `is_alphanumeric` - (Optional) Whether this autolink reference matches alphanumeric characters. If false, this autolink reference only matches numeric characters. Default is true. + ## Attributes Reference The following additional attributes are exported: @@ -46,8 +48,10 @@ The following additional attributes are exported: ## Import -Autolink references can be imported using the `name` of the repository, combined with the `id` of the autolink reference and a `/` character for separating components, e.g. +Autolink references can be imported using the `name` of the repository, combined with the `id` of the autolink reference and a `:` character for separating components, e.g. ```sh -terraform import github_repository_autolink_reference.auto oof/123 +terraform import github_repository_autolink_reference.auto my-repo:123 ``` + +See the GitHub documentation for how to [list all autolinks of a repository](https://docs.github.com/en/rest/repos/autolinks#list-all-autolinks-of-a-repository) to learn the autolink ids to use with the import command. \ No newline at end of file From 768aacecdc376f77ce591044a5639a04e937ac1f Mon Sep 17 00:00:00 2001 From: Doug Ayers Date: Sun, 30 Oct 2022 20:52:44 -0500 Subject: [PATCH 5/7] revert import state parsing to avoid breaking change --- .../resource_github_repository_autolink_reference.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/github/resource_github_repository_autolink_reference.go b/github/resource_github_repository_autolink_reference.go index 8826677384..20efb3ffb4 100644 --- a/github/resource_github_repository_autolink_reference.go +++ b/github/resource_github_repository_autolink_reference.go @@ -2,8 +2,10 @@ package github import ( "context" + "fmt" "regexp" "strconv" + "strings" "github.com/google/go-github/v48/github" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" @@ -18,12 +20,12 @@ func resourceGithubRepositoryAutolinkReference() *schema.Resource { Importer: &schema.ResourceImporter{ State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { - repoName, autolinkRefID, err := parseTwoPartID(d.Id(), "repository", "autolink_reference_id") - if err != nil { - return nil, err + parts := strings.Split(d.Id(), "/") + if len(parts) != 2 { + return nil, fmt.Errorf("invalid ID specified: supplied ID must be written as /") } - d.Set("repository", repoName) - d.SetId(autolinkRefID) + d.Set("repository", parts[0]) + d.SetId(parts[1]) return []*schema.ResourceData{d}, nil }, }, From 7bc1b85a46dbec537ffb0b5c8cac613c3452aded Mon Sep 17 00:00:00 2001 From: Doug Ayers Date: Sun, 30 Oct 2022 20:59:56 -0500 Subject: [PATCH 6/7] revert import state parsing to avoid breaking change --- .../resource_github_repository_autolink_reference_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/github/resource_github_repository_autolink_reference_test.go b/github/resource_github_repository_autolink_reference_test.go index 2e84d91f95..b220dae965 100644 --- a/github/resource_github_repository_autolink_reference_test.go +++ b/github/resource_github_repository_autolink_reference_test.go @@ -183,21 +183,21 @@ func TestAccGithubRepositoryAutolinkReference(t *testing.T) { ResourceName: "github_repository_autolink_reference.autolink_default", ImportState: true, ImportStateVerify: true, - ImportStateIdPrefix: fmt.Sprintf("test-%s:", randomID), + ImportStateIdPrefix: fmt.Sprintf("test-%s/", randomID), }, // autolink_alphanumeric { ResourceName: "github_repository_autolink_reference.autolink_alphanumeric", ImportState: true, ImportStateVerify: true, - ImportStateIdPrefix: fmt.Sprintf("test-%s:", randomID), + ImportStateIdPrefix: fmt.Sprintf("test-%s/", randomID), }, // autolink_numeric { ResourceName: "github_repository_autolink_reference.autolink_numeric", ImportState: true, ImportStateVerify: true, - ImportStateIdPrefix: fmt.Sprintf("test-%s:", randomID), + ImportStateIdPrefix: fmt.Sprintf("test-%s/", randomID), }, }, }) From 41d1c3a56ee471968b50b9de71b6023d17413184 Mon Sep 17 00:00:00 2001 From: Doug Ayers Date: Sun, 30 Oct 2022 21:01:26 -0500 Subject: [PATCH 7/7] revert import state parsing to avoid breaking change --- website/docs/r/repository_autolink_reference.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/website/docs/r/repository_autolink_reference.html.markdown b/website/docs/r/repository_autolink_reference.html.markdown index a647653b2e..c0406dae4d 100644 --- a/website/docs/r/repository_autolink_reference.html.markdown +++ b/website/docs/r/repository_autolink_reference.html.markdown @@ -48,10 +48,10 @@ The following additional attributes are exported: ## Import -Autolink references can be imported using the `name` of the repository, combined with the `id` of the autolink reference and a `:` character for separating components, e.g. +Autolink references can be imported using the `name` of the repository, combined with the `id` of the autolink reference and a `/` character for separating components, e.g. ```sh -terraform import github_repository_autolink_reference.auto my-repo:123 +terraform import github_repository_autolink_reference.auto my-repo/123 ``` See the GitHub documentation for how to [list all autolinks of a repository](https://docs.github.com/en/rest/repos/autolinks#list-all-autolinks-of-a-repository) to learn the autolink ids to use with the import command. \ No newline at end of file