Skip to content
16 changes: 7 additions & 9 deletions github/resource_github_repository_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand Down
14 changes: 6 additions & 8 deletions github/resource_github_repository_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(`

Expand All @@ -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",
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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`,
Expand Down Expand Up @@ -391,7 +389,7 @@ func TestAccGithubRepositoryFile(t *testing.T) {
auto_init = true
archived = true
}
`, randomID),
`, acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)),
},
},
})
Expand Down