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"), + }, }, }) } diff --git a/github/util_v4_branch_protection.go b/github/util_v4_branch_protection.go index 5e175c6d90..58ed795bd2 100644 --- a/github/util_v4_branch_protection.go +++ b/github/util_v4_branch_protection.go @@ -297,15 +297,13 @@ 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 - break + return allRules[i].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 {