Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 12 additions & 2 deletions github/resource_github_repository_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,10 +436,20 @@ func resourceGithubRepositoryFileImport(ctx context.Context, d *schema.ResourceD
owner := meta.(*Owner).name
repo, file := splitRepoFilePath(repoFilePath)

opts := &github.RepositoryContentGetOptions{Ref: branch}
if err := d.Set("branch", branch); err != nil {
repoInfo, _, err := client.Repositories.Get(ctx, owner, repo)
Comment thread
deiga marked this conversation as resolved.
if err != nil {
return nil, err
}
defaultBranch := repoInfo.GetDefaultBranch()
opts := &github.RepositoryContentGetOptions{}

if branch != defaultBranch {
opts.Ref = branch
if err := d.Set("branch", branch); err != nil {
return nil, err
}
}

fc, _, _, err := client.Repositories.GetContents(ctx, owner, repo, file, opts)
if err != nil {
return nil, err
Expand Down
66 changes: 66 additions & 0 deletions github/resource_github_repository_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,4 +400,70 @@ func TestAccGithubRepositoryFile(t *testing.T) {
},
})
})
t.Run("imports_files_without_error", func(t *testing.T) {
randomID := acctest.RandString(5)
repoName := fmt.Sprintf("%sfile-import-%s", testResourcePrefix, randomID)
config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "%s"
auto_init = true
vulnerability_alerts = true
}

resource "github_repository_file" "test" {
repository = github_repository.test.name
file = "test"
content = "bar"
commit_message = "Managed by Terraform"
commit_author = "Terraform User"
commit_email = "[email protected]"
}
`, repoName)

resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnauthenticated(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_repository_file.test", "content",
"bar",
),
resource.TestCheckResourceAttr(
"github_repository_file.test", "sha",
"ba0e162e1c47469e3fe4b393a8bf8c569f302116",
),
resource.TestCheckResourceAttr(
"github_repository_file.test", "ref",
"main",
),
resource.TestCheckResourceAttrSet(
"github_repository_file.test", "commit_author",
),
resource.TestCheckResourceAttrSet(
"github_repository_file.test", "commit_email",
),
resource.TestCheckResourceAttrSet(
"github_repository_file.test", "commit_message",
),
resource.TestCheckResourceAttrSet(
"github_repository_file.test", "commit_sha",
),
resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch"),
resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch_source_branch"),
resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch_source_sha"),
),
},
{
ResourceName: "github_repository_file.test",
ImportState: true,
ImportStateVerify: true,
ImportStateId: fmt.Sprintf("%s/%s:main", repoName, "test"),
ImportStateVerifyIgnore: []string{"commit_author", "commit_email"}, // For some reason `d` doesn't contain the commit author and email when importing.
},
},
})
})
}
48 changes: 21 additions & 27 deletions website/docs/r/repository_file.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ GitHub repository.
## Example Usage

### Existing Branch

```hcl

resource "github_repository" "foo" {
name = "tf-acc-test-%s"
name = "example"
auto_init = true
}

Expand All @@ -36,10 +37,11 @@ resource "github_repository_file" "foo" {
```

### Auto Created Branch

```hcl

resource "github_repository" "foo" {
name = "tf-acc-test-%s"
name = "example"
auto_init = true
}

Expand All @@ -57,55 +59,47 @@ resource "github_repository_file" "foo" {

```


## Argument Reference

The following arguments are supported:

* `repository` - (Required) The repository to create the file in.
- `repository` - (Required) The repository to create the file in.

* `file` - (Required) The path of the file to manage.
- `file` - (Required) The path of the file to manage.

* `content` - (Required) The file content.
- `content` - (Required) The file content.

* `branch` - (Optional) Git branch (defaults to the repository's default branch).
- `branch` - (Optional) Git branch (defaults to the repository's default branch).
The branch must already exist, it will only be created automatically if 'autocreate_branch' is set true.

* `commit_author` - (Optional) Committer author name to use. **NOTE:** GitHub app users may omit author and email information so GitHub can verify commits as the GitHub App. This maybe useful when a branch protection rule requires signed commits.
- `commit_author` - (Optional) Committer author name to use. **NOTE:** GitHub app users may omit author and email information so GitHub can verify commits as the GitHub App. This maybe useful when a branch protection rule requires signed commits.

* `commit_email` - (Optional) Committer email address to use. **NOTE:** GitHub app users may omit author and email information so GitHub can verify commits as the GitHub App. This may be useful when a branch protection rule requires signed commits.
- `commit_email` - (Optional) Committer email address to use. **NOTE:** GitHub app users may omit author and email information so GitHub can verify commits as the GitHub App. This may be useful when a branch protection rule requires signed commits.

* `commit_message` - (Optional) The commit message when creating, updating or deleting the managed file.
- `commit_message` - (Optional) The commit message when creating, updating or deleting the managed file.

* `overwrite_on_create` - (Optional) Enable overwriting existing files. If set to `true` it will overwrite an existing file with the same name. If set to `false` it will fail if there is an existing file with the same name.
- `overwrite_on_create` - (Optional) Enable overwriting existing files. If set to `true` it will overwrite an existing file with the same name. If set to `false` it will fail if there is an existing file with the same name.

* `autocreate_branch` - (Optional) Automatically create the branch if it could not be found. Defaults to false. Subsequent reads if the branch is deleted will occur from 'autocreate_branch_source_branch'.
- `autocreate_branch` - (Optional) Automatically create the branch if it could not be found. Defaults to false. Subsequent reads if the branch is deleted will occur from 'autocreate_branch_source_branch'.

* `autocreate_branch_source_branch` - (Optional) The branch name to start from, if 'autocreate_branch' is set. Defaults to 'main'.
- `autocreate_branch_source_branch` - (Optional) The branch name to start from, if 'autocreate_branch' is set. Defaults to 'main'.

* `autocreate_branch_source_sha` - (Optional) The commit hash to start from, if 'autocreate_branch' is set. Defaults to the tip of 'autocreate_branch_source_branch'. If provided, 'autocreate_branch_source_branch' is ignored.
- `autocreate_branch_source_sha` - (Optional) The commit hash to start from, if 'autocreate_branch' is set. Defaults to the tip of 'autocreate_branch_source_branch'. If provided, 'autocreate_branch_source_branch' is ignored.

## Attributes Reference

The following additional attributes are exported:

* `commit_sha` - The SHA of the commit that modified the file.
- `commit_sha` - The SHA of the commit that modified the file.
Comment thread
stevehipwell marked this conversation as resolved.

* `sha` - The SHA blob of the file.

* `ref` - The name of the commit/branch/tag.
- `sha` - The SHA blob of the file.

- `ref` - The name of the commit/branch/tag.

## Import

Repository files can be imported using a combination of the `repo` and `file`, e.g.
Repository files can be imported using a combination of the `repo`, `file` and `branch`, e.g.

```
$ terraform import github_repository_file.gitignore example/.gitignore
```

To import a file from a branch other than the default branch, append `:` and the branch name, e.g.

```
$ terraform import github_repository_file.gitignore example/.gitignore:dev
```sh
terraform import github_repository_file.gitignore example/.gitignore:main
```
Loading