-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathtable_github_branch.go
More file actions
97 lines (81 loc) · 3.3 KB
/
Copy pathtable_github_branch.go
File metadata and controls
97 lines (81 loc) · 3.3 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
package github
import (
"context"
"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 tableGitHubBranch() *plugin.Table {
return &plugin.Table{
Name: "github_branch",
Description: "Branches in the given repository.",
List: &plugin.ListConfig{
KeyColumns: []*plugin.KeyColumn{
{Name: "repository_full_name", Require: plugin.Required},
},
ShouldIgnoreError: isNotFoundError([]string{"404"}),
Hydrate: tableGitHubBranchList,
},
Columns: commonColumns([]*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 branch."},
{Name: "name", Type: proto.ColumnType_STRING, Description: "Name of the branch."},
{Name: "commit", Type: proto.ColumnType_JSON, Transform: transform.FromField("Target.Commit"), Description: "Latest commit on the branch."},
{Name: "protected", Type: proto.ColumnType_BOOL, Hydrate: branchHydrateProtected, Transform: transform.FromValue().Transform(HasValue), Description: "If true, the branch is protected."},
{Name: "branch_protection_rule", Type: proto.ColumnType_JSON, Hydrate: branchHydrateBranchProtectionRule, Transform: transform.FromValue().NullIfZero(), Description: "Branch protection rule if protected."},
}),
}
}
func tableGitHubBranchList(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
client := connectV4(ctx, d)
fullName := d.EqualsQuals["repository_full_name"].GetStringValue()
owner, repo := parseRepoFullName(fullName)
pageSize := adjustPageSize(100, d.QueryContext.Limit)
var query struct {
RateLimit models.RateLimit
Repository struct {
Refs struct {
TotalCount int
PageInfo models.PageInfo
Edges []struct {
Node models.Branch
}
} `graphql:"refs(refPrefix: \"refs/heads/\", first: $pageSize, after: $cursor)"`
} `graphql:"repository(owner: $owner, name: $repo)"`
}
variables := map[string]interface{}{
"owner": githubv4.String(owner),
"repo": githubv4.String(repo),
"pageSize": githubv4.Int(pageSize),
"cursor": (*githubv4.String)(nil),
}
appendBranchColumnIncludes(&variables, d.QueryContext.Columns)
for {
err := client.Query(ctx, &query, variables)
plugin.Logger(ctx).Debug(rateLimitLogString("github_branch", &query.RateLimit))
if err != nil {
plugin.Logger(ctx).Error("github_branch", "api_error", err)
return nil, err
}
for _, branch := range query.Repository.Refs.Edges {
d.StreamListItem(ctx, branch.Node)
// Context can be cancelled due to manual cancellation or the limit has been hit
if d.RowsRemaining(ctx) == 0 {
return nil, nil
}
}
if !query.Repository.Refs.PageInfo.HasNextPage {
break
}
variables["cursor"] = githubv4.NewString(query.Repository.Refs.PageInfo.EndCursor)
}
return nil, nil
}
// HasValue Note: if useful to other tables, move to utils.go
func HasValue(_ context.Context, input *transform.TransformData) (interface{}, error) {
if input.Value == nil || input.Value.(string) == "" {
return false, nil
}
return true, nil
}