@@ -118,29 +118,54 @@ func validateRules(ctx context.Context, d *schema.ResourceDiff, allowedRules []s
118118 return nil
119119}
120120
121- func validateRepositoryRulesetConditionsFieldForBranchAndTagTargets (ctx context.Context , target Target , conditions map [string ]any ) error {
122- tflog .Debug (ctx , fmt .Sprintf ("Validating conditions field for %s target" , target ), map [string ]any {"target" : target , "conditions" : conditions })
121+ func validateRulesetConditions (ctx context.Context , d * schema.ResourceDiff , isOrg bool ) error {
122+ target := Target (d .Get ("target" ).(string ))
123+ tflog .Debug (ctx , "Validating conditions field based on target" , map [string ]any {"target" : target })
124+ conditionsRaw := d .Get ("conditions" ).([]any )
123125
124- if conditions [ "ref_name" ] == nil || len (conditions [ "ref_name" ].([] any ) ) == 0 {
125- tflog .Debug (ctx , fmt . Sprintf ( "Missing ref_name for %s target" , target ) , map [string ]any {"target" : target })
126- return fmt . Errorf ( "ref_name must be set for %s target" , target )
126+ if len (conditionsRaw ) == 0 {
127+ tflog .Debug (ctx , "An empty conditions block, skipping validation." , map [string ]any {"target" : target })
128+ return nil
127129 }
128130
129- tflog .Debug (ctx , fmt .Sprintf ("Conditions validation passed for %s target" , target ))
131+ conditions := conditionsRaw [0 ].(map [string ]any )
132+
133+ switch target {
134+ case TargetBranch , TargetTag :
135+ return validateConditionsFieldForBranchAndTagTargets (ctx , target , conditions , isOrg )
136+ case TargetPush :
137+ return validateConditionsFieldForPushTarget (ctx , conditions )
138+ }
130139 return nil
131140}
132141
133- func validateConditionsFieldForBranchAndTagTargets (ctx context.Context , target Target , conditions map [string ]any ) error {
134- tflog .Debug (ctx , fmt .Sprintf ("Validating conditions field for %s target" , target ), map [string ]any {"target" : target , "conditions" : conditions })
142+ func validateRulesetRules (ctx context.Context , d * schema.ResourceDiff ) error {
143+ target := Target (d .Get ("target" ).(string ))
144+ tflog .Debug (ctx , "Validating ruleset rules based on target" , map [string ]any {"target" : target })
145+
146+ rulesRaw := d .Get ("rules" ).([]any )
147+ if len (rulesRaw ) == 0 {
148+ tflog .Debug (ctx , "No rules block, skipping validation" )
149+ return nil
150+ }
151+
152+ return validateRulesForTarget (ctx , d )
153+ }
154+
155+ func validateConditionsFieldForBranchAndTagTargets (ctx context.Context , target Target , conditions map [string ]any , isOrg bool ) error {
156+ tflog .Debug (ctx , fmt .Sprintf ("Validating conditions field for %s target" , target ), map [string ]any {"target" : target , "conditions" : conditions , "isOrg" : isOrg })
135157
136158 if conditions ["ref_name" ] == nil || len (conditions ["ref_name" ].([]any )) == 0 {
137159 tflog .Debug (ctx , fmt .Sprintf ("Missing ref_name for %s target" , target ), map [string ]any {"target" : target })
138160 return fmt .Errorf ("ref_name must be set for %s target" , target )
139161 }
140162
141- if (conditions ["repository_name" ] == nil || len (conditions ["repository_name" ].([]any )) == 0 ) && (conditions ["repository_id" ] == nil || len (conditions ["repository_id" ].([]any )) == 0 ) {
142- tflog .Debug (ctx , fmt .Sprintf ("Missing repository_name or repository_id for %s target" , target ), map [string ]any {"target" : target })
143- return fmt .Errorf ("either repository_name or repository_id must be set for %s target" , target )
163+ // Repository rulesets don't have repository_name or repository_id, only org rulesets do.
164+ if isOrg {
165+ if (conditions ["repository_name" ] == nil || len (conditions ["repository_name" ].([]any )) == 0 ) && (conditions ["repository_id" ] == nil || len (conditions ["repository_id" ].([]any )) == 0 ) {
166+ tflog .Debug (ctx , fmt .Sprintf ("Missing repository_name or repository_id for %s target" , target ), map [string ]any {"target" : target })
167+ return fmt .Errorf ("either repository_name or repository_id must be set for %s target" , target )
168+ }
144169 }
145170 tflog .Debug (ctx , fmt .Sprintf ("Conditions validation passed for %s target" , target ))
146171 return nil
0 commit comments