Skip to content

Commit aef6dcf

Browse files
committed
Ensure we only set branch on import if it's not the default branch
Signed-off-by: Timo Sand <[email protected]>
1 parent 3965ecb commit aef6dcf

2 files changed

Lines changed: 17 additions & 6 deletions

File tree

github/resource_github_repository_file.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,10 +436,20 @@ func resourceGithubRepositoryFileImport(ctx context.Context, d *schema.ResourceD
436436
owner := meta.(*Owner).name
437437
repo, file := splitRepoFilePath(repoFilePath)
438438

439-
opts := &github.RepositoryContentGetOptions{Ref: branch}
440-
if err := d.Set("branch", branch); err != nil {
439+
repoInfo, _, err := client.Repositories.Get(ctx, owner, repo)
440+
if err != nil {
441441
return nil, err
442442
}
443+
defaultBranch := repoInfo.GetDefaultBranch()
444+
opts := &github.RepositoryContentGetOptions{}
445+
446+
if branch != defaultBranch {
447+
opts.Ref = branch
448+
if err := d.Set("branch", branch); err != nil {
449+
return nil, err
450+
}
451+
}
452+
443453
fc, _, _, err := client.Repositories.GetContents(ctx, owner, repo, file, opts)
444454
if err != nil {
445455
return nil, err

github/resource_github_repository_file_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -457,10 +457,11 @@ func TestAccGithubRepositoryFile(t *testing.T) {
457457
),
458458
},
459459
{
460-
ResourceName: "github_repository_file.test",
461-
ImportState: true,
462-
ImportStateVerify: true,
463-
ImportStateId: fmt.Sprintf("%s/%s:main", repoName, "test"),
460+
ResourceName: "github_repository_file.test",
461+
ImportState: true,
462+
ImportStateVerify: true,
463+
ImportStateId: fmt.Sprintf("%s/%s:main", repoName, "test"),
464+
ImportStateVerifyIgnore: []string{"commit_author", "commit_email"}, // For some reason `d` doesn't contain the commit author and email when importing.
464465
},
465466
},
466467
})

0 commit comments

Comments
 (0)