-
Notifications
You must be signed in to change notification settings - Fork 12
/
table_jira_user.go
176 lines (152 loc) · 4.71 KB
/
table_jira_user.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
package jira
import (
"context"
"fmt"
"io"
"github.com/andygrunwald/go-jira"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
)
//// TABLE DEFINITION
func tableUser(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "jira_user",
Description: "User in the Jira cloud.",
List: &plugin.ListConfig{
Hydrate: listUsers,
},
HydrateConfig: []plugin.HydrateConfig{
{
// Limit concurrency to avoid a 429 too many requests error
Func: getUserGroups,
MaxConcurrency: 50,
},
},
Columns: commonColumns([]*plugin.Column{
{
Name: "display_name",
Description: "The display name of the user. Depending on the user's privacy setting, this may return an alternative value.",
Type: proto.ColumnType_STRING,
},
{
Name: "account_id",
Description: "The account ID of the user, which uniquely identifies the user across all Atlassian products. For example, 5b10ac8d82e05b22cc7d4ef5.",
Type: proto.ColumnType_STRING,
Transform: transform.FromGo(),
},
{
Name: "email_address",
Description: "The email address of the user. Depending on the user's privacy setting, this may be returned as null.",
Type: proto.ColumnType_STRING,
},
{
Name: "account_type",
Description: "The user account type. Can take the following values: atlassian, app, customer and unknown.",
Type: proto.ColumnType_STRING,
},
{
Name: "active",
Description: "Indicates if user is active.",
Type: proto.ColumnType_BOOL,
Transform: transform.FromField("Active"),
},
{
Name: "self",
Description: "The URL of the user.",
Type: proto.ColumnType_STRING,
},
{
Name: "avatar_urls",
Description: "The avatars of the user.",
Type: proto.ColumnType_JSON,
},
{
Name: "group_names",
Description: "The groups that the user belongs to.",
Type: proto.ColumnType_JSON,
Hydrate: getUserGroups,
Transform: transform.From(groupNames),
},
// Standard columns
{
Name: "title",
Description: ColumnDescriptionTitle,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("DisplayName"),
},
}),
}
}
//// LIST FUNCTION
func listUsers(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
client, err := connect(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("jira_user.listUsers", "connection_error", err)
return nil, err
}
// If the requested number of items is less than the paging max limit
// set the limit to that instead
queryLimit := d.QueryContext.Limit
var maxResults int = 1000
if d.QueryContext.Limit != nil {
if *queryLimit < 1000 {
maxResults = int(*queryLimit)
}
}
last := 0
for {
apiEndpoint := fmt.Sprintf("rest/api/2/users/search?startAt=%d&maxResults=%d", last, maxResults)
req, err := client.NewRequest("GET", apiEndpoint, nil)
if err != nil {
plugin.Logger(ctx).Error("jira_user.listUsers", "get_request_error", err)
return nil, err
}
users := new([]jira.User)
res, err := client.Do(req, users)
body, _ := io.ReadAll(res.Body)
plugin.Logger(ctx).Debug("jira_user.listUsers", "res_body", string(body))
if err != nil {
plugin.Logger(ctx).Error("jira_user.listUsers", "api_error", err)
return nil, err
}
for _, user := range *users {
d.StreamListItem(ctx, user)
// Context may get cancelled due to manual cancellation or if the limit has been reached
if d.RowsRemaining(ctx) == 0 {
return nil, nil
}
}
// evaluate paging start value for next iteration
last = last + len(*users)
// API doesn't gives paging parameters in the response,
// therefore using output length to quit paging
if len(*users) < 1000 {
return nil, nil
}
}
}
//// HYDRATE FUNCTIONS
func getUserGroups(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
user := h.Item.(jira.User)
client, err := connect(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("jira_user.getUserGroups", "connection_error", err)
return nil, err
}
groups, _, err := client.User.GetGroups(user.AccountID)
if err != nil {
plugin.Logger(ctx).Error("jira_user.getUserGroups", "api_error", err)
return nil, err
}
return groups, nil
}
//// TRANSFORM FUNCTION
func groupNames(_ context.Context, d *transform.TransformData) (interface{}, error) {
userGroups := d.HydrateItem.(*[]jira.UserGroup)
var groupNames []string
for _, group := range *userGroups {
groupNames = append(groupNames, group.Name)
}
return groupNames, nil
}