Skip to content

Commit 62e66e6

Browse files
committed
fix: Fixed org teams data lookup parent id
Signed-off-by: Steve Hipwell <[email protected]>
1 parent 4b77944 commit 62e66e6

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

github/data_source_github_organization_teams.go

+35-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package github
22

33
import (
4+
"context"
5+
"strconv"
6+
7+
"github.com/google/go-github/v66/github"
48
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
59
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
610
"github.com/shurcooL/githubv4"
@@ -67,9 +71,18 @@ func dataSourceGithubOrganizationTeams() *schema.Resource {
6771
Elem: &schema.Schema{Type: schema.TypeString},
6872
},
6973
"parent": {
70-
Type: schema.TypeMap,
74+
Deprecated: "Use parent_team_id and parent_team_slug instead.",
75+
Type: schema.TypeMap,
76+
Computed: true,
77+
Elem: &schema.Schema{Type: schema.TypeString},
78+
},
79+
"parent_team_id": {
80+
Type: schema.TypeString,
81+
Computed: true,
82+
},
83+
"parent_team_slug": {
84+
Type: schema.TypeString,
7185
Computed: true,
72-
Elem: &schema.Schema{Type: schema.TypeString},
7386
},
7487
},
7588
},
@@ -84,6 +97,7 @@ func dataSourceGithubOrganizationTeamsRead(d *schema.ResourceData, meta interfac
8497
return err
8598
}
8699

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

110-
additionalTeams := flattenGitHubTeams(query)
124+
additionalTeams, err := flattenGitHubTeams(clientv3, meta.(*Owner).StopContext, orgName, query)
125+
if err != nil {
126+
return err
127+
}
111128
teams = append(teams, additionalTeams...)
112129

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

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

131148
if len(teams) == 0 {
132-
return make([]interface{}, 0)
149+
return make([]interface{}, 0), nil
133150
}
134151

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

153170
t["members"] = flatMembers
154171

172+
var parentTeamId string
173+
if len(team.Parent.Slug) != 0 {
174+
parentTeam, _, err := client.Teams.GetTeamBySlug(ctx, org, string(team.Parent.Slug))
175+
if err != nil {
176+
return nil, err
177+
}
178+
parentTeamId = strconv.FormatInt(parentTeam.GetID(), 10)
179+
}
180+
181+
t["parent_team_id"] = parentTeamId
182+
t["parent_team_slug"] = team.Parent.Slug
183+
155184
parentTeam := make(map[string]interface{})
156185
parentTeam["id"] = team.Parent.ID
157186
parentTeam["slug"] = team.Parent.Slug
@@ -171,5 +200,5 @@ func flattenGitHubTeams(tq TeamsQuery) []interface{} {
171200
flatTeams[i] = t
172201
}
173202

174-
return flatTeams
203+
return flatTeams, nil
175204
}

website/docs/d/organization_teams.html.markdown

+9-7
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ ___
3636

3737
The `team` block consists of:
3838

39-
* `id` - the ID of the team.
40-
* `node_id` - the Node ID of the team.
41-
* `slug` - the slug of the team.
42-
* `name` - the team's full name.
43-
* `description` - the team's description.
44-
* `privacy` - the team's privacy type.
39+
* `id` - The ID of the team.
40+
* `node_id` - The Node ID of the team.
41+
* `slug` - The slug of the team.
42+
* `name` - The team's full name.
43+
* `description` - The team's description.
44+
* `privacy` - The team's privacy type.
4545
* `members` - List of team members. Not returned if `summary_only = true`
4646
* `repositories` - List of team repositories. Not returned if `summary_only = true`
47-
* `parent` - the parent team.
47+
* `parent_team_id` - The ID of the parent team, if there is one.
48+
* `parent_team_slug` - The slug of the parent team, if there is one.
49+
* `parent` - (**DEPRECATED**) The parent team, use `parent_team_id` or `parent_team_slug` instead.

0 commit comments

Comments
 (0)