From fe462e55c438564ff68a32666e21fe722968170f Mon Sep 17 00:00:00 2001 From: KenSpur Date: Fri, 23 Jun 2023 14:00:12 +0200 Subject: [PATCH 1/2] set full_name new computed on name change & add tests --- github/resource_github_repository.go | 8 +++++++ github/resource_github_repository_test.go | 29 +++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/github/resource_github_repository.go b/github/resource_github_repository.go index d14ff60250..cc8086fea9 100644 --- a/github/resource_github_repository.go +++ b/github/resource_github_repository.go @@ -394,6 +394,7 @@ func resourceGithubRepository() *schema.Resource { Description: " Set to 'true' to always suggest updating pull request branches.", }, }, + CustomizeDiff: customDiffFunction, } } @@ -942,3 +943,10 @@ func flattenSecurityAndAnalysis(securityAndAnalysis *github.SecurityAndAnalysis) return []interface{}{securityAndAnalysisMap} } + +func customDiffFunction(diff *schema.ResourceDiff, v interface{}) error { + if diff.HasChange("name") { + diff.SetNewComputed("full_name") + } + return nil +} diff --git a/github/resource_github_repository_test.go b/github/resource_github_repository_test.go index 157c39b9d1..f7561b996f 100644 --- a/github/resource_github_repository_test.go +++ b/github/resource_github_repository_test.go @@ -9,6 +9,7 @@ import ( "testing" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/terraform" "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" @@ -104,12 +105,20 @@ func TestAccGithubRepositories(t *testing.T) { "github_repository.test", "name", oldName, ), + resource.ComposeTestCheckFunc( + testCheckResourceAttrContains("github_repository.test", "full_name", + oldName), + ), ), "after": resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr( "github_repository.test", "name", newName, ), + resource.ComposeTestCheckFunc( + testCheckResourceAttrContains("github_repository.test", "full_name", + newName), + ), ), } @@ -1407,3 +1416,23 @@ func reconfigureVisibility(config, visibility string) string { ) return newConfig } + +func testCheckResourceAttrContains(resourceName, attributeName, substring string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[resourceName] + if !ok { + return fmt.Errorf("Resource not found: %s", resourceName) + } + + value, ok := rs.Primary.Attributes[attributeName] + if !ok { + return fmt.Errorf("Attribute not found: %s", attributeName) + } + + if !strings.Contains(value, substring) { + return fmt.Errorf("Attribute '%s' does not contain '%s'", value, substring) + } + + return nil + } +} From b98eb008290071339f8fff1c71e544e36b63f1e8 Mon Sep 17 00:00:00 2001 From: Keegan Campbell Date: Tue, 27 Jun 2023 12:53:42 -0700 Subject: [PATCH 2/2] Run go fmt ./... --- github/resource_github_repository.go | 2 +- github/resource_github_repository_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/github/resource_github_repository.go b/github/resource_github_repository.go index fb6608d72e..093e2aab04 100644 --- a/github/resource_github_repository.go +++ b/github/resource_github_repository.go @@ -976,4 +976,4 @@ func customDiffFunction(diff *schema.ResourceDiff, v interface{}) error { diff.SetNewComputed("full_name") } return nil -} \ No newline at end of file +} diff --git a/github/resource_github_repository_test.go b/github/resource_github_repository_test.go index 06a2d9ab68..f3965a76d1 100644 --- a/github/resource_github_repository_test.go +++ b/github/resource_github_repository_test.go @@ -1454,4 +1454,4 @@ func testCheckResourceAttrContains(resourceName, attributeName, substring string return nil } -} \ No newline at end of file +}