Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
47 changes: 47 additions & 0 deletions github/resource_github_organization_ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,53 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
},
},
},
"repository_property": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: "Conditions to target repositories by property ",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"include": {
Type: schema.TypeList,
Required: true,
Description: "Array of repository names or patterns to include. One of these patterns must match for the condition to pass. Also accepts `~ALL` to include all repositories.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{

"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the repository property to target",
},
"property_values": {
Type: schema.TypeList,
Required: true,
Description: "The values to match for the repository property.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
Comment on lines +140 to +142
Copy link

@PaarthShah PaarthShah Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this handle properties with boolean values? From a json export which shows the use of string "false" and "true" I assume it does

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I manualy tested the solution and can handle boelean custom_properties using strings

},
"source": {
Type: schema.TypeString,
Optional: true,
Description: "The source of the repository property. Defaults to 'custom' if not specified.Can be one of: custom, system",
Default: "custom",
},
},
},
},
"exclude": {
Type: schema.TypeList,
Required: true,
Description: "Array of repository names or patterns to exclude. The condition will not pass if any of these patterns match.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
"repository_name": {
Type: schema.TypeList,
Optional: true,
Expand Down
138 changes: 92 additions & 46 deletions github/respository_rules_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,71 +87,108 @@ func expandConditions(input []interface{}, org bool) *github.RulesetConditions {

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

for _, v := range inputRefName["include"].([]interface{}) {
if v != nil {
include = append(include, v.(string))
}
// org-only fields
if org {
// repository_name and repository_id
if v, ok := inputConditions["repository_name"].([]interface{}); ok && v != nil && len(v) != 0 {
rulesetConditions.RepositoryName = expandRepositoryNameConditions(v)
} else if v, ok := inputConditions["repository_id"].([]interface{}); ok && v != nil && len(v) != 0 {
rulesetConditions.RepositoryID = expandRepositoryIDConditions(v)
} else if v, ok := inputConditions["repository_property"].([]interface{}); ok && v != nil && len(v) != 0 {
rulesetConditions.RepositoryProperty = expandRepositoryPropertyConditions(v)
}
}

for _, v := range inputRefName["exclude"].([]interface{}) {
if v != nil {
exclude = append(exclude, v.(string))
}
return rulesetConditions
}

func expandRefNameConditions(v []interface{}) *github.RulesetRefConditionParameters {
inputRefName := v[0].(map[string]interface{})
include := make([]string, 0)
exclude := make([]string, 0)

for _, v := range inputRefName["include"].([]interface{}) {
if v != nil {
include = append(include, v.(string))
}
}

rulesetConditions.RefName = &github.RulesetRefConditionParameters{
Include: include,
Exclude: exclude,
for _, v := range inputRefName["exclude"].([]interface{}) {
if v != nil {
exclude = append(exclude, v.(string))
}
}

// org-only fields
if org {
// repository_name and repository_id
if v, ok := inputConditions["repository_name"].([]interface{}); ok && v != nil && len(v) != 0 {
inputRepositoryName := v[0].(map[string]interface{})
include := make([]string, 0)
exclude := make([]string, 0)
return &github.RulesetRefConditionParameters{
Include: include,
Exclude: exclude,
}
}

for _, v := range inputRepositoryName["include"].([]interface{}) {
if v != nil {
include = append(include, v.(string))
}
}
func expandRepositoryNameConditions(v []interface{}) *github.RulesetRepositoryNamesConditionParameters {
inputRepositoryName := v[0].(map[string]interface{})
include := make([]string, 0)
exclude := make([]string, 0)

for _, v := range inputRepositoryName["exclude"].([]interface{}) {
if v != nil {
exclude = append(exclude, v.(string))
}
}
for _, v := range inputRepositoryName["include"].([]interface{}) {
if v != nil {
include = append(include, v.(string))
}
}

protected := inputRepositoryName["protected"].(bool)
for _, v := range inputRepositoryName["exclude"].([]interface{}) {
if v != nil {
exclude = append(exclude, v.(string))
}
}

rulesetConditions.RepositoryName = &github.RulesetRepositoryNamesConditionParameters{
Include: include,
Exclude: exclude,
Protected: &protected,
}
} else if v, ok := inputConditions["repository_id"].([]interface{}); ok && v != nil && len(v) != 0 {
repositoryIDs := make([]int64, 0)
protected := inputRepositoryName["protected"].(bool)

for _, v := range v {
if v != nil {
repositoryIDs = append(repositoryIDs, int64(v.(int)))
}
}
return &github.RulesetRepositoryNamesConditionParameters{
Include: include,
Exclude: exclude,
Protected: &protected,
}
}

rulesetConditions.RepositoryID = &github.RulesetRepositoryIDsConditionParameters{RepositoryIDs: repositoryIDs}
func expandRepositoryIDConditions(v []interface{}) *github.RulesetRepositoryIDsConditionParameters {

repositoryIDs := make([]int64, 0)

for _, v := range v {
if v != nil {
repositoryIDs = append(repositoryIDs, int64(v.(int)))
}
}

return rulesetConditions
return &github.RulesetRepositoryIDsConditionParameters{RepositoryIDs: repositoryIDs}
}

func expandRepositoryPropertyConditions(v []interface{}) *github.RulesetRepositoryPropertyConditionParameters {
repositoryProperties := v[0].(map[string]interface{})
include := make([]github.RulesetRepositoryPropertyTargetParameters, 0)
exclude := make([]github.RulesetRepositoryPropertyTargetParameters, 0)

for _, v := range repositoryProperties["include"].([]interface{}) {
if v != nil {
include = append(include, v.(github.RulesetRepositoryPropertyTargetParameters))
}
}

for _, v := range repositoryProperties["exclude"].([]interface{}) {
if v != nil {
exclude = append(exclude, v.(github.RulesetRepositoryPropertyTargetParameters))
}
}

return &github.RulesetRepositoryPropertyConditionParameters{
Include: include,
Exclude: exclude,
}
}
func flattenConditions(conditions *github.RulesetConditions, org bool) []interface{} {
if conditions == nil || conditions.RefName == nil {
return []interface{}{}
Expand Down Expand Up @@ -189,6 +226,15 @@ func flattenConditions(conditions *github.RulesetConditions, org bool) []interfa
if conditions.RepositoryID != nil {
conditionsMap["repository_id"] = conditions.RepositoryID.RepositoryIDs
}
if conditions.RepositoryProperty != nil {
repositoryPropertySlice := make([]map[string]interface{}, 0)

repositoryPropertySlice = append(repositoryPropertySlice, map[string]interface{}{
"include": conditions.RepositoryProperty.Include,
"exclude": conditions.RepositoryProperty.Exclude,
})
conditionsMap["repository_property"] = repositoryPropertySlice
}
}

return []interface{}{conditionsMap}
Expand Down