Skip to content

Commit

Permalink
[fix]: Fixes parsing for checks with : in the name.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickfloyd authored Oct 12, 2023
2 parents ce9034e + b30064a commit fc5b047
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 8 deletions.
79 changes: 79 additions & 0 deletions github/resource_github_branch_protection_v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,85 @@ func TestAccGithubBranchProtectionV3_conversation_resolution(t *testing.T) {
func TestAccGithubBranchProtectionV3_required_status_checks(t *testing.T) {
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

t.Run("configures required status checks", func(t *testing.T) {

config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-test-%s"
auto_init = true
}
resource "github_branch_protection_v3" "test" {
repository = github_repository.test.name
branch = "main"
required_status_checks {
strict = true
checks = [
"github/foo",
"github/bar:-1",
"github:foo:baz:1",
]
}
}
`, randomID)

check := resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(
"github_branch_protection_v3.test", "required_status_checks.#", "1",
),
resource.TestCheckResourceAttr(
"github_branch_protection_v3.test", "required_status_checks.strict", "true",
),
resource.TestCheckResourceAttr(
"github_branch_protection_v3.test", "required_status_checks.checks.#", "3",
),
resource.TestCheckResourceAttr(
"github_branch_protection_v3.test", "required_status_checks.checks.0", "github/foo",
),
resource.TestCheckResourceAttr(
"github_branch_protection_v3.test", "required_status_checks.checks.1", "github/bar",
),
resource.TestCheckResourceAttr(
"github_branch_protection_v3.test", "required_status_checks.checks.2", "github:foo:baz",
),
)

testCase := func(t *testing.T, mode string) {
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessMode(t, mode) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: check,
},
},
})
}

t.Run("with an anonymous account", func(t *testing.T) {
t.Skip("anonymous account not supported for this operation")
})

t.Run("with an individual account", func(t *testing.T) {
t.Skip("individual account not supported for this operation")
})

t.Run("with an organization account", func(t *testing.T) {
testCase(t, organization)
})

})
}

func TestAccGithubBranchProtectionV3_required_status_contexts(t *testing.T) {
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

t.Run("configures required status checks", func(t *testing.T) {

config := fmt.Sprintf(`
Expand Down
14 changes: 6 additions & 8 deletions github/resource_github_branch_protection_v3_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,15 +269,13 @@ func expandRequiredStatusChecks(d *schema.ResourceData) (*github.RequiredStatusC
for _, c := range checks {

// Expect a string of "context:app_id", allowing for the absence of "app_id"
parts := strings.SplitN(c, ":", 2)
index := strings.LastIndex(c, ":")
var cContext, cAppId string
switch len(parts) {
case 1:
cContext, cAppId = parts[0], ""
case 2:
cContext, cAppId = parts[0], parts[1]
default:
return nil, fmt.Errorf("Could not parse check '%s'. Expected `context:app_id` or `context`", c)
if index <= 0 {
// If there is no ":" or it's in the first position, there is no app_id.
cContext, cAppId = c, ""
} else {
cContext, cAppId = c[:index], c[index+1:]
}

var rscCheck *github.RequiredStatusCheck
Expand Down

0 comments on commit fc5b047

Please sign in to comment.