Skip to content

Commit

Permalink
Include repositories Info associated to the Teams (integrations#791)
Browse files Browse the repository at this point in the history
* Include repositories Info associated to the Teams

* Improbe Test: depends_on

* update organization_teams markdown
  • Loading branch information
jbenaventem authored Jun 18, 2021
1 parent 4bb9ffc commit e82a275
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 1 deletion.
15 changes: 15 additions & 0 deletions github/data_source_github_organization_teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
},
},
},
},
Expand Down Expand Up @@ -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
}

Expand Down
26 changes: 25 additions & 1 deletion github/data_source_github_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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())
Expand Down
71 changes: 71 additions & 0 deletions github/data_source_github_team_repository_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package github

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

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" "test" {
name = "tf-acc-test-%s"
auto_init = true
}
resource "github_team" "test" {
name = "tf-acc-test-%[1]s"
}
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.TestCheckResourceAttr("data.github_team.example", "repositories.#", "1"),
)

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,
ExpectNonEmptyPlan: true,
},
},
})
}

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)
})

})

}
5 changes: 5 additions & 0 deletions github/util_v4_teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)"`
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/organization_teams.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 2 additions & 0 deletions website/docs/d/team.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e82a275

Please sign in to comment.