Skip to content

Commit d2b2857

Browse files
committed
feat: update get actors function
1 parent 9b3d96c commit d2b2857

File tree

1 file changed

+76
-21
lines changed

1 file changed

+76
-21
lines changed

github/util_v4_branch_protection.go

Lines changed: 76 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -464,36 +464,67 @@ func setPushActorIDs(actors []PushActorTypes, data BranchProtectionResourceData,
464464
pushActors := make([]string, 0, len(actors))
465465
orgName := meta.(*Owner).name
466466

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+
}
470486
}
471487

472488
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
482491
if a.Actor.Team.Slug != "" {
483-
pushActors = append(pushActors, orgName+"/"+string(a.Actor.Team.Slug))
492+
formattedID = orgName + "/" + string(a.Actor.Team.Slug)
484493
} else if a.Actor.User.Login != "" {
485-
pushActors = append(pushActors, "/"+string(a.Actor.User.Login))
494+
formattedID = "/" + string(a.Actor.User.Login)
486495
} 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+
}
488504
}
489505
}
490506
}
491507

492508
// Sort for consistent ordering
493-
// This is important for preventing unnecessary drift in the Terraform state
494509
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
497528
}
498529

499530
func setApprovingReviews(protection BranchProtectionRule, data BranchProtectionResourceData, meta interface{}) interface{} {
@@ -545,13 +576,18 @@ func setPushes(protection BranchProtectionRule, data BranchProtectionResourceDat
545576
pushAllowances := protection.PushAllowances.Nodes
546577
pushActors := setPushActorIDs(pushAllowances, data, meta)
547578

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+
548585
restrictsPushes := []interface{}{
549586
map[string]interface{}{
550587
PROTECTION_BLOCKS_CREATIONS: protection.BlocksCreations,
551588
PROTECTION_PUSH_ALLOWANCES: pushActors,
552589
},
553590
}
554-
555591
return restrictsPushes
556592
}
557593

@@ -618,15 +654,34 @@ func getBranchProtectionID(repoID githubv4.ID, pattern string, meta interface{})
618654

619655
func getActorIds(data []string, meta interface{}) ([]string, error) {
620656
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+
621662
for _, v := range data {
663+
if v == "" {
664+
continue
665+
}
666+
622667
id, err := getNodeIDv4(v, meta)
623668
if err != nil {
669+
log.Printf("[DEBUG] Error getting node ID for %s: %v", v, err)
624670
return []string{}, err
625671
}
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+
}
628681
}
629682

683+
sort.Strings(actors)
684+
log.Printf("[DEBUG] Final sorted actor IDs: %v", actors)
630685
return actors, nil
631686
}
632687

0 commit comments

Comments
 (0)