-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtable_github_issue.go
239 lines (209 loc) · 12.8 KB
/
table_github_issue.go
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
package github
import (
"context"
"fmt"
"time"
"github.com/shurcooL/githubv4"
"github.com/turbot/steampipe-plugin-github/github/models"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
)
func gitHubIssueColumns() []*plugin.Column {
tableCols := []*plugin.Column{
{Name: "repository_full_name", Type: proto.ColumnType_STRING, Transform: transform.FromQual("repository_full_name"), Description: "The full name of the repository (login/repo-name)."},
}
return append(tableCols, sharedIssueColumns()...)
}
func sharedIssueColumns() []*plugin.Column {
return []*plugin.Column{
{Name: "number", Type: proto.ColumnType_INT, Transform: transform.FromField("Number", "Node.Number"), Description: "The issue number."},
{Name: "id", Type: proto.ColumnType_INT, Transform: transform.FromField("Id", "Node.Id"), Description: "The ID of the issue."},
{Name: "node_id", Type: proto.ColumnType_STRING, Transform: transform.FromField("NodeId", "Node.NodeId"), Description: "The node ID of the issue."},
{Name: "active_lock_reason", Type: proto.ColumnType_STRING, Transform: transform.FromField("ActiveLockReason", "Node.ActiveLockReason"), Description: "Reason that the conversation was locked."},
{Name: "author", Type: proto.ColumnType_JSON, Hydrate: issueHydrateAuthor, Transform: transform.FromValue().NullIfZero(), Description: "The actor who authored the issue."},
{Name: "author_login", Type: proto.ColumnType_STRING, Hydrate: issueHydrateAuthorLogin, Transform: transform.FromValue(), Description: "The login of the issue author."},
{Name: "author_association", Type: proto.ColumnType_STRING, Transform: transform.FromField("AuthorAssociation", "Node.AuthorAssociation"), Description: "Author's association with the subject of the issue."},
{Name: "body", Type: proto.ColumnType_STRING, Hydrate: issueHydrateBody, Transform: transform.FromValue(), Description: "Identifies the body of the issue."},
{Name: "body_url", Type: proto.ColumnType_STRING, Transform: transform.FromField("BodyUrl", "Node.BodyUrl"), Description: "URL for this issue body."},
{Name: "closed", Type: proto.ColumnType_BOOL, Transform: transform.FromField("Closed", "Node.Closed"), Description: "If true, issue is closed."},
{Name: "closed_at", Type: proto.ColumnType_TIMESTAMP, Transform: transform.FromField("ClosedAt", "Node.ClosedAt").NullIfZero().Transform(convertTimestamp), Description: "Timestamp when issue was closed."},
{Name: "created_at", Type: proto.ColumnType_TIMESTAMP, Transform: transform.FromField("CreatedAt", "Node.CreatedAt").NullIfZero().Transform(convertTimestamp), Description: "Timestamp when issue was created."},
{Name: "created_via_email", Type: proto.ColumnType_BOOL, Transform: transform.FromField("CreatedViaEmail", "Node.CreatedViaEmail"), Description: "If true, issue was created via email."},
{Name: "editor", Type: proto.ColumnType_JSON, Hydrate: issueHydrateEditor, Transform: transform.FromValue().NullIfZero(), Description: "The actor who edited the issue."},
{Name: "full_database_id", Type: proto.ColumnType_INT, Transform: transform.FromField("FullDatabaseId", "Node.FullDatabaseId"), Description: "Identifies the primary key from the database as a BigInt."},
{Name: "includes_created_edit", Type: proto.ColumnType_BOOL, Transform: transform.FromField("IncludesCreatedEdit", "Node.IncludesCreatedEdit"), Description: "If true, issue was edited and includes an edit with the creation data."},
{Name: "is_pinned", Type: proto.ColumnType_BOOL, Transform: transform.FromField("IsPinned", "Node.IsPinned"), Description: "if true, this issue is currently pinned to the repository issues list."},
{Name: "is_read_by_user", Type: proto.ColumnType_BOOL, Transform: transform.FromField("IsReadByUser", "Node.IsReadByUser"), Description: "if true, this issue has been read by the user."},
{Name: "last_edited_at", Type: proto.ColumnType_TIMESTAMP, Transform: transform.FromField("LastEditedAt", "Node.LastEditedAt").NullIfZero().Transform(convertTimestamp), Description: "Timestamp when issue was last edited."},
{Name: "locked", Type: proto.ColumnType_BOOL, Transform: transform.FromField("Locked", "Node.Locked"), Description: "If true, issue is locked."},
{Name: "milestone", Type: proto.ColumnType_JSON, Hydrate: issueHydrateMilestone, Transform: transform.FromValue().NullIfZero(), Description: "The milestone associated with the issue."},
{Name: "published_at", Type: proto.ColumnType_TIMESTAMP, Transform: transform.FromField("PublishedAt", "Node.PublishedAt").NullIfZero().Transform(convertTimestamp), Description: "Timestamp when issue was published."},
{Name: "state", Type: proto.ColumnType_STRING, Transform: transform.FromField("State", "Node.State"), Description: "The state of the issue."},
{Name: "state_reason", Type: proto.ColumnType_STRING, Transform: transform.FromField("StateReason", "Node.StateReason"), Description: "The reason for the issue state."},
{Name: "title", Type: proto.ColumnType_STRING, Transform: transform.FromField("Title", "Node.Title"), Description: "The title of the issue."},
{Name: "updated_at", Type: proto.ColumnType_TIMESTAMP, Transform: transform.FromField("UpdatedAt", "Node.UpdatedAt").NullIfZero().Transform(convertTimestamp), Description: "Timestamp when issue was last updated."},
{Name: "url", Type: proto.ColumnType_STRING, Transform: transform.FromField("Url", "Node.Url"), Description: "URL for the issue."},
{Name: "assignees_total_count", Type: proto.ColumnType_INT, Hydrate: issueHydrateAssigneeCount, Transform: transform.FromValue(), Description: "Count of assignees on the issue."},
{Name: "comments_total_count", Type: proto.ColumnType_INT, Hydrate: issueHydrateCommentCount, Transform: transform.FromValue(), Description: "Count of comments on the issue."},
{Name: "labels_total_count", Type: proto.ColumnType_INT, Hydrate: issueHydrateLabelsCount, Transform: transform.FromValue(), Description: "Count of labels on the issue."},
{Name: "labels_src", Type: proto.ColumnType_JSON, Hydrate: issueHydrateLabels, Transform: transform.FromValue(), Description: "The first 100 labels associated to the issue."},
{Name: "labels", Type: proto.ColumnType_JSON, Description: "A map of labels for the issue.", Hydrate: issueHydrateLabels, Transform: transform.FromValue().Transform(LabelTransform)},
{Name: "user_can_close", Type: proto.ColumnType_BOOL, Hydrate: issueHydrateUserCanClose, Transform: transform.FromValue(), Description: "If true, user can close the issue."},
{Name: "user_can_react", Type: proto.ColumnType_BOOL, Hydrate: issueHydrateUserCanReact, Transform: transform.FromValue(), Description: "If true, user can react on the issue."},
{Name: "user_can_reopen", Type: proto.ColumnType_BOOL, Hydrate: issueHydrateUserCanReopen, Transform: transform.FromValue(), Description: "If true, user can reopen the issue."},
{Name: "user_can_subscribe", Type: proto.ColumnType_BOOL, Hydrate: issueHydrateUserCanSubscribe, Transform: transform.FromValue(), Description: "If true, user can subscribe to the issue."},
{Name: "user_can_update", Type: proto.ColumnType_BOOL, Hydrate: issueHydrateUserCanUpdate, Transform: transform.FromValue(), Description: "If true, user can update the issue,"},
{Name: "user_cannot_update_reasons", Type: proto.ColumnType_JSON, Hydrate: issueHydrateUserCannotUpdateReasons, Transform: transform.FromValue().NullIfZero(), Description: "A list of reason why user cannot update the issue."},
{Name: "user_did_author", Type: proto.ColumnType_BOOL, Hydrate: issueHydrateUserDidAuthor, Transform: transform.FromValue(), Description: "If true, user authored the issue."},
{Name: "user_subscription", Type: proto.ColumnType_STRING, Hydrate: issueHydrateUserSubscription, Transform: transform.FromValue(), Description: "Subscription state of the user to the issue."},
}
}
func tableGitHubIssue() *plugin.Table {
return &plugin.Table{
Name: "github_issue",
Description: "GitHub Issues are used to track ideas, enhancements, tasks, or bugs for work on GitHub.",
List: &plugin.ListConfig{
KeyColumns: []*plugin.KeyColumn{
{
Name: "repository_full_name",
Require: plugin.Required,
},
{
Name: "author_login",
Require: plugin.Optional,
},
{
Name: "state",
Require: plugin.Optional,
},
{
Name: "updated_at",
Require: plugin.Optional,
Operators: []string{">", ">="},
},
},
ShouldIgnoreError: isNotFoundError([]string{"404"}),
Hydrate: tableGitHubRepositoryIssueList,
},
Get: &plugin.GetConfig{
KeyColumns: plugin.AllColumns([]string{"repository_full_name", "number"}),
ShouldIgnoreError: isNotFoundError([]string{"404"}),
Hydrate: tableGitHubRepositoryIssueGet,
},
Columns: gitHubIssueColumns(),
}
}
func tableGitHubRepositoryIssueList(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
quals := d.EqualsQuals
fullName := quals["repository_full_name"].GetStringValue()
owner, repoName := parseRepoFullName(fullName)
pageSize := adjustPageSize(100, d.QueryContext.Limit)
var filters githubv4.IssueFilters
if quals["state"] != nil {
state := quals["state"].GetStringValue()
switch state {
case "OPEN":
filters.States = &[]githubv4.IssueState{githubv4.IssueStateOpen}
case "CLOSED":
filters.States = &[]githubv4.IssueState{githubv4.IssueStateClosed}
default:
plugin.Logger(ctx).Error("github_issue", "invalid filter", "state", state)
return nil, fmt.Errorf("invalid value for 'state' can only filter for 'OPEN' or 'CLOSED' - you attempted to filter for '%s'", state)
}
} else {
filters.States = &[]githubv4.IssueState{githubv4.IssueStateOpen, githubv4.IssueStateClosed}
}
if quals["author_login"] != nil {
author := quals["author_login"].GetStringValue()
filters.CreatedBy = githubv4.NewString(githubv4.String(author))
}
if d.Quals["updated_at"] != nil {
for _, q := range d.Quals["updated_at"].Quals {
givenTime := q.Value.GetTimestampValue().AsTime()
afterTime := givenTime.Add(time.Second * 1)
switch q.Operator {
case ">":
filters.Since = githubv4.NewDateTime(githubv4.DateTime{Time: afterTime})
case ">=":
filters.Since = githubv4.NewDateTime(githubv4.DateTime{Time: givenTime})
}
}
}
var query struct {
RateLimit models.RateLimit
Repository struct {
Issues struct {
PageInfo models.PageInfo
TotalCount int
Nodes []models.Issue
} `graphql:"issues(first: $pageSize, after: $cursor, filterBy: $filters)"`
} `graphql:"repository(owner: $owner, name: $name)"`
}
variables := map[string]interface{}{
"owner": githubv4.String(owner),
"name": githubv4.String(repoName),
"pageSize": githubv4.Int(pageSize),
"cursor": (*githubv4.String)(nil),
"filters": filters,
}
appendIssueColumnIncludes(&variables, d.QueryContext.Columns)
client := connectV4(ctx, d)
for {
err := client.Query(ctx, &query, variables)
plugin.Logger(ctx).Debug(rateLimitLogString("github_issue", &query.RateLimit))
if err != nil {
plugin.Logger(ctx).Error("github_issue", "api_error", err)
return nil, err
}
for _, issue := range query.Repository.Issues.Nodes {
d.StreamListItem(ctx, issue)
// 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.Issues.PageInfo.HasNextPage {
break
}
variables["cursor"] = githubv4.NewString(query.Repository.Issues.PageInfo.EndCursor)
}
return nil, nil
}
func tableGitHubRepositoryIssueGet(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
quals := d.EqualsQuals
issueNumber := int(quals["number"].GetInt64Value())
fullName := quals["repository_full_name"].GetStringValue()
owner, repo := parseRepoFullName(fullName)
client := connectV4(ctx, d)
var query struct {
RateLimit models.RateLimit
Repository struct {
Issue models.Issue `graphql:"issue(number: $issueNumber)"`
} `graphql:"repository(owner: $owner, name: $repo)"`
}
variables := map[string]interface{}{
"owner": githubv4.String(owner),
"repo": githubv4.String(repo),
"issueNumber": githubv4.Int(issueNumber),
}
appendIssueColumnIncludes(&variables, d.QueryContext.Columns)
err := client.Query(ctx, &query, variables)
plugin.Logger(ctx).Debug(rateLimitLogString("github_issue", &query.RateLimit))
if err != nil {
plugin.Logger(ctx).Error("github_issue", "api_error", err)
return nil, err
}
return query.Repository.Issue, nil
}
func LabelTransform(ctx context.Context, input *transform.TransformData) (interface{}, error) {
labels := make(map[string]bool)
t := fmt.Sprintf("%T", input.Value)
if input.Value != nil && t == "[]models.Label" {
ls := input.Value.([]models.Label)
for _, l := range ls {
labels[l.Name] = true
}
}
return labels, nil
}