Skip to content

Commit

Permalink
[fix] Fixes for org and repo rulesets
Browse files Browse the repository at this point in the history
  • Loading branch information
nickfloyd authored Sep 15, 2023
2 parents 3323096 + e4ea9c4 commit 39a4511
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
10 changes: 5 additions & 5 deletions github/resource_github_organization_ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
ValidateFunc: validation.StringInSlice([]string{"disabled", "active", "evaluate"}, false),
Description: "Possible values for Enforcement are `disabled`, `active`, `evaluate`. Note: `evaluate` is currently only supported for owners of type `organization`.",
},

"bypass_actors": {
Type: schema.TypeList,
Optional: true,
Expand All @@ -63,11 +62,12 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
},
"bypass_mode": {
Type: schema.TypeString,
Optional: true,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"always", "pull_request"}, false),
Description: "When the specified actor can bypass the ruleset. pull_request means that an actor can only bypass rules on pull requests. Can be one of: `always`, `pull_request`.",
},
}},
},
},
},
"node_id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -118,7 +118,7 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
ConflictsWith: []string{"conditions.0.repository_id"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"inlcude": {
"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.",
Expand All @@ -137,6 +137,7 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
"protected": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Whether renaming of target repositories is prevented.",
},
},
Expand All @@ -145,7 +146,6 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
"repository_id": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ConflictsWith: []string{"conditions.0.repository_name"},
Description: "The repository IDs that the ruleset applies to. One of these IDs must match for the condition to pass.",
Elem: &schema.Schema{
Expand Down
2 changes: 1 addition & 1 deletion github/resource_github_repository_ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func resourceGithubRepositoryRuleset() *schema.Resource {
},
"bypass_mode": {
Type: schema.TypeString,
Optional: true,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"always", "pull_request"}, false),
Description: "When the specified actor can bypass the ruleset. pull_request means that an actor can only bypass rules on pull requests. Can be one of: `always`, `pull_request`.",
},
Expand Down
38 changes: 27 additions & 11 deletions github/respository_rules_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package github
import (
"encoding/json"
"log"
"sort"

"github.com/google/go-github/v55/github"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -54,7 +55,6 @@ func expandBypassActors(input []interface{}) []*github.BypassActor {
}
bypassActors = append(bypassActors, actor)
}

return bypassActors
}

Expand All @@ -63,7 +63,11 @@ func flattenBypassActors(bypassActors []*github.BypassActor) []interface{} {
return []interface{}{}
}

actorsSlice := make([]map[string]interface{}, 0)
sort.SliceStable(bypassActors, func(i, j int) bool {
return bypassActors[i].GetActorID() > bypassActors[j].GetActorID()
})

actorsSlice := make([]interface{}, 0)
for _, v := range bypassActors {
actorMap := make(map[string]interface{})

Expand All @@ -74,7 +78,7 @@ func flattenBypassActors(bypassActors []*github.BypassActor) []interface{} {
actorsSlice = append(actorsSlice, actorMap)
}

return []interface{}{actorsSlice}
return actorsSlice
}

func expandConditions(input []interface{}, org bool) *github.RulesetConditions {
Expand Down Expand Up @@ -110,7 +114,7 @@ func expandConditions(input []interface{}, org bool) *github.RulesetConditions {

// org-only fields
if org {
// repository_name
// 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)
Expand All @@ -135,10 +139,7 @@ func expandConditions(input []interface{}, org bool) *github.RulesetConditions {
Exclude: exclude,
Protected: &protected,
}
}

// repository_id
if v, ok := inputConditions["repository_id"].([]interface{}); ok && v != nil {
} else if v, ok := inputConditions["repository_id"].([]interface{}); ok && v != nil && len(v) != 0 {
repositoryIDs := make([]int64, 0)

for _, v := range v {
Expand Down Expand Up @@ -174,10 +175,16 @@ func flattenConditions(conditions *github.RulesetConditions, org bool) []interfa
repositoryNameSlice := make([]map[string]interface{}, 0)

if conditions.RepositoryName != nil {
var protected bool

if conditions.RepositoryName.Protected != nil {
protected = *conditions.RepositoryName.Protected
}

repositoryNameSlice = append(repositoryNameSlice, map[string]interface{}{
"include": conditions.RepositoryName.Include,
"exclude": conditions.RepositoryName.Exclude,
"protected": *conditions.RepositoryName.Protected,
"protected": protected,
})
conditionsMap["repository_name"] = repositoryNameSlice
}
Expand Down Expand Up @@ -351,16 +358,25 @@ func flattenRules(rules []*github.RepositoryRule, org bool) []interface{} {

case "commit_message_pattern", "commit_author_email_pattern", "committer_email_pattern", "branch_name_pattern", "tag_name_pattern":
var params github.RulePatternParameters
var name string
var negate bool

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

if params.Name != nil {
name = *params.Name
}
if params.Negate != nil {
negate = *params.Negate
}

rule := make(map[string]interface{})
rule["name"] = *params.Name
rule["negate"] = *params.Negate
rule["name"] = name
rule["negate"] = negate
rule["operator"] = params.Operator
rule["pattern"] = params.Pattern
rulesMap[v.Type] = []map[string]interface{}{rule}
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/organization_ruleset.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ One of `repository_id` and `repository_name` must be set for the rule to target

* `exclude` - (Required) (List of String) Array of repository names or patterns to exclude. The condition will not pass if any of these patterns match.

* `inlcude` - (Required) (List of String) 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.
* `include` - (Required) (List of String) 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.

## Attributes Reference

Expand Down

0 comments on commit 39a4511

Please sign in to comment.