Skip to content

Commit add2157

Browse files
authored
[BUG] Fix drift with external collaborators invite (#3373)
* Update Read to combine Invites and Collaborator users This is to prevent drift when external users have not accepted invites yet. Signed-off-by: Timo Sand <[email protected]> * Improve DEBUG logging of repo collaborators Signed-off-by: Timo Sand <[email protected]> * Revert `ParallelTest` changes Signed-off-by: Timo Sand <[email protected]> --------- Signed-off-by: Timo Sand <[email protected]>
1 parent 7ddbf35 commit add2157

3 files changed

Lines changed: 28 additions & 10 deletions

File tree

github/resource_github_repository_collaborators.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ func resourceGithubRepositoryCollaborators() *schema.Resource {
117117
}
118118

119119
func resourceGithubRepositoryCollaboratorsDiff(ctx context.Context, d *schema.ResourceDiff, m any) error {
120+
tflog.Debug(ctx, "Diffing user collaborators")
120121
if d.HasChange("user") {
121122
users := d.Get("user").(*schema.Set).List()
122123
seen := make(map[string]any)
@@ -181,6 +182,11 @@ func resourceGithubRepositoryCollaboratorsCreate(ctx context.Context, d *schema.
181182
teams := d.Get("team").(*schema.Set).List()
182183
ignoreTeams := d.Get("ignore_team").(*schema.Set).List()
183184

185+
tflog.Debug(ctx, "Creating repository collaborators", map[string]any{
186+
"users": users,
187+
"teams": teams,
188+
"ignoreTeams": ignoreTeams,
189+
})
184190
inUsers, err := getUserCollaborators(users)
185191
if err != nil {
186192
return diag.FromErr(err)
@@ -226,6 +232,7 @@ func resourceGithubRepositoryCollaboratorsCreate(ctx context.Context, d *schema.
226232
}
227233

228234
func resourceGithubRepositoryCollaboratorsRead(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
235+
tflog.Debug(ctx, "Reading repository collaborators")
229236
meta, _ := m.(*Owner)
230237
client := meta.v3client
231238
owner := meta.name
@@ -255,10 +262,6 @@ func resourceGithubRepositoryCollaboratorsRead(ctx context.Context, d *schema.Re
255262
return diag.FromErr(err)
256263
}
257264

258-
if err := d.Set("user", ghUsers.flatten()); err != nil {
259-
return diag.FromErr(err)
260-
}
261-
262265
if isOrg {
263266
ghTeams, err := listTeamCollaborators(ctx, client, owner, repoName, inTeams, inIgnoreTeams)
264267
if err != nil {
@@ -280,6 +283,11 @@ func resourceGithubRepositoryCollaboratorsRead(ctx context.Context, d *schema.Re
280283
return diag.FromErr(err)
281284
}
282285

286+
combinedUsersAndInvitations := slices.Concat(ghUsers, ghInvitations)
287+
if err := d.Set("user", combinedUsersAndInvitations.flatten()); err != nil {
288+
return diag.FromErr(err)
289+
}
290+
283291
if err = d.Set("invitation_ids", ghInvitations.flattenInvitations()); err != nil {
284292
return diag.FromErr(err)
285293
}
@@ -288,6 +296,7 @@ func resourceGithubRepositoryCollaboratorsRead(ctx context.Context, d *schema.Re
288296
}
289297

290298
func resourceGithubRepositoryCollaboratorsUpdate(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
299+
tflog.Debug(ctx, "Updating repository collaborators")
291300
meta, _ := m.(*Owner)
292301
client := meta.v3client
293302
owner := meta.name
@@ -333,6 +342,7 @@ func resourceGithubRepositoryCollaboratorsUpdate(ctx context.Context, d *schema.
333342
}
334343

335344
func resourceGithubRepositoryCollaboratorsDelete(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
345+
tflog.Debug(ctx, "Deleting repository collaborators")
336346
meta, _ := m.(*Owner)
337347
client := meta.v3client
338348
owner := meta.name
@@ -364,6 +374,7 @@ func resourceGithubRepositoryCollaboratorsDelete(ctx context.Context, d *schema.
364374
}
365375

366376
func resourceGithubRepositoryCollaboratorsImport(ctx context.Context, d *schema.ResourceData, m any) ([]*schema.ResourceData, error) {
377+
tflog.Debug(ctx, "Importing repository collaborators")
367378
meta := m.(*Owner)
368379
client := meta.v3client
369380
owner := meta.name
@@ -496,7 +507,10 @@ func getTeamIdentity(d any) (teamIdentity, error) {
496507

497508
func listUserCollaborators(ctx context.Context, client *github.Client, owner, repoName string) (userCollaborators, error) {
498509
col := make([]userCollaborator, 0)
499-
510+
tflog.Debug(ctx, "Listing user collaborators", map[string]any{
511+
"owner": owner,
512+
"repoName": repoName,
513+
})
500514
affiliations := []string{"direct", "outside"}
501515
for _, affiliation := range affiliations {
502516
opt := &github.ListCollaboratorsOptions{
@@ -633,6 +647,14 @@ func updateUserCollaboratorsAndInvites(ctx context.Context, client *github.Clien
633647
lookup[inUser.login] = inUser
634648
}
635649

650+
tflog.Debug(ctx, "Updating user collaborators and invitations", map[string]any{
651+
"repoName": repoName,
652+
"inUsers": inUsers,
653+
"lookup": lookup,
654+
"seen": seen,
655+
"remove": remove,
656+
})
657+
636658
ghUsers, err := listUserCollaborators(ctx, client, owner, repoName)
637659
if err != nil {
638660
return nil, err

github/resource_github_repository_collaborators_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ resource "github_repository_collaborators" "test" {
125125
{
126126
Config: config,
127127
ConfigStateChecks: []statecheck.StateCheck{
128-
statecheck.ExpectKnownValue("github_repository_collaborators.test", tfjsonpath.New("user"), knownvalue.SetSizeExact(0)),
128+
statecheck.ExpectKnownValue("github_repository_collaborators.test", tfjsonpath.New("user"), knownvalue.SetSizeExact(1)),
129129
statecheck.ExpectKnownValue("github_repository_collaborators.test", tfjsonpath.New("team"), knownvalue.SetSizeExact(0)),
130130
statecheck.ExpectKnownValue("github_repository_collaborators.test", tfjsonpath.New("invitation_ids"), knownvalue.MapSizeExact(1)),
131131
},

github/util_user.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ func (u userCollaborator) flatten() any {
2121
"permission": u.permission,
2222
}
2323

24-
if u.invitationID != nil {
25-
m["invitation_id"] = *u.invitationID
26-
}
27-
2824
return m
2925
}
3026

0 commit comments

Comments
 (0)