Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
10 changes: 9 additions & 1 deletion github/resource_github_team_members.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,21 @@ func resourceGithubTeamMembers() *schema.Resource {
Type: schema.TypeSet,
Required: true,
Description: "List of users that should be members of the team.",
Set: func(v any) int {
username, _ := v.(map[string]any)["username"].(string)
return schema.HashString(strings.ToLower(username))
},
Comment thread
deiga marked this conversation as resolved.
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"username": {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: caseInsensitive(),
Description: "User to add to the team.",
StateFunc: func(v any) string {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this, importing a mixed-case username will create a diff.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, because the code is missing a check to reconcile the config members with the actual members as lower case. So we don't need this, but we do need a fix.

I need to revert the GraphQL change as go-github now supports filtering the members so I can take a look at this using your regression test (I think I've handled this in the repository collaborators resource).

val, _ := v.(string)
return strings.ToLower(val)
},
Description: "User to add to the team.",
},
"role": {
Type: schema.TypeString,
Expand Down
72 changes: 60 additions & 12 deletions github/resource_github_team_members_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package github
import (
"fmt"
"strconv"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-testing/compare"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
Expand All @@ -18,6 +20,7 @@ func TestAccGithubTeamMembers(t *testing.T) {

skipUnlessHasOrgs(t)
skipUnlessHasOrgUser1(t)
flippedCaseUsername := flipUsernameCase(testAccConf.testOrgUser1)

t.Run("team_by_slug", func(t *testing.T) {
t.Parallel()
Expand All @@ -33,7 +36,7 @@ resource "github_team_members" "test" {
role = "maintainer"
}
}
`, team.GetSlug(), testAccConf.testOrgUser1)
`, team.GetSlug(), flippedCaseUsername)

resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Expand Down Expand Up @@ -71,7 +74,7 @@ resource "github_team_members" "test" {
role = "maintainer"
}
}
`, team.GetSlug(), testAccConf.testOrgUser1)
`, team.GetSlug(), flippedCaseUsername)

resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Expand Down Expand Up @@ -110,7 +113,7 @@ resource "github_team_members" "test" {
role = "maintainer"
}
}
`, team.GetID(), testAccConf.testOrgUser1)
`, team.GetID(), flippedCaseUsername)

resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Expand Down Expand Up @@ -148,7 +151,7 @@ resource "github_team_members" "test" {
role = "maintainer"
}
}
`, team.GetSlug(), testAccConf.testOrgUser1)
`, team.GetSlug(), flippedCaseUsername)

configMigrate := fmt.Sprintf(`
resource "github_team_members" "test" {
Expand All @@ -159,7 +162,7 @@ resource "github_team_members" "test" {
role = "maintainer"
}
}
`, team.GetSlug(), testAccConf.testOrgUser1)
`, team.GetSlug(), flippedCaseUsername)

resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Expand Down Expand Up @@ -201,7 +204,7 @@ resource "github_team_members" "test" {
role = "maintainer"
}
}
`, team.GetID(), testAccConf.testOrgUser1)
`, team.GetID(), flippedCaseUsername)

configMigrate := fmt.Sprintf(`
resource "github_team_members" "test" {
Expand All @@ -212,7 +215,7 @@ resource "github_team_members" "test" {
role = "maintainer"
}
}
`, team.GetSlug(), testAccConf.testOrgUser1)
`, team.GetSlug(), flippedCaseUsername)

resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Expand Down Expand Up @@ -257,7 +260,7 @@ resource "github_team_members" "test" {
role = "maintainer"
}
}
`, team.GetSlug(), testAccConf.testOrgUser1)
`, team.GetSlug(), flippedCaseUsername)

resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Expand Down Expand Up @@ -296,7 +299,7 @@ resource "github_team_members" "test" {
role = "maintainer"
}
}
`, team.GetSlug(), testAccConf.testOrgUser1)
`, team.GetSlug(), flippedCaseUsername)

resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Expand Down Expand Up @@ -326,7 +329,7 @@ resource "github_team_members" "test" {
role = "maintainer"
}
}
`, testAccConf.testOrgUser1)
`, flippedCaseUsername)

resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Expand Down Expand Up @@ -364,7 +367,7 @@ resource "github_team_members" "test" {
role = "maintainer"
}
}
`, team.GetSlug(), testAccConf.testOrgUser1)
`, team.GetSlug(), flippedCaseUsername)

resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Expand Down Expand Up @@ -403,7 +406,7 @@ resource "github_team_members" "test" {
role = "maintainer"
}
}
`, testAccConf.testOrgUser1)
`, flippedCaseUsername)

resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Expand All @@ -430,4 +433,49 @@ resource "github_team_members" "test" {
},
})
})

t.Run("is_case_insensitive", func(t *testing.T) {
t.Parallel()

team := mustCreateTestTeam(t, nil)

config := fmt.Sprintf(`
resource "github_team_members" "test" {
team_slug = "%s"

members {
username = "%%s"
role = "maintainer"
}
}
`, team.GetSlug())

usernameIsSameComparer := statecheck.CompareValue(compare.ValuesSame())
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessHasOrgs(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(config, flippedCaseUsername),
ConfigStateChecks: []statecheck.StateCheck{
usernameIsSameComparer.AddStateValue("github_team_members.test", tfjsonpath.New("members").AtSliceIndex(0).AtMapKey("username")),
statecheck.ExpectKnownValue("github_team_members.test", tfjsonpath.New("members"), knownvalue.SetSizeExact(1)),
statecheck.ExpectKnownValue("github_team_members.test", tfjsonpath.New("members").AtSliceIndex(0).AtMapKey("username"), knownvalue.StringExact(strings.ToLower(testAccConf.testOrgUser1))),
},
},
{
Config: fmt.Sprintf(config, testAccConf.testOrgUser1),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction("github_team_members.test", plancheck.ResourceActionNoop),
},
},
ConfigStateChecks: []statecheck.StateCheck{
usernameIsSameComparer.AddStateValue("github_team_members.test", tfjsonpath.New("members").AtSliceIndex(0).AtMapKey("username")),
statecheck.ExpectKnownValue("github_team_members.test", tfjsonpath.New("members"), knownvalue.SetSizeExact(1)),
},
},
},
})
})
}
Loading