Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions github/resource_github_repository_collaborators.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions github/resource_github_repository_collaborators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ resource "github_repository_collaborators" "test" {
}
`, repoName, teamName0, teamName1, collaboratorUser)

resource.Test(t, resource.TestCase{
resource.ParallelTest(t, resource.TestCase{
Comment thread
deiga marked this conversation as resolved.
Outdated
PreCheck: func() { skipUnlessHasOrgs(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
Expand Down Expand Up @@ -118,14 +118,14 @@ resource "github_repository_collaborators" "test" {
}
`, repoName, testAccConf.testExternalUser)

resource.Test(t, resource.TestCase{
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { skipUnauthenticated(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
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)),
},
Expand Down Expand Up @@ -196,7 +196,7 @@ resource "github_repository_collaborators" "test" {
}
`, configPre)

resource.Test(t, resource.TestCase{
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { skipUnlessHasOrgs(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
Expand Down Expand Up @@ -270,7 +270,7 @@ resource "github_repository_collaborators" "test" {
}
`, configPre)

resource.Test(t, resource.TestCase{
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { skipUnauthenticated(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
Expand Down Expand Up @@ -336,7 +336,7 @@ resource "github_repository_collaborators" "test" {
}
`, configPre)

resource.Test(t, resource.TestCase{
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { skipUnlessHasOrgs(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
Expand Down Expand Up @@ -397,7 +397,7 @@ resource "github_repository_collaborators" "test" {
}
`, configPre)

resource.Test(t, resource.TestCase{
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { skipUnlessHasOrgs(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
Expand Down Expand Up @@ -451,7 +451,7 @@ resource "github_repository_collaborators" "test" {
}
`, repoName, teamName)

resource.Test(t, resource.TestCase{
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { skipUnlessHasOrgs(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
Expand Down
4 changes: 0 additions & 4 deletions github/util_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ func (u userCollaborator) flatten() any {
"permission": u.permission,
}

if u.invitationID != nil {
m["invitation_id"] = *u.invitationID
}
Comment thread
stevehipwell marked this conversation as resolved.

return m
}

Expand Down