From 5781fe6a34a1ff14c9eac6eca3ab73c17d7deefd Mon Sep 17 00:00:00 2001 From: Julian Tibble Date: Mon, 8 Mar 2021 20:49:46 +0000 Subject: [PATCH 1/3] Add error message for importing branch protection Add a useful error message when a user attempts to import a branch protection rule that does not exist. Before: Error: nil entry in ImportState results. This is always a bug with the resource that is being imported. Please report this as a bug to Terraform. After: Error: Could not find a branch protection rule with the pattern 'master'. --- github/util_v4_branch_protection.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/github/util_v4_branch_protection.go b/github/util_v4_branch_protection.go index 5e175c6d90..720bd2f785 100644 --- a/github/util_v4_branch_protection.go +++ b/github/util_v4_branch_protection.go @@ -301,11 +301,11 @@ func getBranchProtectionID(repoID githubv4.ID, pattern string, meta interface{}) for i := range allRules { if allRules[i].Pattern == pattern { id = allRules[i].ID - break + return id, nil } } - return id, nil + return nil, fmt.Errorf("Could not find a branch protection rule with the pattern '%s'.", pattern) } func statusChecksDiffSuppression(k, old, new string, d *schema.ResourceData) bool { From dba4237fd979d608538a573cb346033303c3e2f7 Mon Sep 17 00:00:00 2001 From: Julian Tibble Date: Tue, 16 Mar 2021 21:55:11 +0000 Subject: [PATCH 2/3] Test error message for importing branch protection --- .../resource_github_branch_protection_test.go | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/github/resource_github_branch_protection_test.go b/github/resource_github_branch_protection_test.go index e56d2eadbc..04f9f3c67f 100644 --- a/github/resource_github_branch_protection_test.go +++ b/github/resource_github_branch_protection_test.go @@ -2,6 +2,7 @@ package github import ( "fmt" + "regexp" "testing" "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" @@ -66,6 +67,16 @@ func TestAccGithubBranchProtection(t *testing.T) { fmt.Sprintf("tf-acc-test-%s", randomID), "main", ), }, + { + ResourceName: "github_branch_protection.test", + ImportState: true, + ExpectError: regexp.MustCompile( + `Could not find a branch protection rule with the pattern 'no-such-pattern'\.`, + ), + ImportStateIdFunc: importBranchProtectionByRepoName( + fmt.Sprintf("tf-acc-test-%s", randomID), "no-such-pattern", + ), + }, }, }) } @@ -129,6 +140,15 @@ func TestAccGithubBranchProtection(t *testing.T) { ImportStateIdFunc: importBranchProtectionByRepoID( "github_repository.test", "main"), }, + { + ResourceName: "github_branch_protection.test", + ImportState: true, + ExpectError: regexp.MustCompile( + `Could not find a branch protection rule with the pattern 'no-such-pattern'\.`, + ), + ImportStateIdFunc: importBranchProtectionByRepoID( + "github_repository.test", "no-such-pattern"), + }, }, }) } From ec9926317c548220c024b9258a7bab295b2ae494 Mon Sep 17 00:00:00 2001 From: Julian Tibble Date: Wed, 17 Mar 2021 10:57:37 +0000 Subject: [PATCH 3/3] Simplify code in branch protection import --- github/util_v4_branch_protection.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/github/util_v4_branch_protection.go b/github/util_v4_branch_protection.go index 720bd2f785..58ed795bd2 100644 --- a/github/util_v4_branch_protection.go +++ b/github/util_v4_branch_protection.go @@ -297,11 +297,9 @@ func getBranchProtectionID(repoID githubv4.ID, pattern string, meta interface{}) variables["cursor"] = githubv4.NewString(query.Node.Repository.BranchProtectionRules.PageInfo.EndCursor) } - var id string for i := range allRules { if allRules[i].Pattern == pattern { - id = allRules[i].ID - return id, nil + return allRules[i].ID, nil } }