Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion github/util_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
52 changes: 52 additions & 0 deletions github/util_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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{
{
Expand Down