diff --git a/github/util_rules.go b/github/util_rules.go index 36075831f2..5e3821a767 100644 --- a/github/util_rules.go +++ b/github/util_rules.go @@ -190,7 +190,7 @@ func expandConditions(input []any, org bool) *github.RepositoryRulesetConditions inputConditions := input[0].(map[string]any) // ref_name is available for both repo and org rulesets - if v, ok := inputConditions["ref_name"].([]any); ok && v != nil && len(v) != 0 { + if v, ok := inputConditions["ref_name"].([]any); ok && v != nil && len(v) != 0 && v[0] != nil { inputRefName := v[0].(map[string]any) include := make([]string, 0) exclude := make([]string, 0) diff --git a/github/util_rules_test.go b/github/util_rules_test.go index 5a8253cc95..caad4e0b44 100644 --- a/github/util_rules_test.go +++ b/github/util_rules_test.go @@ -3,6 +3,7 @@ package github import ( "testing" + "github.com/google/go-cmp/cmp" "github.com/google/go-github/v84/github" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) @@ -1066,6 +1067,57 @@ func TestExpandRepositoryPropertyConditions_NilPropertyValues(t *testing.T) { } } +func TestExpandConditions(t *testing.T) { + t.Parallel() + + for _, d := range []struct { + testName string + input []any + want *github.RepositoryRulesetConditions + }{ + { + testName: "returns_nil_for_empty_input", + input: []any{}, + want: nil, + }, + { + testName: "returns_nil_for_empty_input_slice", + input: []any{nil}, + want: nil, + }, + { + testName: "returns_empty_conditions_for_empty_input_slice", + input: []any{map[string]any{}}, + want: &github.RepositoryRulesetConditions{}, + }, + { + testName: "returns_empty_conditions_for_empty_ref_name", + input: []any{map[string]any{"ref_name": []any{}}}, + want: &github.RepositoryRulesetConditions{}, + }, + { + testName: "returns_empty_conditions_for_empty_ref_name_arrays", + input: []any{map[string]any{"ref_name": []any{map[string]any{"include": []any{}, "exclude": []any{}}}}}, + want: &github.RepositoryRulesetConditions{RefName: &github.RepositoryRulesetRefConditionParameters{Include: []string{}, Exclude: []string{}}}, + }, + { + testName: "returns_empty_conditions_for_nil_ref_name_arrays", + input: []any{map[string]any{"ref_name": []any{nil}}}, + want: &github.RepositoryRulesetConditions{}, + }, + } { + t.Run(d.testName, func(t *testing.T) { + t.Parallel() + + got := expandConditions(d.input, false) + + if diff := cmp.Diff(got, d.want); diff != "" { + t.Fatalf("got %+v, want %+v", got, d.want) + } + }) + } +} + func TestFlattenRulesetRepositoryPropertyTargetParameters(t *testing.T) { input := []*github.RepositoryRulesetRepositoryPropertyTargetParameters{ {