Skip to content

Commit 33d3f78

Browse files
committed
Refactor expandConditions to reduce complexity
Refactor logic to reduce the cognitive complexity and add logic to handle the repository_property field
1 parent 7a98de4 commit 33d3f78

File tree

1 file changed

+83
-48
lines changed

1 file changed

+83
-48
lines changed

github/respository_rules_utils.go

Lines changed: 83 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -87,73 +87,108 @@ func expandConditions(input []interface{}, org bool) *github.RulesetConditions {
8787

8888
// ref_name is available for both repo and org rulesets
8989
if v, ok := inputConditions["ref_name"].([]interface{}); ok && v != nil && len(v) != 0 {
90-
inputRefName := v[0].(map[string]interface{})
91-
include := make([]string, 0)
92-
exclude := make([]string, 0)
90+
rulesetConditions.RefName = expandRefNameConditions(v)
91+
}
9392

94-
for _, v := range inputRefName["include"].([]interface{}) {
95-
if v != nil {
96-
include = append(include, v.(string))
97-
}
93+
// org-only fields
94+
if org {
95+
// repository_name and repository_id
96+
if v, ok := inputConditions["repository_name"].([]interface{}); ok && v != nil && len(v) != 0 {
97+
rulesetConditions.RepositoryName = expandRepositoryNameConditions(v)
98+
} else if v, ok := inputConditions["repository_id"].([]interface{}); ok && v != nil && len(v) != 0 {
99+
rulesetConditions.RepositoryID = expandRepositoryIDConditions(v)
100+
} else if v, ok := inputConditions["repository_property"].([]interface{}); ok && v != nil && len(v) != 0 {
101+
rulesetConditions.RepositoryProperty = expandRepositoryPropertyConditions(v)
98102
}
103+
}
99104

100-
for _, v := range inputRefName["exclude"].([]interface{}) {
101-
if v != nil {
102-
exclude = append(exclude, v.(string))
103-
}
105+
return rulesetConditions
106+
}
107+
108+
func expandRefNameConditions(v []interface{}) *github.RulesetRefConditionParameters {
109+
inputRefName := v[0].(map[string]interface{})
110+
include := make([]string, 0)
111+
exclude := make([]string, 0)
112+
113+
for _, v := range inputRefName["include"].([]interface{}) {
114+
if v != nil {
115+
include = append(include, v.(string))
104116
}
117+
}
105118

106-
rulesetConditions.RefName = &github.RulesetRefConditionParameters{
107-
Include: include,
108-
Exclude: exclude,
119+
for _, v := range inputRefName["exclude"].([]interface{}) {
120+
if v != nil {
121+
exclude = append(exclude, v.(string))
109122
}
110123
}
111124

112-
// org-only fields
113-
if org {
114-
// repository_name and repository_id
115-
if v, ok := inputConditions["repository_name"].([]interface{}); ok && v != nil && len(v) != 0 {
116-
inputRepositoryName := v[0].(map[string]interface{})
117-
include := make([]string, 0)
118-
exclude := make([]string, 0)
125+
return &github.RulesetRefConditionParameters{
126+
Include: include,
127+
Exclude: exclude,
128+
}
129+
}
119130

120-
for _, v := range inputRepositoryName["include"].([]interface{}) {
121-
if v != nil {
122-
include = append(include, v.(string))
123-
}
124-
}
131+
func expandRepositoryNameConditions(v []interface{}) *github.RulesetRepositoryNamesConditionParameters {
132+
inputRepositoryName := v[0].(map[string]interface{})
133+
include := make([]string, 0)
134+
exclude := make([]string, 0)
125135

126-
for _, v := range inputRepositoryName["exclude"].([]interface{}) {
127-
if v != nil {
128-
exclude = append(exclude, v.(string))
129-
}
130-
}
136+
for _, v := range inputRepositoryName["include"].([]interface{}) {
137+
if v != nil {
138+
include = append(include, v.(string))
139+
}
140+
}
131141

132-
protected := inputRepositoryName["protected"].(bool)
142+
for _, v := range inputRepositoryName["exclude"].([]interface{}) {
143+
if v != nil {
144+
exclude = append(exclude, v.(string))
145+
}
146+
}
133147

134-
rulesetConditions.RepositoryName = &github.RulesetRepositoryNamesConditionParameters{
135-
Include: include,
136-
Exclude: exclude,
137-
Protected: &protected,
138-
}
139-
} else if v, ok := inputConditions["repository_id"].([]interface{}); ok && v != nil && len(v) != 0 {
140-
repositoryIDs := make([]int64, 0)
148+
protected := inputRepositoryName["protected"].(bool)
141149

142-
for _, v := range v {
143-
if v != nil {
144-
repositoryIDs = append(repositoryIDs, int64(v.(int)))
145-
}
146-
}
150+
return &github.RulesetRepositoryNamesConditionParameters{
151+
Include: include,
152+
Exclude: exclude,
153+
Protected: &protected,
154+
}
155+
}
147156

148-
rulesetConditions.RepositoryID = &github.RulesetRepositoryIDsConditionParameters{RepositoryIDs: repositoryIDs}
149-
} else if v, ok := inputConditions["repository_property"].([]interface{}); ok && v != nil && len(v) != 0 {
150-
rulesetConditions.
157+
func expandRepositoryIDConditions(v []interface{}) *github.RulesetRepositoryIDsConditionParameters {
158+
159+
repositoryIDs := make([]int64, 0)
160+
161+
for _, v := range v {
162+
if v != nil {
163+
repositoryIDs = append(repositoryIDs, int64(v.(int)))
151164
}
152165
}
153166

154-
return rulesetConditions
167+
return &github.RulesetRepositoryIDsConditionParameters{RepositoryIDs: repositoryIDs}
155168
}
156169

170+
func expandRepositoryPropertyConditions(v []interface{}) *github.RulesetRepositoryPropertyConditionParameters {
171+
repositoryProperties := v[0].(map[string]interface{})
172+
include := make([]github.RulesetRepositoryPropertyTargetParameters, 0)
173+
exclude := make([]github.RulesetRepositoryPropertyTargetParameters, 0)
174+
175+
for _, v := range repositoryProperties["include"].([]interface{}) {
176+
if v != nil {
177+
include = append(include, v.(github.RulesetRepositoryPropertyTargetParameters))
178+
}
179+
}
180+
181+
for _, v := range repositoryProperties["exclude"].([]interface{}) {
182+
if v != nil {
183+
exclude = append(exclude, v.(github.RulesetRepositoryPropertyTargetParameters))
184+
}
185+
}
186+
187+
return &github.RulesetRepositoryPropertyConditionParameters{
188+
Include: include,
189+
Exclude: exclude,
190+
}
191+
}
157192
func flattenConditions(conditions *github.RulesetConditions, org bool) []interface{} {
158193
if conditions == nil || conditions.RefName == nil {
159194
return []interface{}{}

0 commit comments

Comments
 (0)