-
Notifications
You must be signed in to change notification settings - Fork 763
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: automatic branch creation for resource 'github_repository_file' (…
…#2100) * feat: support automatic branch creation for resource 'github_repository_file' * fix: add RequiredWith to new optional variables --------- Co-authored-by: Keegan Campbell <[email protected]> Co-authored-by: Nick Floyd <[email protected]>
- Loading branch information
1 parent
182f8e0
commit e6660d4
Showing
3 changed files
with
209 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -246,4 +246,84 @@ func TestAccGithubRepositoryFile(t *testing.T) { | |
}) | ||
|
||
}) | ||
|
||
t.Run("creates and manages files on auto created branch if branch does not exist", func(t *testing.T) { | ||
|
||
config := fmt.Sprintf(` | ||
resource "github_repository" "test" { | ||
name = "tf-acc-test-%s" | ||
auto_init = true | ||
} | ||
resource "github_repository_file" "test" { | ||
repository = github_repository.test.name | ||
branch = "does/not/exist" | ||
file = "test" | ||
content = "bar" | ||
commit_message = "Managed by Terraform" | ||
commit_author = "Terraform User" | ||
commit_email = "[email protected]" | ||
autocreate_branch = false | ||
} | ||
`, randomID) | ||
|
||
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", | ||
"does/not/exist", | ||
), | ||
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", | ||
), | ||
) | ||
|
||
testCase := func(t *testing.T, mode string) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { skipUnlessMode(t, mode) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
ExpectError: regexp.MustCompile(`unexpected status code: 404 Not Found`), | ||
}, | ||
{ | ||
Config: strings.Replace(config, | ||
"autocreate_branch = false", | ||
"autocreate_branch = true", 1), | ||
Check: check, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
t.Run("with an anonymous account", func(t *testing.T) { | ||
t.Skip("anonymous account not supported for this operation") | ||
}) | ||
|
||
t.Run("with an individual account", func(t *testing.T) { | ||
testCase(t, individual) | ||
}) | ||
|
||
t.Run("with an organization account", func(t *testing.T) { | ||
testCase(t, organization) | ||
}) | ||
|
||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ GitHub repository. | |
|
||
## Example Usage | ||
|
||
### Existing Branch | ||
```hcl | ||
resource "github_repository" "foo" { | ||
|
@@ -33,6 +34,28 @@ resource "github_repository_file" "foo" { | |
``` | ||
|
||
### Auto Created Branch | ||
```hcl | ||
resource "github_repository" "foo" { | ||
name = "tf-acc-test-%s" | ||
auto_init = true | ||
} | ||
resource "github_repository_file" "foo" { | ||
repository = github_repository.foo.name | ||
branch = "does/not/exist" | ||
file = ".gitignore" | ||
content = "**/*.tfstate" | ||
commit_message = "Managed by Terraform" | ||
commit_author = "Terraform User" | ||
commit_email = "[email protected]" | ||
overwrite_on_create = true | ||
autocreate_branch = true | ||
} | ||
``` | ||
|
||
|
||
## Argument Reference | ||
|
||
|
@@ -45,7 +68,7 @@ The following arguments are supported: | |
* `content` - (Required) The file content. | ||
|
||
* `branch` - (Optional) Git branch (defaults to the repository's default branch). | ||
The branch must already exist, it will not be created if it does not already exist. | ||
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. | ||
|
||
|
@@ -55,6 +78,12 @@ The following arguments are supported: | |
|
||
* `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_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. | ||
|
||
## Attributes Reference | ||
|
||
The following additional attributes are exported: | ||
|