-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathsql.go
More file actions
395 lines (370 loc) · 11.8 KB
/
sql.go
File metadata and controls
395 lines (370 loc) · 11.8 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package github
import (
"encoding/json"
"fmt"
"strings"
"github.com/grafana/github-datasource/pkg/models"
"github.com/grafana/grafana-plugin-sdk-go/backend"
schemas "github.com/grafana/schemads"
)
// tableToQueryType maps normalized table names to their QueryType constants.
// Built from the query type constants via normalizeTableNames so the schema
// table definitions and this map stay in sync automatically.
var tableToQueryType = func() map[string]string {
qts := []string{
models.QueryTypeCommits,
models.QueryTypeIssues,
models.QueryTypePullRequests,
models.QueryTypePullRequestReviews,
models.QueryTypeRepositories,
models.QueryTypeContributors,
models.QueryTypeTags,
models.QueryTypeReleases,
models.QueryTypeLabels,
models.QueryTypeMilestones,
models.QueryTypePackages,
models.QueryTypeVulnerabilities,
models.QueryTypeProjects,
models.QueryTypeStargazers,
models.QueryTypeWorkflows,
models.QueryTypeWorkflowUsage,
models.QueryTypeWorkflowRuns,
models.QueryTypeCodeScanning,
models.QueryTypeDeployments,
models.QueryTypeOrganizations,
models.QueryTypeGraphQL,
}
m := make(map[string]string, len(qts))
for _, qt := range qts {
m[normalizeTableNames(qt)] = qt
}
return m
}()
// parseJSONStringValues tries to parse s as a JSON array of strings.
// If successful it returns the individual elements; otherwise it returns
// the original string as a single-element slice so callers can always
// range over the result.
func parseJSONStringValues(s string) []string {
var arr []string
if err := json.Unmarshal([]byte(s), &arr); err == nil && len(arr) > 0 {
return arr
}
return []string{s}
}
func anyToString(v any) string {
if v == nil {
return ""
}
if s, ok := v.(string); ok {
return s
}
return fmt.Sprintf("%v", v)
}
func extractFilterValues(condition schemas.FilterCondition) []string {
out := make([]string, 0, len(condition.Values)+1)
for _, v := range condition.Values {
if s := anyToString(v); s != "" {
out = append(out, s)
}
}
if len(out) == 0 {
if s := anyToString(condition.Value); s != "" {
out = append(out, s)
}
}
return out
}
// applyFilters maps SQL filter predicates to GitHub API query options.
// It modifies the options map in-place and returns a list of GitHub search
// qualifiers for query types that use the search API.
func applyFilters(queryType string, options map[string]interface{}, filters []schemas.ColumnFilter) []string {
var searchQualifiers []string
opts, _ := options["options"].(map[string]interface{})
if opts == nil {
opts = make(map[string]interface{})
options["options"] = opts
}
appendEqualitySearchQualifier := func(name string, operator schemas.Operator, values []string, isJSON bool) {
if operator == schemas.OperatorEquals || operator == schemas.OperatorIn {
for _, value := range values {
if isJSON {
for _, v := range parseJSONStringValues(value) {
searchQualifiers = append(searchQualifiers, name+":"+v)
}
} else {
searchQualifiers = append(searchQualifiers, name+":"+value)
}
}
}
}
setOption := func(name string, operator schemas.Operator, value string) {
if operator == schemas.OperatorEquals || operator == schemas.OperatorIn {
opts[name] = value
}
}
for _, f := range filters {
if f.Name == "" || len(f.Conditions) == 0 {
continue
}
for _, condition := range f.Conditions {
values := extractFilterValues(condition)
if len(values) == 0 {
continue
}
switch queryType {
case models.QueryTypeIssues:
switch f.Name {
case "state":
appendEqualitySearchQualifier(f.Name, condition.Operator, values, false)
case "author":
appendEqualitySearchQualifier(f.Name, condition.Operator, values, false)
case "labels":
appendEqualitySearchQualifier("label", condition.Operator, values, true)
case "assignees":
appendEqualitySearchQualifier("assignee", condition.Operator, values, true)
case "milestone":
appendEqualitySearchQualifier("milestone", condition.Operator, values, false)
}
case models.QueryTypePullRequests, models.QueryTypePullRequestReviews:
switch f.Name {
case "author_login":
appendEqualitySearchQualifier("author", condition.Operator, values, false)
case "labels":
appendEqualitySearchQualifier("label", condition.Operator, values, true)
case "is_draft":
if condition.Operator == schemas.OperatorEquals || condition.Operator == schemas.OperatorIn {
for _, value := range values {
if value == "true" {
searchQualifiers = append(searchQualifiers, "draft:true")
} else {
searchQualifiers = append(searchQualifiers, "draft:false")
}
}
}
}
case models.QueryTypeCodeScanning:
switch f.Name {
case "state":
setOption("state", condition.Operator, values[0])
case "rule_severity":
setOption("severity", condition.Operator, values[0])
case "tool_name":
setOption("toolName", condition.Operator, values[0])
}
case models.QueryTypeWorkflowRuns:
switch f.Name {
case "head_branch":
setOption("branch", condition.Operator, values[0])
case "status":
setOption("status", condition.Operator, values[0])
case "event":
setOption("event", condition.Operator, values[0])
}
case models.QueryTypeContributors:
if f.Name == "name" && (condition.Operator == schemas.OperatorLike || condition.Operator == schemas.OperatorEquals || condition.Operator == schemas.OperatorIn) {
opts["query"] = values[0]
}
case models.QueryTypeLabels:
if f.Name == "name" && (condition.Operator == schemas.OperatorLike || condition.Operator == schemas.OperatorEquals || condition.Operator == schemas.OperatorIn) {
opts["query"] = values[0]
}
case models.QueryTypeMilestones:
if f.Name == "title" && (condition.Operator == schemas.OperatorLike || condition.Operator == schemas.OperatorEquals || condition.Operator == schemas.OperatorIn) {
opts["query"] = values[0]
}
case models.QueryTypePackages:
switch f.Name {
case "name":
if condition.Operator == schemas.OperatorEquals || condition.Operator == schemas.OperatorIn {
for _, value := range values {
existing, _ := opts["names"].(string)
if existing != "" {
opts["names"] = existing + "," + value
} else {
opts["names"] = value
}
}
}
case "type":
if condition.Operator == schemas.OperatorEquals || condition.Operator == schemas.OperatorIn {
opts["packageType"] = values[0]
}
}
case models.QueryTypeRepositories:
appendRepoSearchQualifier := func(qualifier string) {
existing, _ := options["repository"].(string)
options["repository"] = strings.TrimSpace(existing + " " + qualifier)
}
switch f.Name {
case "name":
if condition.Operator == schemas.OperatorLike || condition.Operator == schemas.OperatorEquals || condition.Operator == schemas.OperatorIn {
appendRepoSearchQualifier(values[0])
}
case "is_fork":
if condition.Operator == schemas.OperatorEquals && values[0] == "true" {
appendRepoSearchQualifier("fork:only")
}
case "is_private":
if condition.Operator == schemas.OperatorEquals {
if values[0] == "true" {
appendRepoSearchQualifier("is:private")
} else {
appendRepoSearchQualifier("is:public")
}
}
}
}
}
}
if len(searchQualifiers) > 0 {
existing, _ := opts["query"].(string)
combined := strings.Join(searchQualifiers, " ")
if existing != "" {
opts["query"] = existing + " " + combined
} else {
opts["query"] = combined
}
}
return searchQualifiers
}
func resolveTimeField(queryType, value string) (any, bool) {
switch queryType {
case models.QueryTypeIssues:
switch value {
case "created":
return models.IssueCreatedAt, true
case "closed":
return models.IssueClosedAt, true
case "updated":
return models.IssueUpdatedAt, true
}
case models.QueryTypePullRequests, models.QueryTypePullRequestReviews:
switch value {
case "closed":
return models.PullRequestClosedAt, true
case "created":
return models.PullRequestCreatedAt, true
case "merged":
return models.PullRequestMergedAt, true
case "updated":
return models.PullRequestUpdatedAt, true
}
case models.QueryTypeWorkflows:
switch value {
case "created":
return models.WorkflowCreatedAt, true
case "updated":
return models.WorkflowUpdatedAt, true
}
}
return 0, false
}
func defaultTimeField(queryType string) int {
switch queryType {
case models.QueryTypePullRequests, models.QueryTypePullRequestReviews:
return int(models.PullRequestCreatedAt)
default:
return 0
}
}
func normalizeGrafanaSQLRequest(req *backend.QueryDataRequest) *backend.QueryDataRequest {
if req == nil || len(req.Queries) == 0 {
return req
}
grafanaConfig := req.PluginContext.GrafanaConfig
queries := make([]backend.DataQuery, 0, len(req.Queries))
for _, q := range req.Queries {
var query schemas.Query
if err := json.Unmarshal(q.JSON, &query); err != nil {
queries = append(queries, q)
continue
}
if !query.GrafanaSql || query.Table == "" {
queries = append(queries, q)
continue
}
if query.GrafanaSql {
if grafanaConfig == nil {
backend.Logger.Warn("grafanaConfig is not set, skipping query")
continue
}
if !grafanaConfig.FeatureToggles().IsEnabled("dsAbstractionApp") {
backend.Logger.Warn("dsAbstractionApp is not enabled, skipping query")
continue
}
}
// Table names use hyphens only (never underscores), so the first
// underscore unambiguously separates the table name from the
// owner/repo suffix: "issues_grafana_grafana" -> "issues" + "grafana_grafana".
parts := strings.SplitN(query.Table, "_", 2)
queryType, ok := tableToQueryType[parts[0]]
if !ok {
queries = append(queries, q)
continue
}
var owner, repo string
if len(parts) == 2 {
ownerRepo := strings.SplitN(parts[1], "_", 2)
owner = ownerRepo[0]
if len(ownerRepo) == 2 {
repo = ownerRepo[1]
}
}
if v := strings.TrimSpace(anyToString(query.TableParameterValues["organization"])); v != "" {
owner = v
}
if v := strings.TrimSpace(anyToString(query.TableParameterValues["repository"])); v != "" {
repo = v
}
normalized := map[string]interface{}{
"refId": query.RefID,
"datasource": query.Datasource,
"queryType": queryType,
"owner": owner,
"repository": repo,
"options": map[string]interface{}{},
}
if queryType == models.QueryTypeProjects && owner != "" {
opts, _ := normalized["options"].(map[string]interface{})
opts["organization"] = owner
}
if v := strings.TrimSpace(anyToString(query.TableParameterValues["workflow"])); v != "" {
opts, _ := normalized["options"].(map[string]interface{})
opts["workflow"] = v
}
switch queryType {
case models.QueryTypeIssues, models.QueryTypePullRequests, models.QueryTypePullRequestReviews, models.QueryTypeWorkflows:
opts, _ := normalized["options"].(map[string]interface{})
if tfStr := strings.TrimSpace(anyToString(query.TableParameterValues["timeField"])); tfStr != "" {
if tf, ok := resolveTimeField(queryType, tfStr); ok {
opts["timeField"] = tf
} else {
opts["timeField"] = defaultTimeField(queryType)
}
} else {
opts["timeField"] = defaultTimeField(queryType)
}
}
if len(query.Filters) > 0 {
applyFilters(queryType, normalized, query.Filters)
}
jsonBytes, err := json.Marshal(normalized)
if err != nil {
queries = append(queries, q)
continue
}
queries = append(queries, backend.DataQuery{
RefID: q.RefID,
QueryType: queryType,
MaxDataPoints: q.MaxDataPoints,
Interval: q.Interval,
TimeRange: q.TimeRange,
JSON: jsonBytes,
})
}
return &backend.QueryDataRequest{
PluginContext: req.PluginContext,
Headers: req.Headers,
Queries: queries,
}
}