Skip to content

Commit 77e8cfe

Browse files
committed
Add validation to ensure rules.required_status_checks.required_checks.context is not empty
Signed-off-by: Timo Sand <[email protected]>
1 parent 21e2e02 commit 77e8cfe

2 files changed

Lines changed: 54 additions & 3 deletions

File tree

github/resource_github_organization_ruleset.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,10 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
260260
Elem: &schema.Resource{
261261
Schema: map[string]*schema.Schema{
262262
"context": {
263-
Type: schema.TypeString,
264-
Required: true,
265-
Description: "The status check context name that must be present on the commit.",
263+
Type: schema.TypeString,
264+
Required: true,
265+
ValidateDiagFunc: validation.ToDiagFunc(validation.StringIsNotEmpty),
266+
Description: "The status check context name that must be present on the commit.",
266267
},
267268
"integration_id": {
268269
Type: schema.TypeInt,

github/resource_github_organization_ruleset_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,56 @@ func TestGithubOrganizationRulesets(t *testing.T) {
735735
t.Skip("organization account not supported for this operation, since it needs a paid Team plan.")
736736
})
737737
})
738+
t.Run("Validates required_status_checks context is not empty", func(t *testing.T) {
739+
resourceName := "test-required-status-checks-context-is-not-empty"
740+
config := fmt.Sprintf(`
741+
resource "github_organization_ruleset" "%s" {
742+
name = "test-context-is-not-empty-%s"
743+
target = "branch"
744+
enforcement = "active"
745+
746+
conditions {
747+
ref_name {
748+
include = ["~ALL"]
749+
exclude = []
750+
}
751+
repository_name {
752+
include = ["~ALL"]
753+
exclude = []
754+
}
755+
}
756+
757+
rules {
758+
required_status_checks {
759+
required_check {
760+
context = ""
761+
}
762+
}
763+
}
764+
}
765+
`, resourceName, randomID)
766+
767+
testCase := func(t *testing.T, mode string) {
768+
resource.Test(t, resource.TestCase{
769+
PreCheck: func() { skipUnlessMode(t, mode) },
770+
Providers: testAccProviders,
771+
Steps: []resource.TestStep{
772+
{
773+
Config: config,
774+
ExpectError: regexp.MustCompile("expected \"context\" to not be an empty string"),
775+
},
776+
},
777+
})
778+
}
779+
780+
t.Run("with an enterprise account", func(t *testing.T) {
781+
testCase(t, enterprise)
782+
})
783+
784+
t.Run("with an organization account", func(t *testing.T) {
785+
t.Skip("organization account not supported for this operation, since it needs a paid Team plan.")
786+
})
787+
})
738788

739789
t.Run("Validates repository target rejects ref_name", func(t *testing.T) {
740790
resourceName := "test-repository-reject-ref-name"

0 commit comments

Comments
 (0)