diff --git a/github/resource_github_repository_collaborators.go b/github/resource_github_repository_collaborators.go index 51a389a852..44b86b2043 100644 --- a/github/resource_github_repository_collaborators.go +++ b/github/resource_github_repository_collaborators.go @@ -117,6 +117,7 @@ func resourceGithubRepositoryCollaborators() *schema.Resource { } func resourceGithubRepositoryCollaboratorsDiff(ctx context.Context, d *schema.ResourceDiff, m any) error { + tflog.Debug(ctx, "Diffing user collaborators") if d.HasChange("user") { users := d.Get("user").(*schema.Set).List() seen := make(map[string]any) @@ -181,6 +182,11 @@ func resourceGithubRepositoryCollaboratorsCreate(ctx context.Context, d *schema. teams := d.Get("team").(*schema.Set).List() ignoreTeams := d.Get("ignore_team").(*schema.Set).List() + tflog.Debug(ctx, "Creating repository collaborators", map[string]any{ + "users": users, + "teams": teams, + "ignoreTeams": ignoreTeams, + }) inUsers, err := getUserCollaborators(users) if err != nil { return diag.FromErr(err) @@ -226,6 +232,7 @@ func resourceGithubRepositoryCollaboratorsCreate(ctx context.Context, d *schema. } func resourceGithubRepositoryCollaboratorsRead(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics { + tflog.Debug(ctx, "Reading repository collaborators") meta, _ := m.(*Owner) client := meta.v3client owner := meta.name @@ -255,10 +262,6 @@ func resourceGithubRepositoryCollaboratorsRead(ctx context.Context, d *schema.Re return diag.FromErr(err) } - if err := d.Set("user", ghUsers.flatten()); err != nil { - return diag.FromErr(err) - } - if isOrg { ghTeams, err := listTeamCollaborators(ctx, client, owner, repoName, inTeams, inIgnoreTeams) if err != nil { @@ -280,6 +283,11 @@ func resourceGithubRepositoryCollaboratorsRead(ctx context.Context, d *schema.Re return diag.FromErr(err) } + combinedUsersAndInvitations := slices.Concat(ghUsers, ghInvitations) + if err := d.Set("user", combinedUsersAndInvitations.flatten()); err != nil { + return diag.FromErr(err) + } + if err = d.Set("invitation_ids", ghInvitations.flattenInvitations()); err != nil { return diag.FromErr(err) } @@ -288,6 +296,7 @@ func resourceGithubRepositoryCollaboratorsRead(ctx context.Context, d *schema.Re } func resourceGithubRepositoryCollaboratorsUpdate(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics { + tflog.Debug(ctx, "Updating repository collaborators") meta, _ := m.(*Owner) client := meta.v3client owner := meta.name @@ -333,6 +342,7 @@ func resourceGithubRepositoryCollaboratorsUpdate(ctx context.Context, d *schema. } func resourceGithubRepositoryCollaboratorsDelete(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics { + tflog.Debug(ctx, "Deleting repository collaborators") meta, _ := m.(*Owner) client := meta.v3client owner := meta.name @@ -364,6 +374,7 @@ func resourceGithubRepositoryCollaboratorsDelete(ctx context.Context, d *schema. } func resourceGithubRepositoryCollaboratorsImport(ctx context.Context, d *schema.ResourceData, m any) ([]*schema.ResourceData, error) { + tflog.Debug(ctx, "Importing repository collaborators") meta := m.(*Owner) client := meta.v3client owner := meta.name @@ -496,7 +507,10 @@ func getTeamIdentity(d any) (teamIdentity, error) { func listUserCollaborators(ctx context.Context, client *github.Client, owner, repoName string) (userCollaborators, error) { col := make([]userCollaborator, 0) - + tflog.Debug(ctx, "Listing user collaborators", map[string]any{ + "owner": owner, + "repoName": repoName, + }) affiliations := []string{"direct", "outside"} for _, affiliation := range affiliations { opt := &github.ListCollaboratorsOptions{ @@ -633,6 +647,14 @@ func updateUserCollaboratorsAndInvites(ctx context.Context, client *github.Clien lookup[inUser.login] = inUser } + tflog.Debug(ctx, "Updating user collaborators and invitations", map[string]any{ + "repoName": repoName, + "inUsers": inUsers, + "lookup": lookup, + "seen": seen, + "remove": remove, + }) + ghUsers, err := listUserCollaborators(ctx, client, owner, repoName) if err != nil { return nil, err diff --git a/github/resource_github_repository_collaborators_test.go b/github/resource_github_repository_collaborators_test.go index 62eb917734..c49d5a06b1 100644 --- a/github/resource_github_repository_collaborators_test.go +++ b/github/resource_github_repository_collaborators_test.go @@ -125,7 +125,7 @@ resource "github_repository_collaborators" "test" { { Config: config, ConfigStateChecks: []statecheck.StateCheck{ - statecheck.ExpectKnownValue("github_repository_collaborators.test", tfjsonpath.New("user"), knownvalue.SetSizeExact(0)), + statecheck.ExpectKnownValue("github_repository_collaborators.test", tfjsonpath.New("user"), knownvalue.SetSizeExact(1)), statecheck.ExpectKnownValue("github_repository_collaborators.test", tfjsonpath.New("team"), knownvalue.SetSizeExact(0)), statecheck.ExpectKnownValue("github_repository_collaborators.test", tfjsonpath.New("invitation_ids"), knownvalue.MapSizeExact(1)), }, diff --git a/github/util_user.go b/github/util_user.go index 5f4b5aeddc..132bd887fe 100644 --- a/github/util_user.go +++ b/github/util_user.go @@ -21,10 +21,6 @@ func (u userCollaborator) flatten() any { "permission": u.permission, } - if u.invitationID != nil { - m["invitation_id"] = *u.invitationID - } - return m }