Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve repo:pattern On github_branch_protection Import #599

Merged
merged 1 commit into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
23 changes: 22 additions & 1 deletion github/resource_github_branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func resourceGithubBranchProtection() *schema.Resource {
Delete: resourceGithubBranchProtectionDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
State: resourceGithubBranchProtectionImport,
},

StateUpgraders: []schema.StateUpgrader{
Expand Down Expand Up @@ -269,3 +269,24 @@ func resourceGithubBranchProtectionDelete(d *schema.ResourceData, meta interface

return err
}

func resourceGithubBranchProtectionImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
repoName, pattern, err := parseTwoPartID(d.Id(), "repository", "pattern")
jcudit marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}

repoID, err := getRepositoryID(repoName, meta)
if err != nil {
return nil, err
}
d.Set("repository_id", repoID)

id, err := getBranchProtectionID(repoName, pattern, meta)
if err != nil {
return nil, err
}
d.SetId(fmt.Sprintf("%s", id))

return []*schema.ResourceData{d}, resourceGithubBranchProtectionRead(d, meta)
}
15 changes: 15 additions & 0 deletions github/resource_github_branch_protection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func TestAccGithubBranchProtection(t *testing.T) {
Expand Down Expand Up @@ -57,6 +58,14 @@ func TestAccGithubBranchProtection(t *testing.T) {
Config: config,
Check: check,
},
{
ResourceName: "github_branch_protection.test",
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: branchProtectionImportStateIdFunc(
fmt.Sprintf("tf-acc-test-%s", randomID), "main",
),
},
},
})
}
Expand Down Expand Up @@ -253,3 +262,9 @@ func TestAccGithubBranchProtection(t *testing.T) {

})
}

func branchProtectionImportStateIdFunc(repo, pattern string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
return fmt.Sprintf("%s:%s", repo, pattern), nil
}
}
4 changes: 2 additions & 2 deletions website/docs/r/branch_protection.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ The following arguments are supported:

## Import

GitHub Branch Protection can be imported using an ID made up of `repository:branch`, e.g.
GitHub Branch Protection can be imported using an ID made up of `repository:pattern`, e.g.

```
$ terraform import github_branch_protection.terraform terraform:master
$ terraform import github_branch_protection.terraform terraform:main
```