Skip to content

Commit 42f2a8a

Browse files
committed
fix: validate nil ref_name condition entries
1 parent 2c5bcce commit 42f2a8a

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

github/util_ruleset_validation.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ func validateRulesetRules(ctx context.Context, d *schema.ResourceDiff) error {
166166
func validateConditionsFieldForBranchAndTagTargets(ctx context.Context, target github.RulesetTarget, conditions map[string]any, isOrg bool) error {
167167
tflog.Debug(ctx, fmt.Sprintf("Validating conditions field for %s target", target), map[string]any{"target": target, "conditions": conditions, "isOrg": isOrg})
168168

169-
if conditions["ref_name"] == nil || len(conditions["ref_name"].([]any)) == 0 {
169+
refName, ok := conditions["ref_name"].([]any)
170+
if !ok || len(refName) == 0 || refName[0] == nil {
170171
tflog.Debug(ctx, fmt.Sprintf("Missing ref_name for %s target", target), map[string]any{"target": target})
171172
return fmt.Errorf("ref_name must be set for %s target", target)
172173
}
@@ -178,7 +179,7 @@ func validateConditionsFieldForBranchAndTagTargets(ctx context.Context, target g
178179
func validateConditionsFieldForPushTarget(ctx context.Context, conditions map[string]any) error {
179180
tflog.Debug(ctx, "Validating conditions field for push target", map[string]any{"target": "push", "conditions": conditions})
180181

181-
if conditions["ref_name"] != nil && len(conditions["ref_name"].([]any)) > 0 {
182+
if refName, ok := conditions["ref_name"].([]any); ok && len(refName) > 0 && refName[0] != nil {
182183
tflog.Debug(ctx, "Invalid ref_name for push target", map[string]any{"ref_name": conditions["ref_name"]})
183184
return fmt.Errorf("ref_name must not be set for push target")
184185
}

github/util_ruleset_validation_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ func Test_validateConditionsFieldForPushTarget(t *testing.T) {
3030
conditions: map[string]any{"ref_name": []any{}},
3131
expectError: false,
3232
},
33+
{
34+
name: "valid push target with nil ref_name element",
35+
conditions: map[string]any{"ref_name": []any{nil}},
36+
expectError: false,
37+
},
3338
{
3439
name: "invalid push target with ref_name set",
3540
conditions: map[string]any{
@@ -110,6 +115,13 @@ func Test_validateRepositoryRulesetConditionsFieldForBranchAndTagTargets(t *test
110115
expectError: true,
111116
errorMsg: "ref_name must be set for tag target",
112117
},
118+
{
119+
name: "invalid branch target with nil ref_name element",
120+
target: github.RulesetTargetBranch,
121+
conditions: map[string]any{"ref_name": []any{nil}},
122+
expectError: true,
123+
errorMsg: "ref_name must be set for branch target",
124+
},
113125
}
114126

115127
for _, tt := range tests {

0 commit comments

Comments
 (0)