-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathsonar-tests.service.js
282 lines (266 loc) · 7.07 KB
/
sonar-tests.service.js
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
import { pathParam } from '../index.js'
import {
testResultQueryParamSchema,
testResultOpenApiQueryParams,
renderTestResultBadge,
documentation as testResultsDocumentation,
} from '../test-results.js'
import { metric as metricCount } from '../text-formatters.js'
import SonarBase from './sonar-base.js'
import {
documentation,
queryParamSchema,
openApiQueryParams,
getLabel,
} from './sonar-helpers.js'
class SonarTestsSummary extends SonarBase {
static category = 'test-results'
static route = {
base: 'sonar/tests',
pattern: ':component/:branch*',
queryParamSchema: queryParamSchema.concat(testResultQueryParamSchema),
}
static openApi = {
'/sonar/tests/{component}': {
get: {
summary: 'Sonar Tests',
description: `${documentation}
${testResultsDocumentation}
`,
parameters: [
pathParam({ name: 'component', example: 'swellaby:letra' }),
...openApiQueryParams,
...testResultOpenApiQueryParams,
],
},
},
'/sonar/tests/{component}/{branch}': {
get: {
summary: 'Sonar Tests (branch)',
description: `${documentation}
${testResultsDocumentation}
`,
parameters: [
pathParam({ name: 'component', example: 'swellaby:letra' }),
pathParam({ name: 'branch', example: 'master' }),
...openApiQueryParams,
...testResultOpenApiQueryParams,
],
},
},
}
static defaultBadgeData = {
label: 'tests',
}
static render({
passed,
failed,
skipped,
total,
passedLabel,
failedLabel,
skippedLabel,
isCompact,
}) {
return renderTestResultBadge({
passed,
failed,
skipped,
total,
passedLabel,
failedLabel,
skippedLabel,
isCompact,
})
}
transform({ json, sonarVersion }) {
const {
tests: total,
skipped_tests: skipped,
test_failures: failed,
} = super.transform({
json,
sonarVersion,
})
return {
total,
passed: total - (skipped + failed),
failed,
skipped,
}
}
async handle(
{ component, branch },
{
server,
sonarVersion,
compact_message: compactMessage,
passed_label: passedLabel,
failed_label: failedLabel,
skipped_label: skippedLabel,
},
) {
const json = await this.fetch({
sonarVersion,
server,
component,
branch,
metricName: 'tests,test_failures,skipped_tests',
})
const { total, passed, failed, skipped } = this.transform({
json,
sonarVersion,
})
return this.constructor.render({
passed,
failed,
skipped,
total,
isCompact: compactMessage !== undefined,
passedLabel,
failedLabel,
skippedLabel,
})
}
}
class SonarTests extends SonarBase {
static category = 'build'
static route = {
base: 'sonar',
pattern:
':metric(total_tests|skipped_tests|test_failures|test_errors|test_execution_time|test_success_density)/:component/:branch*',
queryParamSchema,
}
static openApi = {
'/sonar/{metric}/{component}': {
get: {
summary: 'Sonar Test Count',
description: documentation,
parameters: [
pathParam({
name: 'metric',
example: 'total_tests',
schema: {
type: 'string',
enum: [
'total_tests',
'skipped_tests',
'test_failures',
'test_errors',
],
},
}),
pathParam({ name: 'component', example: 'swellaby:letra' }),
...openApiQueryParams,
],
},
},
'/sonar/{metric}/{component}/{branch}': {
get: {
summary: 'Sonar Test Count (branch)',
description: documentation,
parameters: [
pathParam({
name: 'metric',
example: 'total_tests',
schema: {
type: 'string',
enum: [
'total_tests',
'skipped_tests',
'test_failures',
'test_errors',
],
},
}),
pathParam({ name: 'component', example: 'swellaby:letra' }),
pathParam({ name: 'branch', example: 'master' }),
...openApiQueryParams,
],
},
},
'/sonar/test_execution_time/{component}': {
get: {
summary: 'Sonar Test Execution Time',
description: documentation,
parameters: [
pathParam({ name: 'component', example: 'swellaby:letra' }),
...openApiQueryParams,
],
},
},
'/sonar/test_execution_time/{component}/{branch}': {
get: {
summary: 'Sonar Test Execution Time (branch)',
description: documentation,
parameters: [
pathParam({ name: 'component', example: 'swellaby:letra' }),
pathParam({ name: 'branch', example: 'master' }),
...openApiQueryParams,
],
},
},
'/sonar/test_success_density/{component}': {
get: {
summary: 'Sonar Test Success Rate',
description: documentation,
parameters: [
pathParam({ name: 'component', example: 'swellaby:letra' }),
...openApiQueryParams,
],
},
},
'/sonar/test_success_density/{component}/{branch}': {
get: {
summary: 'Sonar Test Success Rate (branch)',
description: documentation,
parameters: [
pathParam({ name: 'component', example: 'swellaby:letra' }),
pathParam({ name: 'branch', example: 'master' }),
...openApiQueryParams,
],
},
},
}
static defaultBadgeData = {
label: 'tests',
}
static render({ value, metric }) {
let color = 'blue'
let label = getLabel({ metric })
let message = metricCount(value)
if (metric === 'test_failures' || metric === 'test_errors') {
color = value === 0 ? 'brightgreen' : 'red'
} else if (metric === 'test_success_density') {
color = value === 100 ? 'brightgreen' : 'red'
label = 'tests'
message = `${value}%`
}
return {
label,
message,
color,
}
}
async handle({ component, metric, branch }, { server, sonarVersion }) {
const json = await this.fetch({
sonarVersion,
server,
component,
branch,
// We're using 'tests' as the metric key to provide our standard
// formatted test badge (passed, failed, skipped) that exists for other
// services. Therefore, we're exposing 'total_tests' to the user, and
// need to map that to the 'tests' metric which sonar uses to represent the
// total number of tests.
// https://docs.sonarqube.org/latest/user-guide/metric-definitions/
metricName: metric === 'total_tests' ? 'tests' : metric,
})
const metrics = this.transform({ json, sonarVersion })
return this.constructor.render({
value: metrics[metric === 'total_tests' ? 'tests' : metric],
metric,
})
}
}
export { SonarTestsSummary, SonarTests }