Skip to content

Commit

Permalink
[Bug]: Renaming github_repository doesn't taint full_name attribute (#…
Browse files Browse the repository at this point in the history
…1756)

* set full_name new computed on name change & add tests

* Run go fmt ./...

---------

Co-authored-by: Keegan Campbell <me@kfcampbell.com>
  • Loading branch information
KenSpur and kfcampbell authored Jun 27, 2023
1 parent db1c41c commit 37783e2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
8 changes: 8 additions & 0 deletions github/resource_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ func resourceGithubRepository() *schema.Resource {
Description: " Set to 'true' to always suggest updating pull request branches.",
},
},
CustomizeDiff: customDiffFunction,
}
}

Expand Down Expand Up @@ -969,3 +970,10 @@ func resourceGithubParseFullName(resourceDataLike interface {
}
return parts[0], parts[1], true
}

func customDiffFunction(diff *schema.ResourceDiff, v interface{}) error {
if diff.HasChange("name") {
diff.SetNewComputed("full_name")
}
return nil
}
29 changes: 29 additions & 0 deletions github/resource_github_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -105,12 +106,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),
),
),
}

Expand Down Expand Up @@ -1426,3 +1435,23 @@ func TestResourceGithubParseFullName(t *testing.T) {
_, _, ok = resourceGithubParseFullName(resourceDataLike(map[string]interface{}{"full_name": "malformed"}))
assert.False(t, ok)
}

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
}
}

0 comments on commit 37783e2

Please sign in to comment.