diff --git a/github/data_source_github_organization_teams.go b/github/data_source_github_organization_teams.go index 2bbac579ee..1cca0feb3c 100644 --- a/github/data_source_github_organization_teams.go +++ b/github/data_source_github_organization_teams.go @@ -50,6 +50,11 @@ func dataSourceGithubOrganizationTeams() *schema.Resource { Computed: true, Elem: &schema.Schema{Type: schema.TypeString}, }, + "repositories": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, }, }, }, @@ -127,6 +132,16 @@ func flattenGitHubTeams(tq TeamsQuery) []interface{} { t["members"] = flatMembers + repositories := team.Repositories.Nodes + + flatRepositories := make([]string, len(repositories)) + + for i, repository := range repositories { + flatRepositories[i] = string(repository.Name) + } + + t["repositories"] = flatRepositories + flatTeams[i] = t } diff --git a/github/data_source_github_team.go b/github/data_source_github_team.go index 22878e0f10..c89d591c76 100644 --- a/github/data_source_github_team.go +++ b/github/data_source_github_team.go @@ -2,10 +2,11 @@ package github import ( "context" - "github.com/google/go-github/v35/github" "log" "strconv" + "github.com/google/go-github/v35/github" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" ) @@ -39,6 +40,11 @@ func dataSourceGithubTeam() *schema.Resource { Computed: true, Elem: &schema.Schema{Type: schema.TypeString}, }, + "repositories": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, "node_id": { Type: schema.TypeString, Computed: true, @@ -83,9 +89,27 @@ func dataSourceGithubTeamRead(d *schema.ResourceData, meta interface{}) error { options.Page = resp.NextPage } + var repositories []string + for { + repository, resp, err := client.Teams.ListTeamReposByID(ctx, orgId, team.GetID(), &options.ListOptions) + if err != nil { + return err + } + + for _, v := range repository { + repositories = append(repositories, v.GetName()) + } + + if resp.NextPage == 0 { + break + } + options.Page = resp.NextPage + } + d.SetId(strconv.FormatInt(team.GetID(), 10)) d.Set("name", team.GetName()) d.Set("members", members) + d.Set("repositories", repositories) d.Set("description", team.GetDescription()) d.Set("privacy", team.GetPrivacy()) d.Set("permission", team.GetPermission()) diff --git a/github/data_source_github_team_repository_test.go b/github/data_source_github_team_repository_test.go new file mode 100644 index 0000000000..68e31b9607 --- /dev/null +++ b/github/data_source_github_team_repository_test.go @@ -0,0 +1,77 @@ +package github + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" +) + +func TestAccGithubTeamRepositorty(t *testing.T) { + + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + + t.Run("Get Repositories By Teams", func(t *testing.T) { + + config := fmt.Sprintf(` + resource "github_repository" "repo-test" { + name = "tf-acc-repo-%s" + auto_init = true + + } + + resource "github_team" "team-test" { + name = "tf-acc-test-team01" + } + + resource "github_team_repository" "team-repo-test" { + repository = "${github_repository.repo-test.id}" + team_id = "${github_team.team-test.id}" + } + + data "github_team" "example" { + slug = "team-test-01" + } + + output "team_repository_name" { + value = data.github_team.example.repositories.0 + } + + output "team_repository_numbers" { + value = data.github_team.example.repositories.# + } + `, randomID) + + check := resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttrSet("data.github_team.example", "name"), + ) + + 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) + }) + + }) + +} diff --git a/github/util_v4_teams.go b/github/util_v4_teams.go index dba6f98829..87c20c86c2 100644 --- a/github/util_v4_teams.go +++ b/github/util_v4_teams.go @@ -18,6 +18,11 @@ type TeamsQuery struct { Login githubv4.String } } + Repositories struct { + Nodes []struct { + Name githubv4.String + } + } } PageInfo PageInfo } `graphql:"teams(first:$first, after:$cursor, rootTeamsOnly:$rootTeamsOnly)"`