Skip to content

Commit 6b2938f

Browse files
committed
Update branch to Computed field
Signed-off-by: Timo Sand <[email protected]>
1 parent aef6dcf commit 6b2938f

2 files changed

Lines changed: 91 additions & 9 deletions

File tree

github/resource_github_repository_file.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"net/http"
88
"net/url"
9+
"strings"
910

1011
"github.com/google/go-github/v82/github"
1112
"github.com/hashicorp/terraform-plugin-log/tflog"
@@ -47,6 +48,7 @@ func resourceGithubRepositoryFile() *schema.Resource {
4748
Type: schema.TypeString,
4849
Optional: true,
4950
ForceNew: true,
51+
Computed: true,
5052
Description: "The branch name, defaults to the repository's default branch",
5153
},
5254
"ref": {
@@ -175,6 +177,14 @@ func resourceGithubRepositoryFileCreate(ctx context.Context, d *schema.ResourceD
175177
}
176178
}
177179
checkOpt.Ref = branch.(string)
180+
} else {
181+
repoInfo, _, err := client.Repositories.Get(ctx, owner, repo)
182+
if err != nil {
183+
return diag.FromErr(err)
184+
}
185+
if err := d.Set("branch", repoInfo.GetDefaultBranch()); err != nil {
186+
return diag.FromErr(err)
187+
}
178188
}
179189

180190
opts := resourceGithubRepositoryFileOptions(d)
@@ -427,27 +437,34 @@ func autoBranchDiffSuppressFunc(k, _, _ string, d *schema.ResourceData) bool {
427437
}
428438

429439
func resourceGithubRepositoryFileImport(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
430-
repoFilePath, branch, err := parseID2(d.Id())
431-
if err != nil {
432-
return nil, fmt.Errorf("invalid ID specified. Supplied ID must be written as <repository>/<file path>:<branch>. %w", err)
440+
importIDParts := strings.Split(d.Id(), idSeparator)
441+
442+
if len(importIDParts) > 2 {
443+
return nil, fmt.Errorf("invalid ID specified. Supplied ID must be written as <repository>/<file path> (when branch is \"main\") or <repository>/<file path>:<branch>")
433444
}
445+
repoFilePath := importIDParts[0]
434446

435447
client := meta.(*Owner).v3client
436448
owner := meta.(*Owner).name
437449
repo, file := splitRepoFilePath(repoFilePath)
438450

439-
repoInfo, _, err := client.Repositories.Get(ctx, owner, repo)
440-
if err != nil {
441-
return nil, err
442-
}
443-
defaultBranch := repoInfo.GetDefaultBranch()
444451
opts := &github.RepositoryContentGetOptions{}
445452

446-
if branch != defaultBranch {
453+
if len(importIDParts) == 2 {
454+
branch := importIDParts[1]
447455
opts.Ref = branch
448456
if err := d.Set("branch", branch); err != nil {
449457
return nil, err
450458
}
459+
} else {
460+
repoInfo, _, err := client.Repositories.Get(ctx, owner, repo)
461+
if err != nil {
462+
return nil, err
463+
}
464+
defaultBranch := repoInfo.GetDefaultBranch()
465+
if err := d.Set("branch", defaultBranch); err != nil {
466+
return nil, err
467+
}
451468
}
452469

453470
fc, _, _, err := client.Repositories.GetContents(ctx, owner, repo, file, opts)

github/resource_github_repository_file_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,71 @@ func TestAccGithubRepositoryFile(t *testing.T) {
420420
}
421421
`, repoName)
422422

423+
resource.Test(t, resource.TestCase{
424+
PreCheck: func() { skipUnauthenticated(t) },
425+
ProviderFactories: providerFactories,
426+
Steps: []resource.TestStep{
427+
{
428+
Config: config,
429+
Check: resource.ComposeTestCheckFunc(
430+
resource.TestCheckResourceAttr(
431+
"github_repository_file.test", "content",
432+
"bar",
433+
),
434+
resource.TestCheckResourceAttr(
435+
"github_repository_file.test", "sha",
436+
"ba0e162e1c47469e3fe4b393a8bf8c569f302116",
437+
),
438+
resource.TestCheckResourceAttr(
439+
"github_repository_file.test", "ref",
440+
"main",
441+
),
442+
resource.TestCheckResourceAttrSet(
443+
"github_repository_file.test", "commit_author",
444+
),
445+
resource.TestCheckResourceAttrSet(
446+
"github_repository_file.test", "commit_email",
447+
),
448+
resource.TestCheckResourceAttrSet(
449+
"github_repository_file.test", "commit_message",
450+
),
451+
resource.TestCheckResourceAttrSet(
452+
"github_repository_file.test", "commit_sha",
453+
),
454+
resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch"),
455+
resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch_source_branch"),
456+
resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch_source_sha"),
457+
),
458+
},
459+
{
460+
ResourceName: "github_repository_file.test",
461+
ImportState: true,
462+
ImportStateVerify: true,
463+
ImportStateVerifyIgnore: []string{"commit_author", "commit_email"}, // For some reason `d` doesn't contain the commit author and email when importing.
464+
},
465+
},
466+
})
467+
})
468+
t.Run("imports_files_with_branch_in_id_without_error", func(t *testing.T) {
469+
randomID := acctest.RandString(5)
470+
repoName := fmt.Sprintf("%sfile-import-%s", testResourcePrefix, randomID)
471+
config := fmt.Sprintf(`
472+
resource "github_repository" "test" {
473+
name = "%s"
474+
auto_init = true
475+
vulnerability_alerts = true
476+
}
477+
478+
resource "github_repository_file" "test" {
479+
repository = github_repository.test.name
480+
file = "test"
481+
content = "bar"
482+
commit_message = "Managed by Terraform"
483+
commit_author = "Terraform User"
484+
commit_email = "[email protected]"
485+
}
486+
`, repoName)
487+
423488
resource.Test(t, resource.TestCase{
424489
PreCheck: func() { skipUnauthenticated(t) },
425490
ProviderFactories: providerFactories,

0 commit comments

Comments
 (0)