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

Fix branch protection import by repository node ID and pattern #713

Merged
merged 2 commits into from
Mar 1, 2021
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
2 changes: 1 addition & 1 deletion github/migrate_github_branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func resourceGithubBranchProtectionUpgradeV0(rawState map[string]interface{}, me
}

branch := rawState["branch"].(string)
protectionRuleID, err := getBranchProtectionID(repoName, branch, meta)
protectionRuleID, err := getBranchProtectionID(repoID, branch, meta)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion github/resource_github_branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func resourceGithubBranchProtectionImport(d *schema.ResourceData, meta interface
}
d.Set("repository_id", repoID)

id, err := getBranchProtectionID(repoName, pattern, meta)
id, err := getBranchProtectionID(repoID, pattern, meta)
if err != nil {
return nil, err
}
Expand Down
28 changes: 26 additions & 2 deletions github/resource_github_branch_protection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestAccGithubBranchProtection(t *testing.T) {
ResourceName: "github_branch_protection.test",
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: branchProtectionImportStateIdFunc(
ImportStateIdFunc: importBranchProtectionByRepoName(
fmt.Sprintf("tf-acc-test-%s", randomID), "main",
),
},
Expand Down Expand Up @@ -122,6 +122,13 @@ func TestAccGithubBranchProtection(t *testing.T) {
Config: config,
Check: check,
},
{
ResourceName: "github_branch_protection.test",
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: importBranchProtectionByRepoID(
"github_repository.test", "main"),
},
},
})
}
Expand Down Expand Up @@ -322,8 +329,25 @@ func TestAccGithubBranchProtection(t *testing.T) {

}

func branchProtectionImportStateIdFunc(repo, pattern string) resource.ImportStateIdFunc {
func importBranchProtectionByRepoName(repo, pattern string) resource.ImportStateIdFunc {
// test importing using an ID of the form <repo-name>:<branch-protection-pattern>
return func(s *terraform.State) (string, error) {
return fmt.Sprintf("%s:%s", repo, pattern), nil
}
}

func importBranchProtectionByRepoID(repoLogicalName, pattern string) resource.ImportStateIdFunc {
// test importing using an ID of the form <repo-node-id>:<branch-protection-pattern>
// by retrieving the GraphQL ID from the terraform.State
return func(s *terraform.State) (string, error) {
repo := s.RootModule().Resources[repoLogicalName]
if repo == nil {
return "", fmt.Errorf("Cannot find %s in terraform state", repoLogicalName)
}
repoID, found := repo.Primary.Attributes["node_id"]
if !found {
return "", fmt.Errorf("Repository %s does not have a node_id in terraform state", repo.Primary.ID)
}
return fmt.Sprintf("%s:%s", repoID, pattern), nil
}
}
7 changes: 3 additions & 4 deletions github/util_v4_branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func setPushes(protection BranchProtectionRule) []string {
return pushActors
}

func getBranchProtectionID(name string, pattern string, meta interface{}) (githubv4.ID, error) {
func getBranchProtectionID(repoID githubv4.ID, pattern string, meta interface{}) (githubv4.ID, error) {
var query struct {
Node struct {
Repository struct {
Expand All @@ -268,11 +268,10 @@ func getBranchProtectionID(name string, pattern string, meta interface{}) (githu
} `graphql:"branchProtectionRules(first: $first, after: $cursor)"`
ID string
} `graphql:"... on Repository"`
} `graphql:"repository(owner: $owner, name: $name)"`
} `graphql:"node(id: $id)"`
}
variables := map[string]interface{}{
"owner": githubv4.String(meta.(*Owner).name),
"name": githubv4.String(name),
"id": repoID,
"first": githubv4.Int(100),
"cursor": (*githubv4.String)(nil),
}
Expand Down