From 62e66e69bcad74801efb8c08145289e5b532e3c6 Mon Sep 17 00:00:00 2001 From: Steve Hipwell Date: Fri, 6 Dec 2024 12:27:32 +0000 Subject: [PATCH] fix: Fixed org teams data lookup parent id Signed-off-by: Steve Hipwell --- .../data_source_github_organization_teams.go | 41 ++++++++++++++++--- .../docs/d/organization_teams.html.markdown | 16 ++++---- 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/github/data_source_github_organization_teams.go b/github/data_source_github_organization_teams.go index 9754dbd040..c4c10890cc 100644 --- a/github/data_source_github_organization_teams.go +++ b/github/data_source_github_organization_teams.go @@ -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" @@ -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}, }, }, }, @@ -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) @@ -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 { @@ -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)) @@ -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 @@ -171,5 +200,5 @@ func flattenGitHubTeams(tq TeamsQuery) []interface{} { flatTeams[i] = t } - return flatTeams + return flatTeams, nil } diff --git a/website/docs/d/organization_teams.html.markdown b/website/docs/d/organization_teams.html.markdown index b9b6665ce8..f2476738ff 100644 --- a/website/docs/d/organization_teams.html.markdown +++ b/website/docs/d/organization_teams.html.markdown @@ -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.