-
Notifications
You must be signed in to change notification settings - Fork 992
Expand file tree
/
Copy pathdata_source_github_organization_teams.go
More file actions
199 lines (170 loc) · 4.67 KB
/
Copy pathdata_source_github_organization_teams.go
File metadata and controls
199 lines (170 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package github
import (
"context"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/shurcooL/githubv4"
)
func dataSourceGithubOrganizationTeams() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceGithubOrganizationTeamsRead,
Schema: map[string]*schema.Schema{
"root_teams_only": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"summary_only": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"results_per_page": {
Type: schema.TypeInt,
Optional: true,
Default: 100,
ValidateDiagFunc: validation.ToDiagFunc(validation.IntBetween(0, 100)),
},
"teams": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Computed: true,
},
"node_id": {
Type: schema.TypeString,
Computed: true,
},
"slug": {
Type: schema.TypeString,
Required: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"privacy": {
Type: schema.TypeString,
Computed: true,
},
"members": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"repositories": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"parent": {
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,
},
},
},
},
},
}
}
func dataSourceGithubOrganizationTeamsRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
err := checkOrganization(meta)
if err != nil {
return diag.FromErr(err)
}
client := meta.(*Owner).v4client
orgName := meta.(*Owner).name
rootTeamsOnly := d.Get("root_teams_only").(bool)
summaryOnly := d.Get("summary_only").(bool)
resultsPerPage := d.Get("results_per_page").(int)
var query TeamsQuery
variables := map[string]any{
"first": githubv4.Int(resultsPerPage),
"login": githubv4.String(orgName),
"cursor": (*githubv4.String)(nil),
"rootTeamsOnly": githubv4.Boolean(rootTeamsOnly),
"summaryOnly": githubv4.Boolean(summaryOnly),
}
var teams []any
for {
err = client.Query(meta.(*Owner).StopContext, &query, variables)
if err != nil {
return diag.FromErr(err)
}
+ additionalTeams, err := flattenGitHubTeams(query)
if err != nil {
return diag.FromErr(err)
}
teams = append(teams, additionalTeams...)
if !query.Organization.Teams.PageInfo.HasNextPage {
break
}
variables["cursor"] = new(query.Organization.Teams.PageInfo.EndCursor)
}
d.SetId(string(query.Organization.ID))
err = d.Set("teams", teams)
if err != nil {
return diag.FromErr(err)
}
return nil
}
func flattenGitHubTeams(tq TeamsQuery) ([]any, error) {
teams := tq.Organization.Teams.Nodes
if len(teams) == 0 {
return make([]any, 0), nil
}
flatTeams := make([]any, len(teams))
for i, team := range teams {
t := make(map[string]any)
t["id"] = team.DatabaseID
t["node_id"] = team.ID
t["slug"] = team.Slug
t["name"] = team.Name
t["description"] = team.Description
t["privacy"] = team.Privacy
members := team.Members.Nodes
flatMembers := make([]string, len(members))
for i, member := range members {
flatMembers[i] = string(member.Login)
}
t["members"] = flatMembers
var parentTeamId string
parentTeam := make(map[string]any)
if team.Parent.DatabaseID != 0 {
parentTeamId = strconv.FormatInt(int64(team.Parent.DatabaseID), 10)
}
parentTeam["id"] = team.Parent.ID
parentTeam["slug"] = team.Parent.Slug
parentTeam["name"] = team.Parent.Name
t["parent_team_id"] = parentTeamId
t["parent_team_slug"] = team.Parent.Slug
t["parent"] = parentTeam
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
}
return flatTeams, nil
}