Skip to content

fix(github_repository_file): Commit detail changes should not do empty commits #2638

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions examples/repository_file/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Repository File

This provides a template for managing [repository files](https://docs.github.com/en/repositories/working-with-files/managing-files).

This example will also create or update a file in the specified `repository`. See https://www.terraform.io/docs/providers/github/index.html for details on configuring [`providers.tf`](./providers.tf) accordingly.

Alternatively, you may use variables passed via the command line or `auto.tfvars`:

```tfvars
organization = ""
github_token = ""

repository = ""
file = ""
content = ""
branch = ""
commit_author = ""
commit_message = ""
commit_email = ""
```

```console
terraform apply
```
11 changes: 11 additions & 0 deletions examples/repository_file/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
resource "github_repository_file" "this" {
repository = var.repository
file = var.file
content = var.content

branch = var.branch

commit_author = var.commit_author
commit_message = var.commit_message
commit_email = var.commit_email
}
Empty file.
12 changes: 12 additions & 0 deletions examples/repository_file/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
terraform {
required_providers {
github = {
source = "integrations/github"
}
}
}

provider "github" {
owner = var.organization
token = var.github_token
}
43 changes: 43 additions & 0 deletions examples/repository_file/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
variable "organization" {
description = "GitHub organization used to configure the provider"
type = string
}

variable "github_token" {
description = "GitHub access token used to configure the provider"
type = string
}

variable "repository" {
description = "The name of the repository"
type = string
}

variable "file" {
description = "The name of the file to create"
type = string
}
variable "content" {
description = "The content of the file to create"
type = string
}
variable "branch" {
description = "The branch to create the file in"
type = string
default = "main"
}
variable "commit_author" {
description = "The name of the author of the commit"
type = string
default = ""
}
variable "commit_message" {
description = "The message of the commit"
type = string
default = ""
}
variable "commit_email" {
description = "The email of the author of the commit"
type = string
default = ""
}
28 changes: 19 additions & 9 deletions github/resource_github_repository_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package github
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"net/url"
"slices"
"strings"

"fmt"

"github.com/google/go-github/v66/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -437,20 +437,30 @@ func resourceGithubRepositoryFileUpdate(d *schema.ResourceData, meta interface{}
opts.Message = &m
}

create, _, err := client.Repositories.CreateFile(ctx, owner, repo, file, opts)
if err != nil {
return err
schema := resourceGithubRepositoryFile()
allSchemaKeys := make([]string, 0, len(schema.Schema))
for k := range schema.Schema {
allSchemaKeys = append(allSchemaKeys, k)
}
allSchemaKeysButCommitDetails := slices.DeleteFunc(allSchemaKeys, func(v string) bool {
return slices.Contains([]string{"commit_sha", "commit_author", "commit_email", "commit_message"}, v)
})

if err = d.Set("commit_sha", create.GetSHA()); err != nil {
return err
if d.HasChanges(allSchemaKeysButCommitDetails...) {
create, _, err := client.Repositories.CreateFile(ctx, owner, repo, file, opts)
if err != nil {
return err
}
if err = d.Set("commit_sha", create.GetSHA()); err != nil {
return err
}
} else {
log.Printf("[DEBUG] No changes to commit, skipping commit")
}

return resourceGithubRepositoryFileRead(d, meta)
}

func resourceGithubRepositoryFileDelete(d *schema.ResourceData, meta interface{}) error {

client := meta.(*Owner).v3client
owner := meta.(*Owner).name
ctx := context.Background()
Expand Down