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

Add error message for importing branch protection #721

Merged
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
20 changes: 20 additions & 0 deletions github/resource_github_branch_protection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package github

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
Expand Down Expand Up @@ -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",
),
},
},
})
}
Expand Down Expand Up @@ -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"),
},
},
})
}
Expand Down
6 changes: 2 additions & 4 deletions github/util_v4_branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down