Skip to content

Commit 1ed3507

Browse files
fix(github_repository_file): Commit detail changes should not do empty commits
1 parent 6008909 commit 1ed3507

6 files changed

Lines changed: 109 additions & 9 deletions

File tree

examples/repository_file/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Repository File
2+
3+
This provides a template for managing [repository files](https://docs.github.com/en/repositories/working-with-files/managing-files).
4+
5+
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.
6+
7+
Alternatively, you may use variables passed via the command line or `auto.tfvars`:
8+
9+
```tfvars
10+
organization = ""
11+
github_token = ""
12+
13+
repository = ""
14+
file = ""
15+
content = ""
16+
branch = ""
17+
commit_author = ""
18+
commit_message = ""
19+
commit_email = ""
20+
```
21+
22+
```console
23+
terraform apply
24+
```

examples/repository_file/main.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
resource "github_repository_file" "this" {
2+
repository = var.repository
3+
file = var.file
4+
content = var.content
5+
6+
branch = var.branch
7+
8+
commit_author = var.commit_author
9+
commit_message = var.commit_message
10+
commit_email = var.commit_email
11+
}

examples/repository_file/outputs.tf

Whitespace-only changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
terraform {
2+
required_providers {
3+
github = {
4+
source = "integrations/github"
5+
}
6+
}
7+
}
8+
9+
provider "github" {
10+
owner = var.organization
11+
token = var.github_token
12+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
variable "organization" {
2+
description = "GitHub organization used to configure the provider"
3+
type = string
4+
}
5+
6+
variable "github_token" {
7+
description = "GitHub access token used to configure the provider"
8+
type = string
9+
}
10+
11+
variable "repository" {
12+
description = "The name of the repository"
13+
type = string
14+
}
15+
16+
variable "file" {
17+
description = "The name of the file to create"
18+
type = string
19+
}
20+
variable "content" {
21+
description = "The content of the file to create"
22+
type = string
23+
}
24+
variable "branch" {
25+
description = "The branch to create the file in"
26+
type = string
27+
default = "main"
28+
}
29+
variable "commit_author" {
30+
description = "The name of the author of the commit"
31+
type = string
32+
default = ""
33+
}
34+
variable "commit_message" {
35+
description = "The message of the commit"
36+
type = string
37+
default = ""
38+
}
39+
variable "commit_email" {
40+
description = "The email of the author of the commit"
41+
type = string
42+
default = ""
43+
}

github/resource_github_repository_file.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ package github
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"log"
78
"net/http"
89
"net/url"
10+
"slices"
911
"strings"
1012

11-
"fmt"
12-
1313
"github.com/google/go-github/v66/github"
1414
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1515
)
@@ -437,20 +437,30 @@ func resourceGithubRepositoryFileUpdate(d *schema.ResourceData, meta interface{}
437437
opts.Message = &m
438438
}
439439

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

445-
if err = d.Set("commit_sha", create.GetSHA()); err != nil {
446-
return err
449+
if d.HasChanges(allSchemaKeysButCommitDetails...) {
450+
create, _, err := client.Repositories.CreateFile(ctx, owner, repo, file, opts)
451+
if err != nil {
452+
return err
453+
}
454+
if err = d.Set("commit_sha", create.GetSHA()); err != nil {
455+
return err
456+
}
457+
} else {
458+
log.Printf("[DEBUG] No changes to commit, skipping commit")
447459
}
448-
449460
return resourceGithubRepositoryFileRead(d, meta)
450461
}
451462

452463
func resourceGithubRepositoryFileDelete(d *schema.ResourceData, meta interface{}) error {
453-
454464
client := meta.(*Owner).v3client
455465
owner := meta.(*Owner).name
456466
ctx := context.Background()

0 commit comments

Comments
 (0)