Skip to content

Commit fc5b047

Browse files
authored
[fix]: Fixes parsing for checks with : in the name.
2 parents ce9034e + b30064a commit fc5b047

File tree

2 files changed

+85
-8
lines changed

2 files changed

+85
-8
lines changed

github/resource_github_branch_protection_v3_test.go

+79
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,85 @@ func TestAccGithubBranchProtectionV3_conversation_resolution(t *testing.T) {
154154
func TestAccGithubBranchProtectionV3_required_status_checks(t *testing.T) {
155155
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
156156

157+
t.Run("configures required status checks", func(t *testing.T) {
158+
159+
config := fmt.Sprintf(`
160+
161+
resource "github_repository" "test" {
162+
name = "tf-acc-test-%s"
163+
auto_init = true
164+
}
165+
166+
resource "github_branch_protection_v3" "test" {
167+
168+
repository = github_repository.test.name
169+
branch = "main"
170+
171+
required_status_checks {
172+
strict = true
173+
checks = [
174+
"github/foo",
175+
"github/bar:-1",
176+
"github:foo:baz:1",
177+
]
178+
}
179+
180+
}
181+
182+
`, randomID)
183+
184+
check := resource.ComposeAggregateTestCheckFunc(
185+
resource.TestCheckResourceAttr(
186+
"github_branch_protection_v3.test", "required_status_checks.#", "1",
187+
),
188+
resource.TestCheckResourceAttr(
189+
"github_branch_protection_v3.test", "required_status_checks.strict", "true",
190+
),
191+
resource.TestCheckResourceAttr(
192+
"github_branch_protection_v3.test", "required_status_checks.checks.#", "3",
193+
),
194+
resource.TestCheckResourceAttr(
195+
"github_branch_protection_v3.test", "required_status_checks.checks.0", "github/foo",
196+
),
197+
resource.TestCheckResourceAttr(
198+
"github_branch_protection_v3.test", "required_status_checks.checks.1", "github/bar",
199+
),
200+
resource.TestCheckResourceAttr(
201+
"github_branch_protection_v3.test", "required_status_checks.checks.2", "github:foo:baz",
202+
),
203+
)
204+
205+
testCase := func(t *testing.T, mode string) {
206+
resource.Test(t, resource.TestCase{
207+
PreCheck: func() { skipUnlessMode(t, mode) },
208+
Providers: testAccProviders,
209+
Steps: []resource.TestStep{
210+
{
211+
Config: config,
212+
Check: check,
213+
},
214+
},
215+
})
216+
}
217+
218+
t.Run("with an anonymous account", func(t *testing.T) {
219+
t.Skip("anonymous account not supported for this operation")
220+
})
221+
222+
t.Run("with an individual account", func(t *testing.T) {
223+
t.Skip("individual account not supported for this operation")
224+
})
225+
226+
t.Run("with an organization account", func(t *testing.T) {
227+
testCase(t, organization)
228+
})
229+
230+
})
231+
}
232+
233+
func TestAccGithubBranchProtectionV3_required_status_contexts(t *testing.T) {
234+
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
235+
157236
t.Run("configures required status checks", func(t *testing.T) {
158237

159238
config := fmt.Sprintf(`

github/resource_github_branch_protection_v3_utils.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,13 @@ func expandRequiredStatusChecks(d *schema.ResourceData) (*github.RequiredStatusC
269269
for _, c := range checks {
270270

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

283281
var rscCheck *github.RequiredStatusCheck

0 commit comments

Comments
 (0)