Skip to content

Commit 5b50181

Browse files
authored
fix: Ignore changes to attributes (#2402)
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.
1 parent 0d53cb2 commit 5b50181

File tree

2 files changed

+42
-18
lines changed

2 files changed

+42
-18
lines changed

github/resource_github_repository_file.go

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,24 +122,27 @@ func resourceGithubRepositoryFile() *schema.Resource {
122122
Default: false,
123123
},
124124
"autocreate_branch": {
125-
Type: schema.TypeBool,
126-
Optional: true,
127-
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'",
128-
Default: false,
125+
Type: schema.TypeBool,
126+
Optional: true,
127+
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'",
128+
Default: false,
129+
DiffSuppressFunc: autoBranchDiffSuppressFunc,
129130
},
130131
"autocreate_branch_source_branch": {
131-
Type: schema.TypeString,
132-
Default: "main",
133-
Optional: true,
134-
Description: "The branch name to start from, if 'autocreate_branch' is set. Defaults to 'main'.",
135-
RequiredWith: []string{"autocreate_branch"},
132+
Type: schema.TypeString,
133+
Default: "main",
134+
Optional: true,
135+
Description: "The branch name to start from, if 'autocreate_branch' is set. Defaults to 'main'.",
136+
RequiredWith: []string{"autocreate_branch"},
137+
DiffSuppressFunc: autoBranchDiffSuppressFunc,
136138
},
137139
"autocreate_branch_source_sha": {
138-
Type: schema.TypeString,
139-
Optional: true,
140-
Computed: true,
141-
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.",
142-
RequiredWith: []string{"autocreate_branch"},
140+
Type: schema.TypeString,
141+
Optional: true,
142+
Computed: true,
143+
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.",
144+
RequiredWith: []string{"autocreate_branch"},
145+
DiffSuppressFunc: autoBranchDiffSuppressFunc,
143146
},
144147
},
145148
}
@@ -507,3 +510,13 @@ func resourceGithubRepositoryFileDelete(d *schema.ResourceData, meta interface{}
507510

508511
return nil
509512
}
513+
514+
func autoBranchDiffSuppressFunc(k, _, _ string, d *schema.ResourceData) bool {
515+
if !d.Get("autocreate_branch").(bool) {
516+
switch k {
517+
case "autocreate_branch", "autocreate_branch_source_branch", "autocreate_branch_source_sha":
518+
return true
519+
}
520+
}
521+
return false
522+
}

github/resource_github_repository_file_test.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ func TestAccGithubRepositoryFile(t *testing.T) {
1717
t.Run("creates and manages files", func(t *testing.T) {
1818

1919
config := fmt.Sprintf(`
20-
20+
2121
resource "github_repository" "test" {
2222
name = "tf-acc-test-%s"
2323
auto_init = true
2424
vulnerability_alerts = true
2525
}
26-
26+
2727
resource "github_repository_file" "test" {
2828
repository = github_repository.test.name
2929
branch = "main"
@@ -34,7 +34,6 @@ func TestAccGithubRepositoryFile(t *testing.T) {
3434
commit_email = "[email protected]"
3535
}
3636
`, randomID)
37-
3837
check := resource.ComposeTestCheckFunc(
3938
resource.TestCheckResourceAttr(
4039
"github_repository_file.test", "content",
@@ -60,6 +59,9 @@ func TestAccGithubRepositoryFile(t *testing.T) {
6059
resource.TestCheckResourceAttrSet(
6160
"github_repository_file.test", "commit_sha",
6261
),
62+
resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch"),
63+
resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch_source_branch"),
64+
resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch_source_sha"),
6365
)
6466

6567
testCase := func(t *testing.T, mode string) {
@@ -132,6 +134,9 @@ func TestAccGithubRepositoryFile(t *testing.T) {
132134
resource.TestCheckResourceAttrSet(
133135
"github_repository_file.test", "commit_sha",
134136
),
137+
resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch"),
138+
resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch_source_branch"),
139+
resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch_source_sha"),
135140
)
136141

137142
testCase := func(t *testing.T, mode string) {
@@ -224,6 +229,9 @@ func TestAccGithubRepositoryFile(t *testing.T) {
224229
resource.TestCheckResourceAttrSet(
225230
"github_repository_file.test", "commit_sha",
226231
),
232+
resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch"),
233+
resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch_source_branch"),
234+
resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch_source_sha"),
227235
)
228236

229237
testCase := func(t *testing.T, mode string) {
@@ -261,7 +269,7 @@ func TestAccGithubRepositoryFile(t *testing.T) {
261269
auto_init = true
262270
vulnerability_alerts = true
263271
}
264-
272+
265273
resource "github_repository_file" "test" {
266274
repository = github_repository.test.name
267275
branch = "does/not/exist"
@@ -299,6 +307,9 @@ func TestAccGithubRepositoryFile(t *testing.T) {
299307
resource.TestCheckResourceAttrSet(
300308
"github_repository_file.test", "commit_sha",
301309
),
310+
resource.TestCheckResourceAttr("github_repository_file.test", "autocreate_branch", "true"),
311+
resource.TestCheckResourceAttr("github_repository_file.test", "autocreate_branch_source_branch", "main"),
312+
resource.TestCheckResourceAttrSet("github_repository_file.test", "autocreate_branch_source_sha"),
302313
)
303314

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

0 commit comments

Comments
 (0)