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

fix: Fixed org teams data lookup parent id #2507

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
41 changes: 35 additions & 6 deletions github/data_source_github_organization_teams.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package github

import (
"context"
"strconv"

"github.com/google/go-github/v66/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/shurcooL/githubv4"
Expand Down Expand Up @@ -67,9 +71,18 @@ func dataSourceGithubOrganizationTeams() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString},
},
"parent": {
Type: schema.TypeMap,
Deprecated: "Use parent_team_id and parent_team_slug instead.",
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"parent_team_id": {
Type: schema.TypeString,
Computed: true,
},
"parent_team_slug": {
Type: schema.TypeString,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
Expand All @@ -84,6 +97,7 @@ func dataSourceGithubOrganizationTeamsRead(d *schema.ResourceData, meta interfac
return err
}

clientv3 := meta.(*Owner).v3client
client := meta.(*Owner).v4client
orgName := meta.(*Owner).name
rootTeamsOnly := d.Get("root_teams_only").(bool)
Expand All @@ -107,7 +121,10 @@ func dataSourceGithubOrganizationTeamsRead(d *schema.ResourceData, meta interfac
return err
}

additionalTeams := flattenGitHubTeams(query)
additionalTeams, err := flattenGitHubTeams(clientv3, meta.(*Owner).StopContext, orgName, query)
if err != nil {
return err
}
teams = append(teams, additionalTeams...)

if !query.Organization.Teams.PageInfo.HasNextPage {
Expand All @@ -125,11 +142,11 @@ func dataSourceGithubOrganizationTeamsRead(d *schema.ResourceData, meta interfac
return nil
}

func flattenGitHubTeams(tq TeamsQuery) []interface{} {
func flattenGitHubTeams(client *github.Client, ctx context.Context, org string, tq TeamsQuery) ([]interface{}, error) {
teams := tq.Organization.Teams.Nodes

if len(teams) == 0 {
return make([]interface{}, 0)
return make([]interface{}, 0), nil
}

flatTeams := make([]interface{}, len(teams))
Expand All @@ -152,6 +169,18 @@ func flattenGitHubTeams(tq TeamsQuery) []interface{} {

t["members"] = flatMembers

var parentTeamId string
if len(team.Parent.Slug) != 0 {
parentTeam, _, err := client.Teams.GetTeamBySlug(ctx, org, string(team.Parent.Slug))
if err != nil {
return nil, err
}
parentTeamId = strconv.FormatInt(parentTeam.GetID(), 10)
}

t["parent_team_id"] = parentTeamId
t["parent_team_slug"] = team.Parent.Slug

parentTeam := make(map[string]interface{})
parentTeam["id"] = team.Parent.ID
parentTeam["slug"] = team.Parent.Slug
Expand All @@ -171,5 +200,5 @@ func flattenGitHubTeams(tq TeamsQuery) []interface{} {
flatTeams[i] = t
}

return flatTeams
return flatTeams, nil
}
16 changes: 9 additions & 7 deletions website/docs/d/organization_teams.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ ___

The `team` block consists of:

* `id` - the ID of the team.
* `node_id` - the Node ID of the team.
* `slug` - the slug of the team.
* `name` - the team's full name.
* `description` - the team's description.
* `privacy` - the team's privacy type.
* `id` - The ID of the team.
* `node_id` - The Node ID of the team.
* `slug` - The slug of the team.
* `name` - The team's full name.
* `description` - The team's description.
* `privacy` - The team's privacy type.
* `members` - List of team members. Not returned if `summary_only = true`
* `repositories` - List of team repositories. Not returned if `summary_only = true`
* `parent` - the parent team.
* `parent_team_id` - The ID of the parent team, if there is one.
* `parent_team_slug` - The slug of the parent team, if there is one.
* `parent` - (**DEPRECATED**) The parent team, use `parent_team_id` or `parent_team_slug` instead.
Loading