Skip to content

Commit

Permalink
fix: Ignore changes to attributes (#2402)
Browse files Browse the repository at this point in the history
When a `github_repository_file` resource was created with a
provider version older than 6.3.0 the new attributes added in
that version causes a new commit to be created when there
should be no changes to the file.
  • Loading branch information
grahamhar authored Sep 27, 2024
1 parent 0d53cb2 commit 5b50181
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 18 deletions.
41 changes: 27 additions & 14 deletions github/resource_github_repository_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,24 +122,27 @@ func resourceGithubRepositoryFile() *schema.Resource {
Default: false,
},
"autocreate_branch": {
Type: schema.TypeBool,
Optional: true,
Description: "Automatically create the branch if it could not be found. Subsequent reads if the branch is deleted will occur from 'autocreate_branch_source_branch'",
Default: false,
Type: schema.TypeBool,
Optional: true,
Description: "Automatically create the branch if it could not be found. Subsequent reads if the branch is deleted will occur from 'autocreate_branch_source_branch'",
Default: false,
DiffSuppressFunc: autoBranchDiffSuppressFunc,
},
"autocreate_branch_source_branch": {
Type: schema.TypeString,
Default: "main",
Optional: true,
Description: "The branch name to start from, if 'autocreate_branch' is set. Defaults to 'main'.",
RequiredWith: []string{"autocreate_branch"},
Type: schema.TypeString,
Default: "main",
Optional: true,
Description: "The branch name to start from, if 'autocreate_branch' is set. Defaults to 'main'.",
RequiredWith: []string{"autocreate_branch"},
DiffSuppressFunc: autoBranchDiffSuppressFunc,
},
"autocreate_branch_source_sha": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The commit hash to start from, if 'autocreate_branch' is set. Defaults to the tip of 'autocreate_branch_source_branch'. If provided, 'autocreate_branch_source_branch' is ignored.",
RequiredWith: []string{"autocreate_branch"},
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The commit hash to start from, if 'autocreate_branch' is set. Defaults to the tip of 'autocreate_branch_source_branch'. If provided, 'autocreate_branch_source_branch' is ignored.",
RequiredWith: []string{"autocreate_branch"},
DiffSuppressFunc: autoBranchDiffSuppressFunc,
},
},
}
Expand Down Expand Up @@ -507,3 +510,13 @@ func resourceGithubRepositoryFileDelete(d *schema.ResourceData, meta interface{}

return nil
}

func autoBranchDiffSuppressFunc(k, _, _ string, d *schema.ResourceData) bool {
if !d.Get("autocreate_branch").(bool) {
switch k {
case "autocreate_branch", "autocreate_branch_source_branch", "autocreate_branch_source_sha":
return true
}
}
return false
}
19 changes: 15 additions & 4 deletions github/resource_github_repository_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ func TestAccGithubRepositoryFile(t *testing.T) {
t.Run("creates and manages files", func(t *testing.T) {

config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-test-%s"
auto_init = true
vulnerability_alerts = true
}
resource "github_repository_file" "test" {
repository = github_repository.test.name
branch = "main"
Expand All @@ -34,7 +34,6 @@ func TestAccGithubRepositoryFile(t *testing.T) {
commit_email = "[email protected]"
}
`, randomID)

check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_repository_file.test", "content",
Expand All @@ -60,6 +59,9 @@ func TestAccGithubRepositoryFile(t *testing.T) {
resource.TestCheckResourceAttrSet(
"github_repository_file.test", "commit_sha",
),
resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch"),
resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch_source_branch"),
resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch_source_sha"),
)

testCase := func(t *testing.T, mode string) {
Expand Down Expand Up @@ -132,6 +134,9 @@ func TestAccGithubRepositoryFile(t *testing.T) {
resource.TestCheckResourceAttrSet(
"github_repository_file.test", "commit_sha",
),
resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch"),
resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch_source_branch"),
resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch_source_sha"),
)

testCase := func(t *testing.T, mode string) {
Expand Down Expand Up @@ -224,6 +229,9 @@ func TestAccGithubRepositoryFile(t *testing.T) {
resource.TestCheckResourceAttrSet(
"github_repository_file.test", "commit_sha",
),
resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch"),
resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch_source_branch"),
resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch_source_sha"),
)

testCase := func(t *testing.T, mode string) {
Expand Down Expand Up @@ -261,7 +269,7 @@ func TestAccGithubRepositoryFile(t *testing.T) {
auto_init = true
vulnerability_alerts = true
}
resource "github_repository_file" "test" {
repository = github_repository.test.name
branch = "does/not/exist"
Expand Down Expand Up @@ -299,6 +307,9 @@ func TestAccGithubRepositoryFile(t *testing.T) {
resource.TestCheckResourceAttrSet(
"github_repository_file.test", "commit_sha",
),
resource.TestCheckResourceAttr("github_repository_file.test", "autocreate_branch", "true"),
resource.TestCheckResourceAttr("github_repository_file.test", "autocreate_branch_source_branch", "main"),
resource.TestCheckResourceAttrSet("github_repository_file.test", "autocreate_branch_source_sha"),
)

testCase := func(t *testing.T, mode string) {
Expand Down

0 comments on commit 5b50181

Please sign in to comment.