Skip to content

Commit

Permalink
Resolve repo:pattern on github_branch_protection import (#599)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Udit authored Nov 13, 2020
1 parent 8475488 commit 156b503
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
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")
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
```

0 comments on commit 156b503

Please sign in to comment.