Skip to content

Commit

Permalink
fix: importing rulesets
Browse files Browse the repository at this point in the history
  • Loading branch information
ihor-hrytskiv committed Dec 2, 2024
1 parent 1c11053 commit 16eb94f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
5 changes: 5 additions & 0 deletions github/resource_github_organization_ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,11 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
Description: "Choose which Actions workflows must pass before branches can be merged into a branch that matches this rule.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"do_not_enforce_on_create": {
Type: schema.TypeBool,
Optional: true,
Description: "Allow repositories and branches to be created if a check would otherwise prohibit it.",
},
"required_workflow": {
Type: schema.TypeSet,
MinItems: 1,
Expand Down
48 changes: 47 additions & 1 deletion github/respository_rules_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,8 @@ func expandRules(input []interface{}, org bool) []*github.RepositoryRule {
}

params := &github.RequiredWorkflowsRuleParameters{
RequiredWorkflows: requiredWorkflows,
DoNotEnforceOnCreate: requiredWorkflowsMap["do_not_enforce_on_create"].(bool),
RequiredWorkflows: requiredWorkflows,
}
rulesSlice = append(rulesSlice, github.NewRequiredWorkflowsRule(params))
}
Expand Down Expand Up @@ -504,6 +505,51 @@ func flattenRules(rules []*github.RepositoryRule, org bool) []interface{} {
rule["required_check"] = requiredStatusChecksSlice
rule["strict_required_status_checks_policy"] = params.StrictRequiredStatusChecksPolicy
rulesMap[v.Type] = []map[string]interface{}{rule}

case "workflows":
var params github.RequiredWorkflowsRuleParameters

err := json.Unmarshal(*v.Parameters, &params)
if err != nil {
log.Printf("[INFO] Unexpected error unmarshalling rule %s with parameters: %v",
v.Type, v.Parameters)
}

requiredWorkflowsSlice := make([]map[string]interface{}, 0)
for _, check := range params.RequiredWorkflows {
requiredWorkflowsSlice = append(requiredWorkflowsSlice, map[string]interface{}{
"repository_id": check.RepositoryID,
"path": check.Path,
"ref": check.Ref,
})
}

rule := make(map[string]interface{})
rule["do_not_enforce_on_create"] = params.DoNotEnforceOnCreate
rule["required_workflow"] = requiredWorkflowsSlice
rulesMap["required_workflows"] = []map[string]interface{}{rule}

case "code_scanning":
var params github.RequiredCodeScanningRuleParameters

err := json.Unmarshal(*v.Parameters, &params)
if err != nil {
log.Printf("[INFO] Unexpected error unmarshalling rule %s with parameters: %v",
v.Type, v.Parameters)
}

requiredCodeScanningSlice := make([]map[string]interface{}, 0)
for _, check := range params.RequiredCodeScanningTools {
requiredCodeScanningSlice = append(requiredCodeScanningSlice, map[string]interface{}{
"alerts_threshold": check.AlertsThreshold,
"security_alerts_threshold": check.SecurityAlertsThreshold,
"tool": check.Tool,
})
}

rule := make(map[string]interface{})
rule["required_code_scanning_tool"] = requiredCodeScanningSlice
rulesMap["required_code_scanning"] = []map[string]interface{}{rule}
}
}

Expand Down

0 comments on commit 16eb94f

Please sign in to comment.