Skip to content

Commit

Permalink
Add membership_type to data.github_team (integrations#1242)
Browse files Browse the repository at this point in the history
* Add membership_type to data.github_team

* Improve doc

* Add extra integration test check

Co-authored-by: Keegan Campbell <me@kfcampbell.com>
  • Loading branch information
bpaquet and kfcampbell committed Aug 26, 2022
1 parent 45d2dc5 commit afb93fa
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 15 deletions.
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)

0 comments on commit afb93fa

Please sign in to comment.