From 01b47db51ced195e3df7479cc1144124e9105446 Mon Sep 17 00:00:00 2001 From: Ivan Martos Date: Wed, 20 Aug 2025 20:27:32 +0200 Subject: [PATCH 1/3] fix: improve error handling and define delete file options --- github/resource_github_repository_file.go | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/github/resource_github_repository_file.go b/github/resource_github_repository_file.go index c84a8a1953..f3b012bea3 100644 --- a/github/resource_github_repository_file.go +++ b/github/resource_github_repository_file.go @@ -460,16 +460,9 @@ func resourceGithubRepositoryFileDelete(d *schema.ResourceData, meta interface{} var branch string - message := fmt.Sprintf("Delete %s", file) - - if commitMessage, hasCommitMessage := d.GetOk("commit_message"); hasCommitMessage { - message = commitMessage.(string) - } - - sha := d.Get("sha").(string) - opts := &github.RepositoryContentFileOptions{ - Message: &message, - SHA: &sha, + opts, err := resourceGithubRepositoryFileOptions(d) + if err != nil { + return err } if b, ok := d.GetOk("branch"); ok { @@ -503,9 +496,9 @@ func resourceGithubRepositoryFileDelete(d *schema.ResourceData, meta interface{} opts.Branch = &branch } - _, _, err := client.Repositories.DeleteFile(ctx, owner, repo, file, opts) + _, _, err = client.Repositories.DeleteFile(ctx, owner, repo, file, opts) if err != nil { - return nil + return err } return nil From c204c30591a78509dad4ab2e5e8ed8f4bfefec70 Mon Sep 17 00:00:00 2001 From: Ivan Martos Date: Wed, 20 Aug 2025 21:08:36 +0200 Subject: [PATCH 2/3] fix: update delete file message formatting in GitHub repository file resource --- github/resource_github_repository_file.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/github/resource_github_repository_file.go b/github/resource_github_repository_file.go index f3b012bea3..ede8dada0e 100644 --- a/github/resource_github_repository_file.go +++ b/github/resource_github_repository_file.go @@ -465,6 +465,11 @@ func resourceGithubRepositoryFileDelete(d *schema.ResourceData, meta interface{} return err } + if *opts.Message == fmt.Sprintf("Add %s", file) { + m := fmt.Sprintf("Delete %s", file) + opts.Message = &m + } + if b, ok := d.GetOk("branch"); ok { log.Printf("[DEBUG] Using explicitly set branch: %s", b.(string)) if err := checkRepositoryBranchExists(client, owner, repo, b.(string)); err != nil { From ad479a39dcc0719b563920d974792cbbd92626c2 Mon Sep 17 00:00:00 2001 From: Ivan Martos Date: Wed, 20 Aug 2025 21:08:44 +0200 Subject: [PATCH 3/3] fix: replace hardcoded random ID with dynamic generation in repository file tests --- github/resource_github_repository_file_test.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/github/resource_github_repository_file_test.go b/github/resource_github_repository_file_test.go index 0a2da9aa5a..c11ddd2778 100644 --- a/github/resource_github_repository_file_test.go +++ b/github/resource_github_repository_file_test.go @@ -12,8 +12,6 @@ import ( func TestAccGithubRepositoryFile(t *testing.T) { - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("creates and manages files", func(t *testing.T) { config := fmt.Sprintf(` @@ -33,7 +31,7 @@ func TestAccGithubRepositoryFile(t *testing.T) { commit_author = "Terraform User" commit_email = "terraform@example.com" } - `, randomID) + `, acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)) check := resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr( "github_repository_file.test", "content", @@ -111,7 +109,7 @@ func TestAccGithubRepositoryFile(t *testing.T) { commit_email = "terraform@example.com" } - `, randomID) + `, acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)) check := resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr( @@ -202,7 +200,7 @@ func TestAccGithubRepositoryFile(t *testing.T) { commit_author = "Terraform User" commit_email = "terraform@example.com" } - `, randomID) + `, acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)) check := resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr( @@ -280,7 +278,7 @@ func TestAccGithubRepositoryFile(t *testing.T) { commit_email = "terraform@example.com" autocreate_branch = false } - `, randomID) + `, acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)) check := resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(