diff --git a/github/resource_github_repository_file.go b/github/resource_github_repository_file.go index 46775f9c6..8a9c278cd 100644 --- a/github/resource_github_repository_file.go +++ b/github/resource_github_repository_file.go @@ -455,16 +455,14 @@ func resourceGithubRepositoryFileDelete(d *schema.ResourceData, meta any) error var branch string - message := fmt.Sprintf("Delete %s", file) - - if commitMessage, hasCommitMessage := d.GetOk("commit_message"); hasCommitMessage { - message = commitMessage.(string) + opts, err := resourceGithubRepositoryFileOptions(d) + if err != nil { + return err } - sha := d.Get("sha").(string) - opts := &github.RepositoryContentFileOptions{ - Message: &message, - SHA: &sha, + if *opts.Message == fmt.Sprintf("Add %s", file) { + m := fmt.Sprintf("Delete %s", file) + opts.Message = &m } if b, ok := d.GetOk("branch"); ok { @@ -498,7 +496,7 @@ func resourceGithubRepositoryFileDelete(d *schema.ResourceData, meta any) error opts.Branch = &branch } - _, _, err := client.Repositories.DeleteFile(ctx, owner, repo, file, opts) + _, _, err = client.Repositories.DeleteFile(ctx, owner, repo, file, opts) return handleArchivedRepoDelete(err, "repository file", file, owner, repo) } diff --git a/github/resource_github_repository_file_test.go b/github/resource_github_repository_file_test.go index 34d37c589..03603e5d8 100644 --- a/github/resource_github_repository_file_test.go +++ b/github/resource_github_repository_file_test.go @@ -11,8 +11,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(` @@ -31,7 +29,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", @@ -107,7 +105,7 @@ func TestAccGithubRepositoryFile(t *testing.T) { commit_email = "terraform@example.com" } - `, randomID) + `, acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)) check := resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr( @@ -196,7 +194,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( @@ -272,7 +270,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( @@ -352,7 +350,7 @@ func TestAccGithubRepositoryFile(t *testing.T) { commit_author = "Terraform User" commit_email = "terraform@example.com" } - `, randomID) + `, acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)) archivedConfig := strings.Replace(config, `auto_init = true`, @@ -391,7 +389,7 @@ func TestAccGithubRepositoryFile(t *testing.T) { auto_init = true archived = true } - `, randomID), + `, acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)), }, }, })