From 247d8267490981a68eea9df7433d25638440d70b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Benavente?= Date: Tue, 18 May 2021 08:25:34 +0000 Subject: [PATCH] Include repositories Info associated to the Teams --- .../data_source_github_organization_teams.go | 15 +++++++++++ github/data_source_github_team.go | 26 ++++++++++++++++++- github/util_v4_teams.go | 5 ++++ 3 files changed, 45 insertions(+), 1 deletion(-) 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/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)"`