diff --git a/github/resource_github_branch_protection_v3_test.go b/github/resource_github_branch_protection_v3_test.go index 397a2c0263..9f673662d2 100644 --- a/github/resource_github_branch_protection_v3_test.go +++ b/github/resource_github_branch_protection_v3_test.go @@ -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(` diff --git a/github/resource_github_branch_protection_v3_utils.go b/github/resource_github_branch_protection_v3_utils.go index 3a8519802a..b4488a63f0 100644 --- a/github/resource_github_branch_protection_v3_utils.go +++ b/github/resource_github_branch_protection_v3_utils.go @@ -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