@@ -2,6 +2,7 @@ package github
22
33import (
44 "fmt"
5+ "regexp"
56 "testing"
67
78 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
@@ -267,11 +268,6 @@ resource "github_organization_ruleset" "test" {
267268 exclude = []
268269 }
269270
270- ref_name {
271- include = ["~ALL"]
272- exclude = []
273- }
274- }
275271
276272 rules {
277273 file_path_restriction {
@@ -555,6 +551,246 @@ resource "github_organization_ruleset" "test" {
555551 },
556552 })
557553 })
554+
555+ t .Run ("push_no_ref_name" , func (t * testing.T ) {
556+ randomID := acctest .RandStringFromCharSet (5 , acctest .CharSetAlphaNum )
557+ rulesetName := fmt .Sprintf ("%s-push-no-ref-%s" , testResourcePrefix , randomID )
558+
559+ config := fmt .Sprintf (`
560+ resource "github_organization_ruleset" "test" {
561+ name = "%s"
562+ target = "push"
563+ enforcement = "active"
564+
565+ conditions {
566+ repository_name {
567+ include = ["~ALL"]
568+ exclude = []
569+ }
570+ }
571+
572+ rules {
573+ max_file_size {
574+ max_file_size = 50
575+ }
576+ }
577+ }
578+ ` , rulesetName )
579+
580+ resource .Test (t , resource.TestCase {
581+ PreCheck : func () { skipUnlessHasPaidOrgs (t ) },
582+ ProviderFactories : providerFactories ,
583+ Steps : []resource.TestStep {
584+ {
585+ Config : config ,
586+ Check : resource .ComposeTestCheckFunc (
587+ resource .TestCheckResourceAttr ("github_organization_ruleset.test" , "name" , rulesetName ),
588+ resource .TestCheckResourceAttr ("github_organization_ruleset.test" , "target" , "push" ),
589+ ),
590+ },
591+ },
592+ })
593+ })
594+
595+ t .Run ("push_with_ref_name_should_fail" , func (t * testing.T ) {
596+ randomID := acctest .RandStringFromCharSet (5 , acctest .CharSetAlphaNum )
597+ rulesetName := fmt .Sprintf ("%s-push-with-ref-%s" , testResourcePrefix , randomID )
598+
599+ config := fmt .Sprintf (`
600+ resource "github_organization_ruleset" "test" {
601+ name = "%s"
602+ target = "push"
603+ enforcement = "active"
604+
605+ conditions {
606+ repository_name {
607+ include = ["~ALL"]
608+ exclude = []
609+ }
610+
611+ ref_name {
612+ include = ["~ALL"]
613+ exclude = []
614+ }
615+ }
616+
617+ rules {
618+ max_file_size {
619+ max_file_size = 50
620+ }
621+ }
622+ }
623+ ` , rulesetName )
624+
625+ resource .Test (t , resource.TestCase {
626+ PreCheck : func () { skipUnlessHasPaidOrgs (t ) },
627+ ProviderFactories : providerFactories ,
628+ Steps : []resource.TestStep {
629+ {
630+ Config : config ,
631+ ExpectError : regexp .MustCompile ("ref_name cannot be set for push rulesets" ),
632+ },
633+ },
634+ })
635+ })
636+
637+ t .Run ("push_with_repository_property" , func (t * testing.T ) {
638+ randomID := acctest .RandStringFromCharSet (5 , acctest .CharSetAlphaNum )
639+ rulesetName := fmt .Sprintf ("%s-push-repo-prop-%s" , testResourcePrefix , randomID )
640+
641+ config := fmt .Sprintf (`
642+ resource "github_organization_ruleset" "test" {
643+ name = "%s"
644+ target = "push"
645+ enforcement = "active"
646+
647+ conditions {
648+ repository_property {
649+ include {
650+ name = "test-property"
651+ property_values = ["test-value"]
652+ }
653+ exclude {
654+ name = "exclude-property"
655+ property_values = ["exclude-value"]
656+ }
657+ }
658+ }
659+
660+ rules {
661+ max_file_size {
662+ max_file_size = 50
663+ }
664+ }
665+ }
666+ ` , rulesetName )
667+
668+ resource .Test (t , resource.TestCase {
669+ PreCheck : func () { skipUnlessHasPaidOrgs (t ) },
670+ ProviderFactories : providerFactories ,
671+ Steps : []resource.TestStep {
672+ {
673+ Config : config ,
674+ Check : resource .ComposeTestCheckFunc (
675+ resource .TestCheckResourceAttr ("github_organization_ruleset.test" , "name" , rulesetName ),
676+ resource .TestCheckResourceAttr ("github_organization_ruleset.test" , "target" , "push" ),
677+ ),
678+ },
679+ },
680+ })
681+ })
682+
683+ t .Run ("branch_requires_ref_name" , func (t * testing.T ) {
684+ randomID := acctest .RandStringFromCharSet (5 , acctest .CharSetAlphaNum )
685+ rulesetName := fmt .Sprintf ("%s-branch-missing-ref-%s" , testResourcePrefix , randomID )
686+
687+ config := fmt .Sprintf (`
688+ resource "github_organization_ruleset" "test" {
689+ name = "%s"
690+ target = "branch"
691+ enforcement = "active"
692+
693+ conditions {
694+ repository_name {
695+ include = ["~ALL"]
696+ exclude = []
697+ }
698+ }
699+
700+ rules {
701+ creation = true
702+ }
703+ }
704+ ` , rulesetName )
705+
706+ resource .Test (t , resource.TestCase {
707+ PreCheck : func () { skipUnlessHasPaidOrgs (t ) },
708+ ProviderFactories : providerFactories ,
709+ Steps : []resource.TestStep {
710+ {
711+ Config : config ,
712+ ExpectError : regexp .MustCompile ("branch rulesets require ref_name to be set" ),
713+ },
714+ },
715+ })
716+ })
717+
718+ t .Run ("branch_missing_repo_targeting" , func (t * testing.T ) {
719+ randomID := acctest .RandStringFromCharSet (5 , acctest .CharSetAlphaNum )
720+ rulesetName := fmt .Sprintf ("%s-branch-no-repo-%s" , testResourcePrefix , randomID )
721+
722+ config := fmt .Sprintf (`
723+ resource "github_organization_ruleset" "test" {
724+ name = "%s"
725+ target = "branch"
726+ enforcement = "active"
727+
728+ conditions {
729+ ref_name {
730+ include = ["~ALL"]
731+ exclude = []
732+ }
733+ }
734+
735+ rules {
736+ creation = true
737+ }
738+ }
739+ ` , rulesetName )
740+
741+ resource .Test (t , resource.TestCase {
742+ PreCheck : func () { skipUnlessHasPaidOrgs (t ) },
743+ ProviderFactories : providerFactories ,
744+ Steps : []resource.TestStep {
745+ {
746+ Config : config ,
747+ ExpectError : regexp .MustCompile ("branch rulesets require exactly one of repository_id, repository_name, or repository_property" ),
748+ },
749+ },
750+ })
751+ })
752+
753+ t .Run ("multiple_repo_targeting_should_fail" , func (t * testing.T ) {
754+ randomID := acctest .RandStringFromCharSet (5 , acctest .CharSetAlphaNum )
755+ rulesetName := fmt .Sprintf ("%s-multi-target-%s" , testResourcePrefix , randomID )
756+
757+ config := fmt .Sprintf (`
758+ resource "github_organization_ruleset" "test" {
759+ name = "%s"
760+ target = "branch"
761+ enforcement = "active"
762+
763+ conditions {
764+ ref_name {
765+ include = ["~ALL"]
766+ exclude = []
767+ }
768+
769+ repository_id = [123, 456]
770+
771+ repository_name {
772+ include = ["~ALL"]
773+ exclude = []
774+ }
775+ }
776+
777+ rules {
778+ creation = true
779+ }
780+ }
781+ ` , rulesetName )
782+
783+ resource .Test (t , resource.TestCase {
784+ PreCheck : func () { skipUnlessHasPaidOrgs (t ) },
785+ ProviderFactories : providerFactories ,
786+ Steps : []resource.TestStep {
787+ {
788+ Config : config ,
789+ ExpectError : regexp .MustCompile ("require exactly one of repository_id, repository_name, or repository_property" ),
790+ },
791+ },
792+ })
793+ })
558794}
559795
560796func TestOrganizationPushRulesetSupport (t * testing.T ) {
0 commit comments