Skip to content

Commit

Permalink
Fix ruleset bypass actors diff issues (#1950)
Browse files Browse the repository at this point in the history
Co-authored-by: Keegan Campbell <[email protected]>
  • Loading branch information
o-sama and kfcampbell authored Jan 19, 2024
1 parent d975c70 commit 819abbf
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
7 changes: 4 additions & 3 deletions github/resource_github_organization_ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
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,
Description: "The actors that can bypass the rules in this ruleset.",
Type: schema.TypeList,
Optional: true,
DiffSuppressFunc: bypassActorsDiffSuppressFunc,
Description: "The actors that can bypass the rules in this ruleset.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"actor_id": {
Expand Down
7 changes: 4 additions & 3 deletions github/resource_github_repository_ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ func resourceGithubRepositoryRuleset() *schema.Resource {
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,
Description: "The actors that can bypass the rules in this ruleset.",
Type: schema.TypeList,
Optional: true,
DiffSuppressFunc: bypassActorsDiffSuppressFunc,
Description: "The actors that can bypass the rules in this ruleset.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"actor_id": {
Expand Down
26 changes: 22 additions & 4 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"
"reflect"
"sort"

"github.com/google/go-github/v57/github"
Expand Down Expand Up @@ -63,10 +64,6 @@ func flattenBypassActors(bypassActors []*github.BypassActor) []interface{} {
return []interface{}{}
}

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 Down Expand Up @@ -480,3 +477,24 @@ func flattenRules(rules []*github.RepositoryRule, org bool) []interface{} {

return []interface{}{rulesMap}
}

func bypassActorsDiffSuppressFunc(k, old, new string, d *schema.ResourceData) bool {
// If the length has changed, no need to suppress
if k == "bypass_actors.#" {
return old == new
}

// Get change to bypass actors
o, n := d.GetChange("bypass_actors")
oldBypassActors := o.([]interface{})
newBypassActors := n.([]interface{})

sort.SliceStable(oldBypassActors, func(i, j int) bool {
return oldBypassActors[i].(map[string]interface{})["actor_id"].(int) > oldBypassActors[j].(map[string]interface{})["actor_id"].(int)
})
sort.SliceStable(newBypassActors, func(i, j int) bool {
return newBypassActors[i].(map[string]interface{})["actor_id"].(int) > newBypassActors[j].(map[string]interface{})["actor_id"].(int)
})

return reflect.DeepEqual(oldBypassActors, newBypassActors)
}

0 comments on commit 819abbf

Please sign in to comment.