@@ -464,36 +464,67 @@ func setPushActorIDs(actors []PushActorTypes, data BranchProtectionResourceData,
464
464
pushActors := make ([]string , 0 , len (actors ))
465
465
orgName := meta .(* Owner ).name
466
466
467
- idMap := make (map [string ]bool )
468
- for _ , v := range data .PushActorIDs {
469
- idMap [v ] = true
467
+ // Create a map to track seen IDs to prevent duplicates
468
+ seenIDs := make (map [string ]struct {})
469
+
470
+ for _ , a := range actors {
471
+ var id string
472
+ if a .Actor .Team .ID != nil {
473
+ id = a .Actor .Team .ID .(string )
474
+ } else if a .Actor .User .ID != nil {
475
+ id = a .Actor .User .ID .(string )
476
+ } else if a .Actor .App .ID != nil {
477
+ id = a .Actor .App .ID .(string )
478
+ }
479
+
480
+ if id != "" {
481
+ if _ , exists := seenIDs [id ]; ! exists {
482
+ pushActors = append (pushActors , id )
483
+ seenIDs [id ] = struct {}{}
484
+ }
485
+ }
470
486
}
471
487
472
488
for _ , a := range actors {
473
- // Check for raw IDs first
474
- if a .Actor .Team .ID != nil && idMap [a .Actor .Team .ID .(string )] {
475
- pushActors = append (pushActors , a .Actor .Team .ID .(string ))
476
- } else if a .Actor .User .ID != nil && idMap [a .Actor .User .ID .(string )] {
477
- pushActors = append (pushActors , a .Actor .User .ID .(string ))
478
- } else if a .Actor .App .ID != nil && idMap [a .Actor .App .ID .(string )] {
479
- pushActors = append (pushActors , a .Actor .App .ID .(string ))
480
- } else {
481
- // Fall back to formatted strings only if no ID match
489
+ if a .Actor .Team .ID == nil && a .Actor .User .ID == nil && a .Actor .App .ID == nil {
490
+ var formattedID string
482
491
if a .Actor .Team .Slug != "" {
483
- pushActors = append ( pushActors , orgName + "/" + string (a .Actor .Team .Slug ) )
492
+ formattedID = orgName + "/" + string (a .Actor .Team .Slug )
484
493
} else if a .Actor .User .Login != "" {
485
- pushActors = append ( pushActors , "/" + string (a .Actor .User .Login ) )
494
+ formattedID = "/" + string (a .Actor .User .Login )
486
495
} else if a .Actor .App != (Actor {}) {
487
- pushActors = append (pushActors , a .Actor .App .ID .(string ))
496
+ continue
497
+ }
498
+
499
+ if formattedID != "" {
500
+ if _ , exists := seenIDs [formattedID ]; ! exists {
501
+ pushActors = append (pushActors , formattedID )
502
+ seenIDs [formattedID ] = struct {}{}
503
+ }
488
504
}
489
505
}
490
506
}
491
507
492
508
// Sort for consistent ordering
493
- // This is important for preventing unnecessary drift in the Terraform state
494
509
sort .Strings (pushActors )
495
- log .Printf ("[DEBUG] Final sorted pushActors: %v" , pushActors )
496
- return pushActors
510
+
511
+ // Validate against provided IDs
512
+ idMap := make (map [string ]bool )
513
+ for _ , v := range data .PushActorIDs {
514
+ idMap [v ] = true
515
+ }
516
+
517
+ // Only keep IDs that were in the original PushActorIDs
518
+ validPushActors := make ([]string , 0 , len (pushActors ))
519
+ for _ , actor := range pushActors {
520
+ if idMap [actor ] {
521
+ validPushActors = append (validPushActors , actor )
522
+ }
523
+ }
524
+
525
+ sort .Strings (validPushActors )
526
+ log .Printf ("[DEBUG] Final sorted and validated pushActors: %v" , validPushActors )
527
+ return validPushActors
497
528
}
498
529
499
530
func setApprovingReviews (protection BranchProtectionRule , data BranchProtectionResourceData , meta interface {}) interface {} {
@@ -545,13 +576,18 @@ func setPushes(protection BranchProtectionRule, data BranchProtectionResourceDat
545
576
pushAllowances := protection .PushAllowances .Nodes
546
577
pushActors := setPushActorIDs (pushAllowances , data , meta )
547
578
579
+ // If we have no push actors but restrictions are enabled, return an empty list
580
+ // rather than nil to prevent drift
581
+ if len (pushActors ) == 0 && protection .RestrictsPushes {
582
+ pushActors = make ([]string , 0 )
583
+ }
584
+
548
585
restrictsPushes := []interface {}{
549
586
map [string ]interface {}{
550
587
PROTECTION_BLOCKS_CREATIONS : protection .BlocksCreations ,
551
588
PROTECTION_PUSH_ALLOWANCES : pushActors ,
552
589
},
553
590
}
554
-
555
591
return restrictsPushes
556
592
}
557
593
@@ -618,15 +654,34 @@ func getBranchProtectionID(repoID githubv4.ID, pattern string, meta interface{})
618
654
619
655
func getActorIds (data []string , meta interface {}) ([]string , error ) {
620
656
var actors []string
657
+ log .Printf ("[DEBUG] getActorIds input data: %v" , data )
658
+
659
+ // Create a map to track processed IDs and prevent duplicates
660
+ seen := make (map [string ]bool )
661
+
621
662
for _ , v := range data {
663
+ if v == "" {
664
+ continue
665
+ }
666
+
622
667
id , err := getNodeIDv4 (v , meta )
623
668
if err != nil {
669
+ log .Printf ("[DEBUG] Error getting node ID for %s: %v" , v , err )
624
670
return []string {}, err
625
671
}
626
- log .Printf ("[DEBUG] Retrieved node ID for user/team : %s - node ID : %s" , v , id )
627
- actors = append (actors , id )
672
+
673
+ log .Printf ("[DEBUG] Retrieved node ID for user/team: %s - node ID: %s" , v , id )
674
+
675
+ if ! seen [id ] {
676
+ actors = append (actors , id )
677
+ seen [id ] = true
678
+ } else {
679
+ log .Printf ("[DEBUG] Skipping duplicate ID: %s" , id )
680
+ }
628
681
}
629
682
683
+ sort .Strings (actors )
684
+ log .Printf ("[DEBUG] Final sorted actor IDs: %v" , actors )
630
685
return actors , nil
631
686
}
632
687
0 commit comments