Skip to content

Commit

Permalink
fix: Fixed org teams data lookup parent id
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Hipwell <[email protected]>
  • Loading branch information
stevehipwell committed Dec 6, 2024
1 parent 1c11053 commit c83135b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
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.

0 comments on commit c83135b

Please sign in to comment.