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