@@ -32,7 +32,7 @@ func resourceGithubRepositoryCollaborators() *schema.Resource {
32
32
"user" : {
33
33
Type : schema .TypeSet ,
34
34
Optional : true ,
35
- Description : "List of users" ,
35
+ Description : "List of users. " ,
36
36
Elem : & schema.Resource {
37
37
Schema : map [string ]* schema.Schema {
38
38
"permission" : {
@@ -52,7 +52,7 @@ func resourceGithubRepositoryCollaborators() *schema.Resource {
52
52
"team" : {
53
53
Type : schema .TypeSet ,
54
54
Optional : true ,
55
- Description : "List of teams" ,
55
+ Description : "List of teams. " ,
56
56
Elem : & schema.Resource {
57
57
Schema : map [string ]* schema.Schema {
58
58
"permission" : {
@@ -76,6 +76,20 @@ func resourceGithubRepositoryCollaborators() *schema.Resource {
76
76
},
77
77
Computed : true ,
78
78
},
79
+ "ignore_team" : {
80
+ Type : schema .TypeSet ,
81
+ Optional : true ,
82
+ Description : "List of teams to ignore." ,
83
+ Elem : & schema.Resource {
84
+ Schema : map [string ]* schema.Schema {
85
+ "team_id" : {
86
+ Type : schema .TypeString ,
87
+ Description : "ID or slug of the team to ignore." ,
88
+ Required : true ,
89
+ },
90
+ },
91
+ },
92
+ },
79
93
},
80
94
81
95
CustomizeDiff : customdiff .Sequence (
@@ -145,16 +159,16 @@ func (c teamCollaborator) Empty() bool {
145
159
return c == teamCollaborator {}
146
160
}
147
161
148
- func flattenTeamCollaborator (obj teamCollaborator , teamIDs []int64 ) interface {} {
162
+ func flattenTeamCollaborator (obj teamCollaborator , teamSlugs []string ) interface {} {
149
163
if obj .Empty () {
150
164
return nil
151
165
}
152
166
153
167
var teamIDString string
154
- if slices .Contains (teamIDs , obj .teamID ) {
155
- teamIDString = strconv .FormatInt (obj .teamID , 10 )
156
- } else {
168
+ if slices .Contains (teamSlugs , obj .teamSlug ) {
157
169
teamIDString = obj .teamSlug
170
+ } else {
171
+ teamIDString = strconv .FormatInt (obj .teamID , 10 )
158
172
}
159
173
160
174
transformed := map [string ]interface {}{
@@ -165,7 +179,7 @@ func flattenTeamCollaborator(obj teamCollaborator, teamIDs []int64) interface{}
165
179
return transformed
166
180
}
167
181
168
- func flattenTeamCollaborators (objs []teamCollaborator , teamIDs []int64 ) []interface {} {
182
+ func flattenTeamCollaborators (objs []teamCollaborator , teamSlugs []string ) []interface {} {
169
183
if objs == nil {
170
184
return nil
171
185
}
@@ -176,14 +190,14 @@ func flattenTeamCollaborators(objs []teamCollaborator, teamIDs []int64) []interf
176
190
177
191
items := make ([]interface {}, len (objs ))
178
192
for i , obj := range objs {
179
- items [i ] = flattenTeamCollaborator (obj , teamIDs )
193
+ items [i ] = flattenTeamCollaborator (obj , teamSlugs )
180
194
}
181
195
182
196
return items
183
197
}
184
198
185
199
func listUserCollaborators (client * github.Client , isOrg bool , ctx context.Context , owner , repoName string ) ([]userCollaborator , error ) {
186
- var userCollaborators []userCollaborator
200
+ userCollaborators := make ( []userCollaborator , 0 )
187
201
affiliations := []string {"direct" , "outside" }
188
202
for _ , affiliation := range affiliations {
189
203
opt := & github.ListCollaboratorsOptions {ListOptions : github.ListOptions {
@@ -217,7 +231,7 @@ func listUserCollaborators(client *github.Client, isOrg bool, ctx context.Contex
217
231
}
218
232
219
233
func listInvitations (client * github.Client , ctx context.Context , owner , repoName string ) ([]invitedCollaborator , error ) {
220
- var invitedCollaborators []invitedCollaborator
234
+ invitedCollaborators := make ( []invitedCollaborator , 0 )
221
235
222
236
opt := & github.ListOptions {PerPage : maxPerPage }
223
237
for {
@@ -230,7 +244,8 @@ func listInvitations(client *github.Client, ctx context.Context, owner, repoName
230
244
permissionName := getPermission (i .GetPermissions ())
231
245
232
246
invitedCollaborators = append (invitedCollaborators , invitedCollaborator {
233
- userCollaborator {permissionName , i .GetInvitee ().GetLogin ()}, i .GetID ()})
247
+ userCollaborator {permissionName , i .GetInvitee ().GetLogin ()}, i .GetID (),
248
+ })
234
249
}
235
250
236
251
if resp .NextPage == 0 {
@@ -241,11 +256,11 @@ func listInvitations(client *github.Client, ctx context.Context, owner, repoName
241
256
return invitedCollaborators , nil
242
257
}
243
258
244
- func listTeams (client * github.Client , isOrg bool , ctx context.Context , owner , repoName string ) ([]teamCollaborator , error ) {
245
- var teamCollaborators []teamCollaborator
259
+ func listTeams (client * github.Client , isOrg bool , ctx context.Context , owner , repoName string , ignoreTeamIds [] int64 ) ([]teamCollaborator , error ) {
260
+ allTeams := make ( []teamCollaborator , 0 )
246
261
247
262
if ! isOrg {
248
- return teamCollaborators , nil
263
+ return allTeams , nil
249
264
}
250
265
251
266
opt := & github.ListOptions {PerPage : maxPerPage }
@@ -256,20 +271,23 @@ func listTeams(client *github.Client, isOrg bool, ctx context.Context, owner, re
256
271
}
257
272
258
273
for _ , t := range repoTeams {
259
- permissionName := getPermission (t .GetPermission ())
274
+ if slices .Contains (ignoreTeamIds , t .GetID ()) {
275
+ continue
276
+ }
260
277
261
- teamCollaborators = append (teamCollaborators , teamCollaborator {permissionName , t .GetID (), t .GetSlug ()})
278
+ allTeams = append (allTeams , teamCollaborator {permission : getPermission ( t . GetPermission ()), teamID : t .GetID (), teamSlug : t .GetSlug ()})
262
279
}
263
280
264
281
if resp .NextPage == 0 {
265
282
break
266
283
}
267
284
opt .Page = resp .NextPage
268
285
}
269
- return teamCollaborators , nil
286
+
287
+ return allTeams , nil
270
288
}
271
289
272
- func listAllCollaborators (client * github.Client , isOrg bool , ctx context.Context , owner , repoName string ) ([]userCollaborator , []invitedCollaborator , []teamCollaborator , error ) {
290
+ func listAllCollaborators (client * github.Client , isOrg bool , ctx context.Context , owner , repoName string , ignoreTeamIds [] int64 ) ([]userCollaborator , []invitedCollaborator , []teamCollaborator , error ) {
273
291
userCollaborators , err := listUserCollaborators (client , isOrg , ctx , owner , repoName )
274
292
if err != nil {
275
293
return nil , nil , nil , err
@@ -278,16 +296,14 @@ func listAllCollaborators(client *github.Client, isOrg bool, ctx context.Context
278
296
if err != nil {
279
297
return nil , nil , nil , err
280
298
}
281
- teamCollaborators , err := listTeams (client , isOrg , ctx , owner , repoName )
299
+ teamCollaborators , err := listTeams (client , isOrg , ctx , owner , repoName , ignoreTeamIds )
282
300
if err != nil {
283
301
return nil , nil , nil , err
284
302
}
285
303
return userCollaborators , invitations , teamCollaborators , err
286
304
}
287
305
288
- func matchUserCollaboratorsAndInvites (
289
- repoName string , want []interface {}, hasUsers []userCollaborator , hasInvites []invitedCollaborator ,
290
- meta interface {}) error {
306
+ func matchUserCollaboratorsAndInvites (repoName string , want []interface {}, hasUsers []userCollaborator , hasInvites []invitedCollaborator , meta interface {}) error {
291
307
client := meta .(* Owner ).v3client
292
308
293
309
owner := meta .(* Owner ).name
@@ -383,8 +399,7 @@ func matchUserCollaboratorsAndInvites(
383
399
return nil
384
400
}
385
401
386
- func matchTeamCollaborators (
387
- repoName string , want []interface {}, has []teamCollaborator , meta interface {}) error {
402
+ func matchTeamCollaborators (repoName string , want []interface {}, has []teamCollaborator , meta interface {}) error {
388
403
client := meta .(* Owner ).v3client
389
404
orgID := meta .(* Owner ).id
390
405
owner := meta .(* Owner ).name
@@ -471,15 +486,15 @@ func resourceGithubRepositoryCollaboratorsCreate(d *schema.ResourceData, meta in
471
486
repoName := d .Get ("repository" ).(string )
472
487
ctx := context .Background ()
473
488
474
- teamsMap := make (map [string ]struct {})
489
+ teamsMap := make (map [string ]struct {}, len ( teams ) )
475
490
for _ , team := range teams {
476
491
teamIDString := team .(map [string ]interface {})["team_id" ].(string )
477
492
if _ , found := teamsMap [teamIDString ]; found {
478
493
return fmt .Errorf ("duplicate set member: %s" , teamIDString )
479
494
}
480
495
teamsMap [teamIDString ] = struct {}{}
481
496
}
482
- usersMap := make (map [string ]struct {})
497
+ usersMap := make (map [string ]struct {}, len ( users ) )
483
498
for _ , user := range users {
484
499
username := user .(map [string ]interface {})["username" ].(string )
485
500
if _ , found := usersMap [username ]; found {
@@ -488,7 +503,12 @@ func resourceGithubRepositoryCollaboratorsCreate(d *schema.ResourceData, meta in
488
503
usersMap [username ] = struct {}{}
489
504
}
490
505
491
- userCollaborators , invitations , teamCollaborators , err := listAllCollaborators (client , isOrg , ctx , owner , repoName )
506
+ ignoreTeamIds , err := getIgnoreTeamIds (d , meta )
507
+ if err != nil {
508
+ return err
509
+ }
510
+
511
+ userCollaborators , invitations , teamCollaborators , err := listAllCollaborators (client , isOrg , ctx , owner , repoName , ignoreTeamIds )
492
512
if err != nil {
493
513
return deleteResourceOn404AndSwallow304OtherwiseReturnError (err , d , "repository collaborators (%s/%s)" , owner , repoName )
494
514
}
@@ -516,7 +536,12 @@ func resourceGithubRepositoryCollaboratorsRead(d *schema.ResourceData, meta inte
516
536
repoName := d .Id ()
517
537
ctx := context .WithValue (context .Background (), ctxId , d .Id ())
518
538
519
- userCollaborators , invitedCollaborators , teamCollaborators , err := listAllCollaborators (client , isOrg , ctx , owner , repoName )
539
+ ignoreTeamIds , err := getIgnoreTeamIds (d , meta )
540
+ if err != nil {
541
+ return err
542
+ }
543
+
544
+ userCollaborators , invitedCollaborators , teamCollaborators , err := listAllCollaborators (client , isOrg , ctx , owner , repoName , ignoreTeamIds )
520
545
if err != nil {
521
546
return deleteResourceOn404AndSwallow304OtherwiseReturnError (err , d , "repository collaborators (%s/%s)" , owner , repoName )
522
547
}
@@ -526,9 +551,14 @@ func resourceGithubRepositoryCollaboratorsRead(d *schema.ResourceData, meta inte
526
551
invitationIds [i .username ] = strconv .FormatInt (i .invitationID , 10 )
527
552
}
528
553
529
- teamIDs := make ([]int64 , len (teamCollaborators ))
530
- for i , t := range teamCollaborators {
531
- teamIDs [i ] = t .teamID
554
+ sourceTeams := d .Get ("team" ).(* schema.Set ).List ()
555
+ teamSlugs := make ([]string , len (sourceTeams ))
556
+ for i , t := range sourceTeams {
557
+ teamIdString := t .(map [string ]interface {})["team_id" ].(string )
558
+ _ , parseIntErr := strconv .ParseInt (teamIdString , 10 , 64 )
559
+ if parseIntErr != nil {
560
+ teamSlugs [i ] = teamIdString
561
+ }
532
562
}
533
563
534
564
err = d .Set ("repository" , repoName )
@@ -539,7 +569,7 @@ func resourceGithubRepositoryCollaboratorsRead(d *schema.ResourceData, meta inte
539
569
if err != nil {
540
570
return err
541
571
}
542
- err = d .Set ("team" , flattenTeamCollaborators (teamCollaborators , teamIDs ))
572
+ err = d .Set ("team" , flattenTeamCollaborators (teamCollaborators , teamSlugs ))
543
573
if err != nil {
544
574
return err
545
575
}
@@ -563,7 +593,12 @@ func resourceGithubRepositoryCollaboratorsDelete(d *schema.ResourceData, meta in
563
593
repoName := d .Get ("repository" ).(string )
564
594
ctx := context .Background ()
565
595
566
- userCollaborators , invitations , teamCollaborators , err := listAllCollaborators (client , isOrg , ctx , owner , repoName )
596
+ ignoreTeamIds , err := getIgnoreTeamIds (d , meta )
597
+ if err != nil {
598
+ return err
599
+ }
600
+
601
+ userCollaborators , invitations , teamCollaborators , err := listAllCollaborators (client , isOrg , ctx , owner , repoName , ignoreTeamIds )
567
602
if err != nil {
568
603
return deleteResourceOn404AndSwallow304OtherwiseReturnError (err , d , "repository collaborators (%s/%s)" , owner , repoName )
569
604
}
@@ -580,3 +615,19 @@ func resourceGithubRepositoryCollaboratorsDelete(d *schema.ResourceData, meta in
580
615
err = matchTeamCollaborators (repoName , nil , teamCollaborators , meta )
581
616
return err
582
617
}
618
+
619
+ func getIgnoreTeamIds (d * schema.ResourceData , meta interface {}) ([]int64 , error ) {
620
+ ignoreTeams := d .Get ("ignore_team" ).(* schema.Set ).List ()
621
+ ignoreTeamIds := make ([]int64 , len (ignoreTeams ))
622
+
623
+ for i , t := range ignoreTeams {
624
+ s := t .(map [string ]interface {})["team_id" ].(string )
625
+ id , err := getTeamID (s , meta )
626
+ if err != nil {
627
+ return nil , err
628
+ }
629
+ ignoreTeamIds [i ] = id
630
+ }
631
+
632
+ return ignoreTeamIds , nil
633
+ }
0 commit comments