-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathtable_github_my_team.go
More file actions
139 lines (120 loc) · 4.1 KB
/
Copy pathtable_github_my_team.go
File metadata and controls
139 lines (120 loc) · 4.1 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
package github
import (
"context"
"github.com/shurcooL/githubv4"
"github.com/turbot/steampipe-plugin-github/github/models"
"github.com/turbot/steampipe-plugin-sdk/v6/plugin"
)
func tableGitHubMyTeam() *plugin.Table {
return &plugin.Table{
Name: "github_my_team",
Description: "GitHub Teams that you belong to in your organization. GitHub Teams are groups of organization members that reflect your company or group's structure with cascading access permissions and mentions.",
List: &plugin.ListConfig{
Hydrate: tableGitHubMyTeamList,
},
Get: &plugin.GetConfig{
KeyColumns: plugin.AllColumns([]string{"organization", "slug"}),
ShouldIgnoreError: isNotFoundError([]string{"404"}),
Hydrate: tableGitHubTeamGet,
},
Columns: commonColumns(gitHubTeamColumns()),
}
}
func tableGitHubMyTeamList(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
var query struct {
RateLimit models.RateLimit
Viewer struct {
Organizations struct {
PageInfo models.PageInfo
Nodes []struct {
Login string
Teams struct {
PageInfo models.PageInfo
Nodes []models.TeamWithCounts
} `graphql:"teams(first: $pageSize, after: $cursor)"`
}
} `graphql:"organizations(first: $orgPageSize, after: $orgCursor)"`
}
}
orgPageSize := 10 // Note: most users will be in <10 orgs, so this keeps node count down
pageSize := adjustPageSize(100, d.QueryContext.Limit)
variables := map[string]interface{}{
"orgPageSize": githubv4.Int(orgPageSize),
"orgCursor": (*githubv4.String)(nil),
"pageSize": githubv4.Int(pageSize),
"cursor": (*githubv4.String)(nil),
}
appendTeamColumnIncludes(&variables, d.QueryContext.Columns)
client := connectV4(ctx, d)
var teams []models.TeamWithCounts
for {
err := client.Query(ctx, &query, variables)
plugin.Logger(ctx).Debug(rateLimitLogString("github_my_team", &query.RateLimit))
if err != nil {
plugin.Logger(ctx).Error("github_my_team", "api_error", err)
return nil, err
}
for _, org := range query.Viewer.Organizations.Nodes {
plugin.Logger(ctx).Debug("github_my_team", "org", org.Login)
teams = append(teams, org.Teams.Nodes...)
if len(teams) >= int(d.RowsRemaining(ctx)) {
break
}
if org.Teams.PageInfo.HasNextPage {
ts, err := getAdditionalTeams(ctx, d, client, org.Login, org.Teams.PageInfo.EndCursor)
if err != nil {
plugin.Logger(ctx).Error("github_my_team", "api_error", err)
return nil, err
}
teams = append(teams, ts...)
}
if len(teams) >= int(d.RowsRemaining(ctx)) {
break
}
}
for _, team := range teams {
d.StreamListItem(ctx, team)
// Context can be cancelled due to manual cancellation or the limit has been hit
if d.RowsRemaining(ctx) == 0 {
return nil, nil
}
}
if !query.Viewer.Organizations.PageInfo.HasNextPage {
break
}
variables["cursor"] = githubv4.NewString(query.Viewer.Organizations.PageInfo.EndCursor)
}
return nil, nil
}
func getAdditionalTeams(ctx context.Context, d *plugin.QueryData, client *githubv4.Client, org string, initialCursor githubv4.String) ([]models.TeamWithCounts, error) {
var query struct {
RateLimit models.RateLimit
Organization struct {
Teams struct {
PageInfo models.PageInfo
Nodes []models.TeamWithCounts
} `graphql:"teams(first: $pageSize, after: $cursor)"`
} `graphql:"organization(login: $login)"`
}
variables := map[string]interface{}{
"pageSize": githubv4.Int(100),
"cursor": githubv4.NewString(initialCursor),
"login": githubv4.String(org),
}
appendTeamColumnIncludes(&variables, d.QueryContext.Columns)
var ts []models.TeamWithCounts
for {
err := client.Query(ctx, &query, variables)
plugin.Logger(ctx).Debug(rateLimitLogString("github_my_team", &query.RateLimit))
if err != nil {
plugin.Logger(ctx).Error("github_my_team", "api_error", err)
return nil, err
}
ts = append(ts, query.Organization.Teams.Nodes...)
if !query.Organization.Teams.PageInfo.HasNextPage {
break
}
variables["cursor"] = githubv4.NewString(query.Organization.Teams.PageInfo.EndCursor)
}
return ts, nil
}