Skip to content

Commit d43be85

Browse files
fix(github_repository_file): delete error handling and codesigning (#2736)
* fix: improve error handling and define delete file options * fix: update delete file message formatting in GitHub repository file resource * fix: replace hardcoded random ID with dynamic generation in repository file tests * addresses PR comments * lint fixes --------- Co-authored-by: Nick Floyd <139819+nickfloyd@users.noreply.github.com> Co-authored-by: Nick Floyd <nicholas.floyd.info@gmail.com>
1 parent e2d8f47 commit d43be85

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

github/resource_github_repository_file.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -455,16 +455,14 @@ func resourceGithubRepositoryFileDelete(d *schema.ResourceData, meta any) error
455455

456456
var branch string
457457

458-
message := fmt.Sprintf("Delete %s", file)
459-
460-
if commitMessage, hasCommitMessage := d.GetOk("commit_message"); hasCommitMessage {
461-
message = commitMessage.(string)
458+
opts, err := resourceGithubRepositoryFileOptions(d)
459+
if err != nil {
460+
return err
462461
}
463462

464-
sha := d.Get("sha").(string)
465-
opts := &github.RepositoryContentFileOptions{
466-
Message: &message,
467-
SHA: &sha,
463+
if *opts.Message == fmt.Sprintf("Add %s", file) {
464+
m := fmt.Sprintf("Delete %s", file)
465+
opts.Message = &m
468466
}
469467

470468
if b, ok := d.GetOk("branch"); ok {
@@ -498,7 +496,7 @@ func resourceGithubRepositoryFileDelete(d *schema.ResourceData, meta any) error
498496
opts.Branch = &branch
499497
}
500498

501-
_, _, err := client.Repositories.DeleteFile(ctx, owner, repo, file, opts)
499+
_, _, err = client.Repositories.DeleteFile(ctx, owner, repo, file, opts)
502500
return handleArchivedRepoDelete(err, "repository file", file, owner, repo)
503501
}
504502

github/resource_github_repository_file_test.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import (
1111
)
1212

1313
func TestAccGithubRepositoryFile(t *testing.T) {
14-
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
15-
1614
t.Run("creates and manages files", func(t *testing.T) {
1715
config := fmt.Sprintf(`
1816
@@ -31,7 +29,7 @@ func TestAccGithubRepositoryFile(t *testing.T) {
3129
commit_author = "Terraform User"
3230
commit_email = "terraform@example.com"
3331
}
34-
`, randomID)
32+
`, acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum))
3533
check := resource.ComposeTestCheckFunc(
3634
resource.TestCheckResourceAttr(
3735
"github_repository_file.test", "content",
@@ -107,7 +105,7 @@ func TestAccGithubRepositoryFile(t *testing.T) {
107105
commit_email = "terraform@example.com"
108106
}
109107
110-
`, randomID)
108+
`, acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum))
111109

112110
check := resource.ComposeTestCheckFunc(
113111
resource.TestCheckResourceAttr(
@@ -196,7 +194,7 @@ func TestAccGithubRepositoryFile(t *testing.T) {
196194
commit_author = "Terraform User"
197195
commit_email = "terraform@example.com"
198196
}
199-
`, randomID)
197+
`, acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum))
200198

201199
check := resource.ComposeTestCheckFunc(
202200
resource.TestCheckResourceAttr(
@@ -272,7 +270,7 @@ func TestAccGithubRepositoryFile(t *testing.T) {
272270
commit_email = "terraform@example.com"
273271
autocreate_branch = false
274272
}
275-
`, randomID)
273+
`, acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum))
276274

277275
check := resource.ComposeTestCheckFunc(
278276
resource.TestCheckResourceAttr(
@@ -352,7 +350,7 @@ func TestAccGithubRepositoryFile(t *testing.T) {
352350
commit_author = "Terraform User"
353351
commit_email = "terraform@example.com"
354352
}
355-
`, randomID)
353+
`, acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum))
356354

357355
archivedConfig := strings.Replace(config,
358356
`auto_init = true`,
@@ -391,7 +389,7 @@ func TestAccGithubRepositoryFile(t *testing.T) {
391389
auto_init = true
392390
archived = true
393391
}
394-
`, randomID),
392+
`, acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)),
395393
},
396394
},
397395
})

0 commit comments

Comments
 (0)