Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include repositories Info associated to the Teams #791

Merged
merged 3 commits into from
Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
77 changes: 77 additions & 0 deletions github/data_source_github_team_repository_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func TestAccGithubTeamRepositorty(t *testing.T) {
func TestAccGithubTeamRepository(t *testing.T) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll change it to TestAccGithubTeamRepositories to avoid duplication with test resource_github_team_repository_test.go


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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)
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-team-%[1]s"
}
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 = github_team.team-test.slug
}
`, randomID)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran into some difficulty with getting tests to pass here. This suggests we remove the output blocks as they do not seem to be used and were leading to errors like:

testing.go:654: Step 0 error: Invalid attribute name: On /var/folders/t1/p9cyhv6s2wg4g8s_x6zkpz_m0000gn/T/tf-test790708991/main.tf line 26: An attribute name is required after a dot.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It´s a good point.
I´ll try to improve the test! Thks


check := resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.github_team.example", "name"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
resource.TestCheckResourceAttrSet("data.github_team.example", "name"),
resource.TestCheckResourceAttr("data.github_team.example", "repositories.#", "1"),

A more helpful attribute to check would be the new repositories.# field that this PR adds. When running the test with TF_LOG=DEBUG, I'm seeing expected state:

data.github_team.example:
  ID = 4838313
  provider = provider.github
  description = 
  name = tf-acc-test-team-zniiu
  node_id = MDQ6VGVhbTQ4MzgzMTM=
  permission = pull
  privacy = secret
  repositories.# = 1
  repositories.0 = tf-acc-repo-zniiu
  slug = tf-acc-test-team-zniiu

However, the test fails 😕 . We should find a way to check this attribute as a next step, but I've run out of time for today.

Copy link
Contributor Author

@jbenaventem jbenaventem May 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The final Change is to use "depends_on". Is a good point for You?

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

)

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

})

}
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