Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add membership_type to data.github_team #1242

Merged
merged 4 commits into from
Aug 26, 2022
Merged
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
71 changes: 59 additions & 12 deletions github/data_source_github_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (

"github.com/google/go-github/v45/github"

"github.com/shurcooL/githubv4"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)

func dataSourceGithubTeam() *schema.Resource {
Expand Down Expand Up @@ -48,6 +51,12 @@ func dataSourceGithubTeam() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"membership_type": {
Type: schema.TypeString,
Default: "all",
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"all", "immediate"}, false),
},
},
}
}
Expand All @@ -71,22 +80,60 @@ func dataSourceGithubTeamRead(d *schema.ResourceData, meta interface{}) error {
}

var members []string
for {
member, resp, err := client.Teams.ListTeamMembersByID(ctx, orgId, team.GetID(), &options)
if err != nil {
return err
if d.Get("membership_type").(string) == "all" {
for {
member, resp, err := client.Teams.ListTeamMembersByID(ctx, orgId, team.GetID(), &options)
if err != nil {
return err
}

for _, v := range member {
members = append(members, v.GetLogin())
}

if resp.NextPage == 0 {
break
}
options.Page = resp.NextPage
}

for _, v := range member {
members = append(members, v.GetLogin())
} else {
type member struct {
Login string
}

if resp.NextPage == 0 {
break
var query struct {
Organization struct {
Team struct {
Members struct {
Nodes []member
PageInfo struct {
EndCursor githubv4.String
HasNextPage bool
}
} `graphql:"members(first:100,after:$memberCursor,membership:IMMEDIATE)"`
} `graphql:"team(slug:$slug)"`
} `graphql:"organization(login:$owner)"`
}
variables := map[string]interface{}{
"owner": githubv4.String(meta.(*Owner).name),
"slug": githubv4.String(slug),
"memberCursor": (*githubv4.String)(nil),
}
client := meta.(*Owner).v4client
for {
nameErr := client.Query(ctx, &query, variables)
if nameErr != nil {
return nameErr
}
for _, v := range query.Organization.Team.Members.Nodes {
members = append(members, v.Login)
}
if query.Organization.Team.Members.PageInfo.HasNextPage {
variables["memberCursor"] = query.Organization.Team.Members.PageInfo.EndCursor
} else {
break
}
}
options.Page = resp.NextPage
}

var repositories []string
for {
repository, resp, err := client.Teams.ListTeamReposByID(ctx, orgId, team.GetID(), &options.ListOptions)
Expand Down
44 changes: 44 additions & 0 deletions github/data_source_github_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,50 @@ func TestAccGithubTeamDataSource(t *testing.T) {

})

t.Run("queries an existing team without error with immediate membership", func(t *testing.T) {

config := fmt.Sprintf(`
resource "github_team" "test" {
name = "tf-acc-test-%s"
}

data "github_team" "test" {
slug = github_team.test.slug
membership_type = "immediate"
}
`, randomID)

check := resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.github_team.test", "name"),
resource.TestCheckResourceAttr("data.github_team.test", "name", fmt.Sprintf("tf-acc-test-%s", randomID)),
)

testCase := func(t *testing.T, mode string) {
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessMode(t, mode) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: check,
},
},
})
}

t.Run("with an anonymous account", func(t *testing.T) {
t.Skip("anonymous account not supported for this operation")
})

t.Run("with an individual account", func(t *testing.T) {
t.Skip("individual account not supported for this operation")
})

t.Run("with an organization account", func(t *testing.T) {
testCase(t, organization)
})
})

t.Run("errors when querying a non-existing team", func(t *testing.T) {

config := `
Expand Down
7 changes: 4 additions & 3 deletions website/docs/d/team.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ data "github_team" "example" {
## Argument Reference

* `slug` - (Required) The team slug.
* `membership_type` - (Optional) Type of membershp to be requested to fill the list of members. Can be either "all" or "immediate". Default: "all"

## Attributes Reference

Expand All @@ -29,6 +30,6 @@ data "github_team" "example" {
* `description` - the team's description.
* `privacy` - the team's privacy type.
* `permission` - the team's permission level.
* `members` - List of team members
* `repositories` - List of team repositories
* `members` - List of team members (list of GitHub usernames)
* `repositories` - List of team repositories (list of repo names)