From 819abbff4c22f4b5ef0801e0f5bae780bccc72ac Mon Sep 17 00:00:00 2001 From: Osama Faqhruldin Date: Fri, 19 Jan 2024 12:44:11 -0500 Subject: [PATCH] Fix ruleset bypass actors diff issues (#1950) Co-authored-by: Keegan Campbell --- .../resource_github_organization_ruleset.go | 7 ++--- github/resource_github_repository_ruleset.go | 7 ++--- github/respository_rules_utils.go | 26 ++++++++++++++++--- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/github/resource_github_organization_ruleset.go b/github/resource_github_organization_ruleset.go index 2ef7b07f33..a8bfe5def1 100644 --- a/github/resource_github_organization_ruleset.go +++ b/github/resource_github_organization_ruleset.go @@ -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": { diff --git a/github/resource_github_repository_ruleset.go b/github/resource_github_repository_ruleset.go index f4b9a5313c..06a933a8c5 100644 --- a/github/resource_github_repository_ruleset.go +++ b/github/resource_github_repository_ruleset.go @@ -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": { diff --git a/github/respository_rules_utils.go b/github/respository_rules_utils.go index 1b77a5e541..6eeb2fbc6d 100644 --- a/github/respository_rules_utils.go +++ b/github/respository_rules_utils.go @@ -3,6 +3,7 @@ package github import ( "encoding/json" "log" + "reflect" "sort" "github.com/google/go-github/v57/github" @@ -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{}) @@ -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) +}