Skip to content

Commit 39a4511

Browse files
authored
[fix] Fixes for org and repo rulesets
2 parents 3323096 + e4ea9c4 commit 39a4511

4 files changed

+34
-18
lines changed

github/resource_github_organization_ruleset.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
4343
ValidateFunc: validation.StringInSlice([]string{"disabled", "active", "evaluate"}, false),
4444
Description: "Possible values for Enforcement are `disabled`, `active`, `evaluate`. Note: `evaluate` is currently only supported for owners of type `organization`.",
4545
},
46-
4746
"bypass_actors": {
4847
Type: schema.TypeList,
4948
Optional: true,
@@ -63,11 +62,12 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
6362
},
6463
"bypass_mode": {
6564
Type: schema.TypeString,
66-
Optional: true,
65+
Required: true,
6766
ValidateFunc: validation.StringInSlice([]string{"always", "pull_request"}, false),
6867
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`.",
6968
},
70-
}},
69+
},
70+
},
7171
},
7272
"node_id": {
7373
Type: schema.TypeString,
@@ -118,7 +118,7 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
118118
ConflictsWith: []string{"conditions.0.repository_id"},
119119
Elem: &schema.Resource{
120120
Schema: map[string]*schema.Schema{
121-
"inlcude": {
121+
"include": {
122122
Type: schema.TypeList,
123123
Required: true,
124124
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.",
@@ -137,6 +137,7 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
137137
"protected": {
138138
Type: schema.TypeBool,
139139
Optional: true,
140+
Default: false,
140141
Description: "Whether renaming of target repositories is prevented.",
141142
},
142143
},
@@ -145,7 +146,6 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
145146
"repository_id": {
146147
Type: schema.TypeList,
147148
Optional: true,
148-
MaxItems: 1,
149149
ConflictsWith: []string{"conditions.0.repository_name"},
150150
Description: "The repository IDs that the ruleset applies to. One of these IDs must match for the condition to pass.",
151151
Elem: &schema.Schema{

github/resource_github_repository_ruleset.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func resourceGithubRepositoryRuleset() *schema.Resource {
6767
},
6868
"bypass_mode": {
6969
Type: schema.TypeString,
70-
Optional: true,
70+
Required: true,
7171
ValidateFunc: validation.StringInSlice([]string{"always", "pull_request"}, false),
7272
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`.",
7373
},

github/respository_rules_utils.go

+27-11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package github
33
import (
44
"encoding/json"
55
"log"
6+
"sort"
67

78
"github.com/google/go-github/v55/github"
89
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
@@ -54,7 +55,6 @@ func expandBypassActors(input []interface{}) []*github.BypassActor {
5455
}
5556
bypassActors = append(bypassActors, actor)
5657
}
57-
5858
return bypassActors
5959
}
6060

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

66-
actorsSlice := make([]map[string]interface{}, 0)
66+
sort.SliceStable(bypassActors, func(i, j int) bool {
67+
return bypassActors[i].GetActorID() > bypassActors[j].GetActorID()
68+
})
69+
70+
actorsSlice := make([]interface{}, 0)
6771
for _, v := range bypassActors {
6872
actorMap := make(map[string]interface{})
6973

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

77-
return []interface{}{actorsSlice}
81+
return actorsSlice
7882
}
7983

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

111115
// org-only fields
112116
if org {
113-
// repository_name
117+
// repository_name and repository_id
114118
if v, ok := inputConditions["repository_name"].([]interface{}); ok && v != nil && len(v) != 0 {
115119
inputRepositoryName := v[0].(map[string]interface{})
116120
include := make([]string, 0)
@@ -135,10 +139,7 @@ func expandConditions(input []interface{}, org bool) *github.RulesetConditions {
135139
Exclude: exclude,
136140
Protected: &protected,
137141
}
138-
}
139-
140-
// repository_id
141-
if v, ok := inputConditions["repository_id"].([]interface{}); ok && v != nil {
142+
} else if v, ok := inputConditions["repository_id"].([]interface{}); ok && v != nil && len(v) != 0 {
142143
repositoryIDs := make([]int64, 0)
143144

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

176177
if conditions.RepositoryName != nil {
178+
var protected bool
179+
180+
if conditions.RepositoryName.Protected != nil {
181+
protected = *conditions.RepositoryName.Protected
182+
}
183+
177184
repositoryNameSlice = append(repositoryNameSlice, map[string]interface{}{
178185
"include": conditions.RepositoryName.Include,
179186
"exclude": conditions.RepositoryName.Exclude,
180-
"protected": *conditions.RepositoryName.Protected,
187+
"protected": protected,
181188
})
182189
conditionsMap["repository_name"] = repositoryNameSlice
183190
}
@@ -351,16 +358,25 @@ func flattenRules(rules []*github.RepositoryRule, org bool) []interface{} {
351358

352359
case "commit_message_pattern", "commit_author_email_pattern", "committer_email_pattern", "branch_name_pattern", "tag_name_pattern":
353360
var params github.RulePatternParameters
361+
var name string
362+
var negate bool
354363

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

370+
if params.Name != nil {
371+
name = *params.Name
372+
}
373+
if params.Negate != nil {
374+
negate = *params.Negate
375+
}
376+
361377
rule := make(map[string]interface{})
362-
rule["name"] = *params.Name
363-
rule["negate"] = *params.Negate
378+
rule["name"] = name
379+
rule["negate"] = negate
364380
rule["operator"] = params.Operator
365381
rule["pattern"] = params.Pattern
366382
rulesMap[v.Type] = []map[string]interface{}{rule}

website/docs/r/organization_ruleset.html.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ One of `repository_id` and `repository_name` must be set for the rule to target
203203

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

206-
* `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.
206+
* `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.
207207

208208
## Attributes Reference
209209

0 commit comments

Comments
 (0)