Skip to content
This repository was archived by the owner on Dec 24, 2024. It is now read-only.

Commit ee26838

Browse files
committed
fix: empty event type query includes all events in query
1 parent f343c10 commit ee26838

File tree

7 files changed

+215
-136
lines changed

7 files changed

+215
-136
lines changed

src/api/sastEvents.ts

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { z } from 'zod';
22
import { DataFrame, FieldType, TimeRange, createDataFrame } from '@grafana/data';
33
import { FetchResponse, isFetchError } from '@grafana/runtime';
4-
import { SastEventsQueryOptions } from 'types';
4+
import { SastEventType, SastEventsQueryOptions } from 'types';
55
import { SastFinding } from './sastCommon';
66
import { unwrapRepositoryTemplateVariables } from 'utils/utils';
77

@@ -29,9 +29,9 @@ const SastEventsGitProvider = z.object({
2929
bitbucket: z.any().optional(),
3030
});
3131

32-
const SastEventsEventSchema = z.union([
33-
_BaseEventSchema.extend({
34-
type: z.literal('new-branch-summary'),
32+
const sastEventSchemaMap = {
33+
[SastEventType.NewBranchSummary]: _BaseEventSchema.extend({
34+
type: z.literal(SastEventType.NewBranchSummary),
3535
data: z.object({
3636
id: z.string(),
3737
provider: SastEventsGitProvider,
@@ -45,8 +45,8 @@ const SastEventsEventSchema = z.union([
4545
numUnknown: z.number(),
4646
}),
4747
}),
48-
_BaseEventSchema.extend({
49-
type: z.literal('new-finding'),
48+
[SastEventType.NewFinding]: _BaseEventSchema.extend({
49+
type: z.literal(SastEventType.NewFinding),
5050
data: z.object({
5151
id: z.string(),
5252
branch: z.string(),
@@ -57,8 +57,8 @@ const SastEventsEventSchema = z.union([
5757
userId: z.string(),
5858
}),
5959
}),
60-
_BaseEventSchema.extend({
61-
type: z.literal('new-findings'),
60+
[SastEventType.NewFindings]: _BaseEventSchema.extend({
61+
type: z.literal(SastEventType.NewFindings),
6262
data: z.object({
6363
id: z.string(),
6464
part: z.number().optional(),
@@ -70,8 +70,8 @@ const SastEventsEventSchema = z.union([
7070
userId: z.string(),
7171
}),
7272
}),
73-
_BaseEventSchema.extend({
74-
type: z.literal('new-fix'),
73+
[SastEventType.NewFix]: _BaseEventSchema.extend({
74+
type: z.literal(SastEventType.NewFix),
7575
data: z.object({
7676
id: z.string(),
7777
branch: z.string(),
@@ -82,8 +82,8 @@ const SastEventsEventSchema = z.union([
8282
userId: z.string(),
8383
}),
8484
}),
85-
_BaseEventSchema.extend({
86-
type: z.literal('new-fixes'),
85+
[SastEventType.NewFixes]: _BaseEventSchema.extend({
86+
type: z.literal(SastEventType.NewFixes),
8787
data: z.object({
8888
id: z.string(),
8989
part: z.number().optional(),
@@ -95,8 +95,8 @@ const SastEventsEventSchema = z.union([
9595
userId: z.string(),
9696
}),
9797
}),
98-
_BaseEventSchema.extend({
99-
type: z.literal('new-allowlisted-finding'),
98+
[SastEventType.NewAllowlistedFinding]: _BaseEventSchema.extend({
99+
type: z.literal(SastEventType.NewAllowlistedFinding),
100100
data: z.object({
101101
id: z.string(),
102102
branch: z.string(),
@@ -107,8 +107,8 @@ const SastEventsEventSchema = z.union([
107107
userId: z.string(),
108108
}),
109109
}),
110-
_BaseEventSchema.extend({
111-
type: z.literal('new-allowlisted-findings'),
110+
[SastEventType.NewAllowlistedFindings]: _BaseEventSchema.extend({
111+
type: z.literal(SastEventType.NewAllowlistedFindings),
112112
data: z.object({
113113
id: z.string(),
114114
part: z.number().optional(),
@@ -120,8 +120,8 @@ const SastEventsEventSchema = z.union([
120120
userId: z.string(),
121121
}),
122122
}),
123-
_BaseEventSchema.extend({
124-
type: z.literal('new-unallowlisted-finding'),
123+
[SastEventType.NewUnallowlistedFinding]: _BaseEventSchema.extend({
124+
type: z.literal(SastEventType.NewUnallowlistedFinding),
125125
data: z.object({
126126
id: z.string(),
127127
branch: z.string(),
@@ -132,8 +132,8 @@ const SastEventsEventSchema = z.union([
132132
userId: z.string(),
133133
}),
134134
}),
135-
_BaseEventSchema.extend({
136-
type: z.literal('new-unallowlisted-findings'),
135+
[SastEventType.NewUnallowlistedFindings]: _BaseEventSchema.extend({
136+
type: z.literal(SastEventType.NewUnallowlistedFindings),
137137
data: z.object({
138138
id: z.string(),
139139
part: z.number().optional(),
@@ -145,8 +145,8 @@ const SastEventsEventSchema = z.union([
145145
userId: z.string(),
146146
}),
147147
}),
148-
_BaseEventSchema.extend({
149-
type: z.literal('new-pull-request-finding'),
148+
[SastEventType.NewPullRequestFinding]: _BaseEventSchema.extend({
149+
type: z.literal(SastEventType.NewPullRequestFinding),
150150
data: z.object({
151151
id: z.string(),
152152
provider: SastEventsGitProvider,
@@ -158,8 +158,8 @@ const SastEventsEventSchema = z.union([
158158
userId: z.string(),
159159
}),
160160
}),
161-
_BaseEventSchema.extend({
162-
type: z.literal('new-pull-request-findings'),
161+
[SastEventType.NewPullRequestFindings]: _BaseEventSchema.extend({
162+
type: z.literal(SastEventType.NewPullRequestFindings),
163163
data: z.object({
164164
id: z.string(),
165165
provider: SastEventsGitProvider,
@@ -171,8 +171,8 @@ const SastEventsEventSchema = z.union([
171171
userId: z.string(),
172172
}),
173173
}),
174-
_BaseEventSchema.extend({
175-
type: z.literal('new-pull-request-fix'),
174+
[SastEventType.NewPullRequestFix]: _BaseEventSchema.extend({
175+
type: z.literal(SastEventType.NewPullRequestFix),
176176
data: z.object({
177177
id: z.string(),
178178
branch: z.string(),
@@ -183,8 +183,8 @@ const SastEventsEventSchema = z.union([
183183
userId: z.string(),
184184
}),
185185
}),
186-
_BaseEventSchema.extend({
187-
type: z.literal('new-pull-request-fixes'),
186+
[SastEventType.NewPullRequestFixes]: _BaseEventSchema.extend({
187+
type: z.literal(SastEventType.NewPullRequestFixes),
188188
data: z.object({
189189
id: z.string(),
190190
branch: z.string(),
@@ -195,8 +195,8 @@ const SastEventsEventSchema = z.union([
195195
userId: z.string(),
196196
}),
197197
}),
198-
_BaseEventSchema.extend({
199-
type: z.literal('new-pull-request-allowlisted-finding'),
198+
[SastEventType.NewPullRequestAllowlistedFinding]: _BaseEventSchema.extend({
199+
type: z.literal(SastEventType.NewPullRequestAllowlistedFinding),
200200
data: z.object({
201201
id: z.string(),
202202
branch: z.string(),
@@ -207,8 +207,8 @@ const SastEventsEventSchema = z.union([
207207
userId: z.string(),
208208
}),
209209
}),
210-
_BaseEventSchema.extend({
211-
type: z.literal('new-pull-request-allowlisted-findings'),
210+
[SastEventType.NewPullRequestAllowlistedFindings]: _BaseEventSchema.extend({
211+
type: z.literal(SastEventType.NewPullRequestAllowlistedFindings),
212212
data: z.object({
213213
id: z.string(),
214214
part: z.number().optional(),
@@ -220,8 +220,8 @@ const SastEventsEventSchema = z.union([
220220
userId: z.string(),
221221
}),
222222
}),
223-
_BaseEventSchema.extend({
224-
type: z.literal('new-pull-request-unallowlisted-finding'),
223+
[SastEventType.NewPullRequestUnallowlistedFinding]: _BaseEventSchema.extend({
224+
type: z.literal(SastEventType.NewPullRequestUnallowlistedFinding),
225225
data: z.object({
226226
id: z.string(),
227227
branch: z.string(),
@@ -232,8 +232,8 @@ const SastEventsEventSchema = z.union([
232232
userId: z.string(),
233233
}),
234234
}),
235-
_BaseEventSchema.extend({
236-
type: z.literal('new-pull-request-unallowlisted-findings'),
235+
[SastEventType.NewPullRequestUnallowlistedFindings]: _BaseEventSchema.extend({
236+
type: z.literal(SastEventType.NewPullRequestUnallowlistedFindings),
237237
data: z.object({
238238
id: z.string(),
239239
part: z.number().optional(),
@@ -245,10 +245,12 @@ const SastEventsEventSchema = z.union([
245245
userId: z.string(),
246246
}),
247247
}),
248-
]);
248+
};
249+
250+
const SastEventsEventSchema = z.union([z.never(), z.never(), ...Object.values(sastEventSchemaMap)]);
249251

250252
const SastEventsApiResponseSchema = z.object({
251-
events: z.array(SastEventsEventSchema).nullable().nullable(),
253+
events: z.array(SastEventsEventSchema).nullable(),
252254
numItems: z.number(),
253255
nextEventId: z.string(),
254256
});
@@ -284,7 +286,7 @@ export const processSastEvents = async (
284286
...(queryOptions.queryParameters.branch ? { branch: queryOptions.queryParameters.branch } : {}),
285287
...(queryOptions.queryParameters.eventTypes && queryOptions.queryParameters.eventTypes.length > 0
286288
? { eventType: queryOptions.queryParameters.eventTypes }
287-
: {}),
289+
: { eventType: Object.values(SastEventType) }),
288290
...(prevEventId ? { fromEvent: prevEventId } : { fromTime: range.from.toISOString() }),
289291
sort: 'desc',
290292
};
@@ -341,32 +343,40 @@ export const processSastEvents = async (
341343
{
342344
name: 'numFindings',
343345
type: FieldType.number,
344-
values: events.map((event) => (event.type === 'new-branch-summary' ? event.data.numFindings : undefined)),
346+
values: events.map((event) =>
347+
event.type === SastEventType.NewBranchSummary ? event.data.numFindings : undefined
348+
),
345349
},
346350
{
347351
name: 'numCritical',
348352
type: FieldType.number,
349-
values: events.map((event) => (event.type === 'new-branch-summary' ? event.data.numCritical : undefined)),
353+
values: events.map((event) =>
354+
event.type === SastEventType.NewBranchSummary ? event.data.numCritical : undefined
355+
),
350356
},
351357
{
352358
name: 'numHigh',
353359
type: FieldType.number,
354-
values: events.map((event) => (event.type === 'new-branch-summary' ? event.data.numHigh : undefined)),
360+
values: events.map((event) => (event.type === SastEventType.NewBranchSummary ? event.data.numHigh : undefined)),
355361
},
356362
{
357363
name: 'numMedium',
358364
type: FieldType.number,
359-
values: events.map((event) => (event.type === 'new-branch-summary' ? event.data.numMedium : undefined)),
365+
values: events.map((event) =>
366+
event.type === SastEventType.NewBranchSummary ? event.data.numMedium : undefined
367+
),
360368
},
361369
{
362370
name: 'numLow',
363371
type: FieldType.number,
364-
values: events.map((event) => (event.type === 'new-branch-summary' ? event.data.numLow : undefined)),
372+
values: events.map((event) => (event.type === SastEventType.NewBranchSummary ? event.data.numLow : undefined)),
365373
},
366374
{
367375
name: 'numUnknown',
368376
type: FieldType.number,
369-
values: events.map((event) => (event.type === 'new-branch-summary' ? event.data.numUnknown : undefined)),
377+
values: events.map((event) =>
378+
event.type === SastEventType.NewBranchSummary ? event.data.numUnknown : undefined
379+
),
370380
},
371381
{
372382
name: 'repositoryId',

0 commit comments

Comments
 (0)