-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathtable_github_repository_ruleset.go
More file actions
277 lines (242 loc) · 9.25 KB
/
Copy pathtable_github_repository_ruleset.go
File metadata and controls
277 lines (242 loc) · 9.25 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package github
import (
"context"
"time"
"github.com/shurcooL/githubv4"
"github.com/turbot/steampipe-plugin-github/github/models"
"github.com/turbot/steampipe-plugin-sdk/v6/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v6/plugin"
"github.com/turbot/steampipe-plugin-sdk/v6/plugin/transform"
)
func tableGitHubRepositoryRuleset() *plugin.Table {
return &plugin.Table{
Name: "github_repository_ruleset",
Description: "Retrieve the rulesets of a specified GitHub repository.",
List: &plugin.ListConfig{
Hydrate: tableGitHubRepositoryRulesetList,
KeyColumns: []*plugin.KeyColumn{
{Name: "repository_full_name", Require: plugin.Required},
},
},
Columns: gitHubRulesetColumns(),
}
}
func gitHubRulesetColumns() []*plugin.Column {
return []*plugin.Column{
{Name: "repository_full_name", Type: proto.ColumnType_STRING, Transform: transform.FromQual("repository_full_name"), Description: "Full name of the repository that contains the ruleset."},
{Name: "name", Type: proto.ColumnType_STRING, Description: "The name of the ruleset."},
{Name: "id", Type: proto.ColumnType_STRING, Description: "The ID of the ruleset."},
{Name: "created_at", Type: proto.ColumnType_TIMESTAMP, Transform: transform.FromField("CreatedAt").Transform(convertRulesetTimestamp), Description: "The date and time when the ruleset was created."},
{Name: "database_id", Type: proto.ColumnType_INT, Description: "The database ID of the ruleset."},
{Name: "enforcement", Type: proto.ColumnType_STRING, Description: "The enforcement level of the ruleset."},
{Name: "rules", Type: proto.ColumnType_JSON, Description: "The list of rules in the ruleset."},
{Name: "bypass_actors", Type: proto.ColumnType_JSON, Description: "The list of actors who can bypass the ruleset."},
{Name: "conditions", Type: proto.ColumnType_JSON, Description: "The conditions under which the ruleset applies."},
}
}
func tableGitHubRepositoryRulesetList(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
var query struct {
RateLimit models.RateLimit
Repository struct {
Rulesets struct {
PageInfo struct {
HasNextPage bool
EndCursor githubv4.String
}
Edges []struct {
Node struct {
CreatedAt githubv4.DateTime
DatabaseID int
Enforcement string
Name string
ID string
Rules struct {
PageInfo models.PageInfo
Edges []struct {
Node models.Rule
}
} `graphql:"rules(first: $rulePageSize, after: $ruleCursor)"`
BypassActors struct {
PageInfo models.PageInfo
Edges []struct {
Node models.BypassActor
}
} `graphql:"bypassActors(first: $bypassActorPageSize, after: $bypassActorCursor)"`
Conditions models.Conditions
}
}
} `graphql:"rulesets(first: $rulesetPageSize, after: $rulesetCursor)"`
} `graphql:"repository(owner: $owner, name: $name)"`
}
rulesetPageSize := adjustPageSize(100, d.QueryContext.Limit)
rulePageSize := 100
bypassActorPageSize := 100
fullName := d.EqualsQuals["repository_full_name"].GetStringValue()
owner, repo := parseRepoFullName(fullName)
variables := map[string]interface{}{
"owner": githubv4.String(owner),
"name": githubv4.String(repo),
"rulesetPageSize": githubv4.Int(rulesetPageSize),
"rulesetCursor": (*githubv4.String)(nil),
"rulePageSize": githubv4.Int(rulePageSize),
"ruleCursor": (*githubv4.String)(nil),
"bypassActorPageSize": githubv4.Int(bypassActorPageSize),
"bypassActorCursor": (*githubv4.String)(nil),
}
client := connectV4(ctx, d)
var rulesets []models.Ruleset
for {
err := client.Query(ctx, &query, variables)
plugin.Logger(ctx).Debug(rateLimitLogString("github_repository_ruleset", &query.RateLimit))
if err != nil {
plugin.Logger(ctx).Error("github_repository_ruleset", "api_error", err)
return nil, err
}
for _, edge := range query.Repository.Rulesets.Edges {
// Fetch additional Rules.
var rules []models.Rule
for _, rule := range edge.Node.Rules.Edges {
rules = append(rules, rule.Node)
}
if edge.Node.Rules.PageInfo.HasNextPage {
additionalRules := getAdditionalRules(ctx, d, client, edge.Node.DatabaseID, owner, repo, "")
rules = append(rules, additionalRules...)
}
// Fetch additional ByPassActors.
var bypassActors []models.BypassActor
for _, actor := range edge.Node.BypassActors.Edges {
bypassActors = append(bypassActors, actor.Node)
}
if edge.Node.BypassActors.PageInfo.HasNextPage {
additionalBypassActors := getAdditionalBypassActors(ctx, d, client, owner, repo, edge.Node.DatabaseID, "")
bypassActors = append(bypassActors, additionalBypassActors...)
}
ruleset := models.Ruleset{
CreatedAt: edge.Node.CreatedAt.String(),
DatabaseID: edge.Node.DatabaseID,
Enforcement: edge.Node.Enforcement,
Name: edge.Node.Name,
ID: edge.Node.ID,
Rules: rules,
BypassActors: bypassActors,
Conditions: edge.Node.Conditions,
}
rulesets = append(rulesets, ruleset)
}
for _, ruleset := range rulesets {
d.StreamListItem(ctx, ruleset)
if d.RowsRemaining(ctx) == 0 {
return nil, nil
}
}
if !query.Repository.Rulesets.PageInfo.HasNextPage {
break
}
variables["rulesetCursor"] = githubv4.NewString(query.Repository.Rulesets.PageInfo.EndCursor)
}
return nil, nil
}
func getAdditionalRules(ctx context.Context, d *plugin.QueryData, client *githubv4.Client, databaseID int, owner string, repo string, initialCursor githubv4.String) []models.Rule {
var query struct {
RateLimit models.RateLimit
Repository struct {
Ruleset struct {
Rules struct {
PageInfo struct {
HasNextPage bool
EndCursor githubv4.String
}
Edges []struct {
Node models.Rule
}
} `graphql:"rules(first: $pageSize, after: $cursor)"`
} `graphql:"ruleset(databaseId: $databaseID)"`
} `graphql:"repository(owner: $owner, name: $name)"`
}
variables := map[string]interface{}{
"pageSize": githubv4.Int(100),
"cursor": githubv4.NewString(initialCursor),
"databaseID": githubv4.Int(databaseID),
"owner": githubv4.String(owner),
"name": githubv4.String(repo),
}
var rules []models.Rule
for {
err := client.Query(ctx, &query, variables)
plugin.Logger(ctx).Debug(rateLimitLogString("github_repository_ruleset.getAdditionalRules", &query.RateLimit))
if err != nil {
plugin.Logger(ctx).Error("github_repository_ruleset.getAdditionalRules", "api_error", err)
return nil
}
for _, edge := range query.Repository.Ruleset.Rules.Edges {
rules = append(rules, edge.Node)
}
if !query.Repository.Ruleset.Rules.PageInfo.HasNextPage {
break
}
variables["cursor"] = githubv4.NewString(query.Repository.Ruleset.Rules.PageInfo.EndCursor)
}
return rules
}
func getAdditionalBypassActors(ctx context.Context, d *plugin.QueryData, client *githubv4.Client, owner string, repo string, databaseID int, initialCursor githubv4.String) []models.BypassActor {
var query struct {
RateLimit models.RateLimit
Repository struct {
Ruleset struct {
BypassActors struct {
PageInfo struct {
HasNextPage bool
EndCursor githubv4.String
}
Edges []struct {
Node models.BypassActor
}
} `graphql:"bypassActors(first: $pageSize, after: $cursor)"`
} `graphql:"ruleset(databaseId: $databaseID)"`
} `graphql:"repository(owner: $owner, name: $name)"`
}
variables := map[string]interface{}{
"owner": githubv4.String(owner),
"name": githubv4.String(repo),
"pageSize": githubv4.Int(100),
"cursor": githubv4.NewString(initialCursor),
"databaseID": githubv4.Int(databaseID),
}
var bypassActors []models.BypassActor
for {
err := client.Query(ctx, &query, variables)
plugin.Logger(ctx).Debug(rateLimitLogString("github_repository_ruleset.getAdditionalBypassActors", &query.RateLimit))
if err != nil {
plugin.Logger(ctx).Error("github_repository_ruleset.getAdditionalBypassActors", "api_error", err)
return nil
}
for _, edge := range query.Repository.Ruleset.BypassActors.Edges {
bypassActors = append(bypassActors, edge.Node)
}
if !query.Repository.Ruleset.BypassActors.PageInfo.HasNextPage {
break
}
variables["cursor"] = githubv4.NewString(query.Repository.Ruleset.BypassActors.PageInfo.EndCursor)
}
return bypassActors
}
//// TRANSFORM FUNCTION
// The timestamp value we are receiving has the layout "2024-06-11 13:18:48 +0000 UTC".
// Our generic timestamp function does not support converting this specific layout to the desired format.
// Additionally, it is not feasible to create a generic function that handles all possible timestamp layouts.
// Therefore, we have opted to implement a specific timestamp conversion function for this table only.
func convertRulesetTimestamp(ctx context.Context, d *transform.TransformData) (interface{}, error) {
if d.Value == nil {
return nil, nil
}
t := d.Value.(string)
// Parse the timestamp into a time.Time object
parsedTime, err := time.Parse("2006-01-02 15:04:05 -0700 MST", t)
if err != nil {
plugin.Logger(ctx).Error("Error parsing time:", err)
return nil, err
}
// Format the time.Time object to RFC 3339 format
rfc3339Time := parsedTime.Format(time.RFC3339)
return rfc3339Time, nil
}