-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
343 lines (331 loc) · 12.2 KB
/
index.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
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
const axios = require('axios')
const vl = require('vega-lite')
const vega = require('vega')
const imgur = require('imgur')
// GitHub Organization to CDash project.
const cdashProjects = {
InsightSoftwareConsortium: 'Insight',
KitwareMedical: 'Insight',
SimpleITK: 'SimpleITK',
SuperElastix: 'SuperElastix'
}
// GitHub Organization to CDash instance
const cdashInstances = {
InsightSoftwareConsortium: 'https://open.cdash.org',
KitwareMedical: 'https://open.cdash.org',
SimpleITK: 'https://open.cdash.org',
SuperElastix: 'http://trunk.cdash.org'
}
// GitHub Status Context that generate CDash builds
const contextWithCTestBuildsPrefix = [
'ci/circleci', // ITKSoftwareGuide, ITK, SimpleITK builds
'continuous-integration/jenkins/pr-merge', // SuperElastix
'continuous-integration/jenkins/branch', // SuperElastix
'continuous-integration/travis-ci/pr', // SuperElastix
'continuous-integration/appveyor/pr', // ITKTubeTK
'ITK.Linux', // ITK Azure Pipelines
'ITK.macOS', // ITK Azure Pipelines
'ITK.Windows', // ITK Azure Pipelines
'ITK.Linux.Python', // ITK Azure Pipelines
'ITK.macOS.Python', // ITK Azure Pipelines
'ITK.Windows.Python', // ITK Azure Pipelines
'InsightSoftwareConsortium.', // ITKExamples, ITK modules Azure Pipelines
'SimpleITK.', // SimpleITK Azure Pipelines
'KitwareMedical.', // ITKTubeTK
'Build, test, package / build-test-cxx' // GitHub Actions for remote modules
]
function contextHasCTestBuild (context) {
return contextWithCTestBuildsPrefix.filter((withCTest) => context.startsWith(withCTest)).length > 0
}
function sleep (ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
module.exports = app => {
app.on(['status'], check)
app.on(['check_suite.rerequested'], check)
app.on(['check_run.rerequested'], check)
async function check (context) {
context.log(`Received payload id: ${context.payload.id}`)
// context.log(context.payload)
let sha = null
if (context.event === 'status') {
sha = context.payload.sha
} else if (context.event === 'check_run.rerequested') {
sha = context.payload.check_run.head_sha
} else {
// check_suite.rerequested
sha = context.payload.check_suite.head_sha
}
const organization = context.payload.organization.login
const headShaShort = sha.substr(0, 7)
const cdashProject = cdashProjects[organization]
const cdashInstance = cdashInstances[organization]
const cdashUrl = `${cdashInstance}/index.php?project=${cdashProject}&filtercount=1&showfilters=0&field1=revision&compare1=63&value1=${headShaShort}&showfeed=0`
let postCDashLinkStatus = false
if (context.event === 'status') {
if (contextHasCTestBuild(context.payload.context)) {
const description = context.payload.description.toLowerCase()
if (!description.includes('cdash')) {
postCDashLinkStatus = true
}
}
} else {
postCDashLinkStatus = true
}
if (postCDashLinkStatus) {
let hasFailedBuild = false
let hasBuild = false
const statusesForRef = await context.github.repos.listStatusesForRef(context.repo({ ref: sha }))
const pendingBuilds = {}
let successAndFailureCount = 0
statusesForRef.data.forEach((status) => {
if (contextHasCTestBuild(status.context)) {
if (status.state === 'pending') {
pendingBuilds[status.context] = true
}
if (status.state !== 'pending') {
hasBuild = true
}
if (status.state === 'error' || status.state === 'failure') {
hasFailedBuild = true
successAndFailureCount++
}
if (status.state === 'success') {
successAndFailureCount++
}
}
})
statusesForRef.data.forEach((status) => {
if (contextHasCTestBuild(status.context)) {
if (status.state !== 'pending') {
pendingBuilds[status.context] = false
}
}
})
let buildsArePending = false
for (const build in pendingBuilds) {
if (pendingBuilds[build]) {
buildsArePending = true
break
}
}
if (!hasBuild) {
context.log('No builds have completed yet.')
return
}
let buildsResponse = {}
for (let attempt in [1, 2, 3, 4]) { // eslint-disable-line
buildsResponse = await axios.get(`${cdashInstance}/api/v1/index.php?project=${cdashProject}&filtercount=1&showfilters=0&field1=revision&compare1=63&value1=${headShaShort}`)
// Wait for CDash to process the initial build
if (buildsResponse.data.buildgroups.length > 0) {
break
}
await sleep(2000)
}
const data = buildsResponse.data
const metricExtrema = {
configureErrors: 0,
configureWarnings: 0,
buildErrors: 0,
buildWarnings: 0,
testsFailed: 0,
testsPassed: 0
}
const metricSums = {
configure: 0,
buildErrors: 0,
buildWarnings: 0,
testsFailed: 0
}
data.buildgroups[0].builds.forEach((build) => {
metricSums.configure += build.configure.error
metricSums.configure += build.configure.warning
metricSums.buildErrors += build.compilation.error
metricSums.buildWarnings += build.compilation.warning
metricSums.testsFailed += build.test.fail
if (build.configure.error > metricExtrema.configureErrors) {
metricExtrema.configureErrors = build.configure.error
}
if (build.configure.warning > metricExtrema.configureWarningcs) {
metricExtrema.configureWarningcs = build.configure.warning
}
if (build.compilation.error > metricExtrema.buildErrors) {
metricExtrema.buildErrors = build.compilation.error
}
if (build.compilation.warning > metricExtrema.buildWarningcs) {
metricExtrema.buildWarningcs = build.compilation.warning
}
if (build.test.fail > metricExtrema.testsFailed) {
metricExtrema.testsFailed = build.test.fail
}
if (build.test.pass > metricExtrema.testsPassed) {
metricExtrema.testsPassed = build.test.pass
}
})
const vegaDataValues = []
data.buildgroups[0].builds.forEach((build) => {
const site = `${build.site} - ${build.buildplatform}`
vegaDataValues.push({
site: site,
metricName: 'Configure Errors',
metricValue: build.configure.error,
metricIndicator: build.configure.error ? -1.0 * build.configure.error / metricExtrema.configureErrors : 0.0
})
vegaDataValues.push({
site: site,
metricName: 'Configure Warnings',
metricValue: build.configure.warning,
metricIndicator: build.configure.warning ? -1.0 * build.configure.warning / metricExtrema.configureWarnings : 0.0
})
vegaDataValues.push({
site: site,
metricName: 'Build Errors',
metricValue: build.compilation.error,
metricIndicator: build.compilation.error ? -1.0 * build.compilation.error / metricExtrema.buildErrors : 0.0
})
vegaDataValues.push({
site: site,
metricName: 'Build Warnings',
metricValue: build.compilation.warning,
metricIndicator: build.compilation.warning ? -1.0 * build.compilation.warning / metricExtrema.buildWarnings : 0.0
})
vegaDataValues.push({
site: site,
metricName: 'Tests Failed',
metricValue: build.test.fail,
metricIndicator: build.test.fail ? -1.0 * build.test.fail / metricExtrema.testsFailed : 0.0
})
vegaDataValues.push({
site: site,
metricName: 'Tests Passed',
metricValue: build.test.pass,
metricIndicator: build.test.pass ? 1.0 * build.test.pass / metricExtrema.testsPassed : 0.0
})
vegaDataValues.push({
site: site,
metricName: 'Time',
metricValue: build.time,
metricIndicator: 0.0
})
})
// const revision = data.filterdata.filters[0].value
const vegaLiteSpec = {
$schema: 'https://vega.github.io/schema/vega-lite/v3.json',
width: 800,
data: {
values: vegaDataValues,
name: 'source'
},
encoding: {
y: { field: 'site', type: 'ordinal', axis: { title: 'Site' } },
x: {
field: 'metricName',
type: 'nominal',
sort: ['Configure Warnings', 'Configure Errors', 'Build Warnings', 'Build Errors', 'Tests Failed', 'Tests Passed', 'Time'],
axis: { title: null, labelAngle: 0, orient: 'bottom' }
}
},
layer: [{
mark: 'rect',
encoding: {
color: {
field: 'metricIndicator',
type: 'quantitative',
legend: null,
scale: { domain: [-1.0, 1.0], scheme: 'pinkyellowgreen' }
}
}
}, {
mark: 'text',
encoding: {
text: { field: 'metricValue', type: 'ordinal' },
color: {
condition: { test: 'datum.metricIndicator > -0.3 && datum.metricIndicator < 0.3', value: 'black' },
value: 'white'
}
}
}],
config: {
scale: { bandPaddingInner: 0, bandPaddingOuter: 0 },
text: { baseline: 'middle' }
}
}
const { spec } = vl.compile(vegaLiteSpec)
// create a new view instance for a given Vega JSON spec
const view = new vega.View(vega.parse(spec))
.renderer('none')
.initialize()
// generate a static PNG image
const canvas = await view.toCanvas()
// process node-canvas instance
const dataUrl = canvas.toDataURL()
const regex = /^data:.+\/(.+);base64,(.*)$/
const base64 = dataUrl.match(regex)[2]
const uploadResponse = await imgur.uploadBase64(base64)
const imageUrl = uploadResponse.data.link
let summary = 'Totals: '
if (metricSums.configure) {
summary += `**${metricSums.configure} configuration issues**, `
} else {
summary += `${metricSums.configure} configuration issues, `
}
if (metricSums.buildErrors) {
summary += `**${metricSums.buildErrors} build errors**, `
} else {
summary += `${metricSums.buildErrors} build errors, `
}
if (metricSums.buildWarnings) {
summary += `**${metricSums.buildWarnings} build warnings**, `
} else {
summary += `${metricSums.buildWarnings} build warnings, `
}
if (metricSums.testsFailed) {
summary += `**${metricSums.testsFailed} test failures**.`
} else {
summary += `${metricSums.testsFailed} test failures.`
}
const checkParameters = {
name: 'CDash',
head_sha: sha,
details_url: cdashUrl,
output: {
title: 'Build analysis summary',
summary,
text: `[![CDash build analysis summary](${imageUrl})](${cdashUrl}).`
// images: [{ alt: "CDash build analysis summary", image_url: imageUrl, caption: "Build summary" }]
}
}
let checkStatus = 'completed'
if (buildsArePending) {
checkStatus = 'in_progress'
} else {
checkParameters.completed_at = new Date()
if (hasFailedBuild) {
checkParameters.conclusion = 'failure'
} else {
checkParameters.conclusion = 'success'
}
}
checkParameters.status = checkStatus
const existingChecks = await context.github.checks.listForRef(context.repo({
check_name: 'CDash',
ref: sha
}))
const createNewCheck = existingChecks.data.total_count < 1
if (createNewCheck) {
// Only create the check when a check does not exist and this is the
// initial success or failure
if (successAndFailureCount >= 1) {
return context.github.checks.create(context.repo(checkParameters))
} else {
context.log('Skipping check creation')
}
} else {
checkParameters.check_run_id = existingChecks.data.check_runs[0].id
return context.github.checks.update(context.repo(checkParameters))
}
} else {
context.log('No CDash status post required.')
}
}
}