From b43ee389359a900c87422d0434d260346b9dcd4a 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 1/3] Include repositories Info associated to the Teams --- .../data_source_github_organization_teams.go | 15 ++++ github/data_source_github_team.go | 26 ++++++- ...data_source_github_team_repository_test.go | 77 +++++++++++++++++++ github/util_v4_teams.go | 5 ++ 4 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 github/data_source_github_team_repository_test.go 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)"` From 775ac55a79e35fcb0dd36a8292795ce5a65e57e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Benavente?= Date: Wed, 2 Jun 2021 15:57:36 +0000 Subject: [PATCH 2/3] Improbe Test: depends_on --- ...data_source_github_team_repository_test.go | 48 ++++++++----------- website/docs/d/team.html.markdown | 2 + 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/github/data_source_github_team_repository_test.go b/github/data_source_github_team_repository_test.go index 68e31b9607..e0a47ac950 100644 --- a/github/data_source_github_team_repository_test.go +++ b/github/data_source_github_team_repository_test.go @@ -8,43 +8,36 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/helper/resource" ) -func TestAccGithubTeamRepositorty(t *testing.T) { +func TestAccGithubTeamRepositories(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" - } + resource "github_repository" "test" { + name = "tf-acc-test-%s" + auto_init = true + } - output "team_repository_name" { - value = data.github_team.example.repositories.0 - } + resource "github_team" "test" { + name = "tf-acc-test-%[1]s" + } - output "team_repository_numbers" { - value = data.github_team.example.repositories.# - } + resource "github_team_repository" "test" { + team_id = "${github_team.test.id}" + repository = "${github_repository.test.name}" + } + + data "github_team" "example" { + depends_on = ["github_repository.test", "github_team.test", "github_team_repository.test"] + slug = github_team.test.slug + } `, randomID) check := resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttrSet("data.github_team.example", "name"), + resource.TestCheckResourceAttr("data.github_team.example", "repositories.#", "1"), ) testCase := func(t *testing.T, mode string) { @@ -53,8 +46,9 @@ func TestAccGithubTeamRepositorty(t *testing.T) { Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: config, - Check: check, + Config: config, + Check: check, + ExpectNonEmptyPlan: true, }, }, }) diff --git a/website/docs/d/team.html.markdown b/website/docs/d/team.html.markdown index bfe2d1b1ff..a5f348de2c 100644 --- a/website/docs/d/team.html.markdown +++ b/website/docs/d/team.html.markdown @@ -30,3 +30,5 @@ data "github_team" "example" { * `privacy` - the team's privacy type. * `permission` - the team's permission level. * `members` - List of team members + * `repositories` - List of team repositories + From 0b2c4fda0f4364fc4b267d1196929c4832cd0eca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Benavente?= Date: Wed, 16 Jun 2021 18:01:19 +0000 Subject: [PATCH 3/3] update organization_teams markdown --- website/docs/d/organization_teams.html.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/website/docs/d/organization_teams.html.markdown b/website/docs/d/organization_teams.html.markdown index 266670c6d5..ad51be75c2 100644 --- a/website/docs/d/organization_teams.html.markdown +++ b/website/docs/d/organization_teams.html.markdown @@ -40,3 +40,4 @@ The `team` block consists of: * `description` - the team's description. * `privacy` - the team's privacy type. * `members` - List of team members. + * `repositories` - List of team repositories.