-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
parse.js
332 lines (270 loc) · 9.06 KB
/
parse.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
const core = require('@actions/core');
const { getPathToFile, getContentFile, getCoverageColor } = require('./utils');
// return true if "covergae file" include all special words
const isValidCoverageContent = (data) => {
if (!data || !data.length) {
return false;
}
const wordsToInclude = [
'coverage: platform',
'Stmts',
'Miss',
'Cover',
'TOTAL',
];
return wordsToInclude.every((w) => data.includes(w));
};
// return full html coverage report and coverage percenatge
const getCoverageReport = (options) => {
const { covFile, covXmlFile } = options;
if (!covXmlFile) {
try {
const covFilePath = getPathToFile(covFile);
const content = getContentFile(covFilePath);
const coverage = getTotalCoverage(content);
const isValid = isValidCoverageContent(content);
if (content && !isValid) {
// prettier-ignore
core.error(`Coverage file "${covFilePath}" has bad format or wrong data`);
}
if (content && isValid) {
const html = toHtml(content, options);
const total = getTotal(content);
const warnings = getWarnings(content);
const color = getCoverageColor(total ? total.cover : '0');
return { html, coverage, color, warnings };
}
} catch (error) {
core.error(`Generating coverage report. ${error.message}`);
}
}
return { html: '', coverage: '0', color: 'red', warnings: 0 };
};
// get actual lines from coverage-file
const getActualLines = (data) => {
if (!data || !data.length) {
return null;
}
const lines = data.split('\n');
const startIndex = lines.findIndex((l) => l.includes('coverage: platform'));
const endIndex = lines.findIndex((l) => l.includes('TOTAL '));
if (startIndex === -1) {
return null;
}
return lines.slice(startIndex + 3, endIndex - 1);
};
// get total line from coverage-file
const getTotal = (data) => {
if (!data || !data.length) {
return null;
}
const lines = data.split('\n');
const line = lines.find((l) => l.includes('TOTAL '));
return parseTotalLine(line);
};
// get number of warnings from coverage-file
const getWarnings = (data) => {
if (!data || !data.length) {
return null;
}
const WARNINGS_KEY = ' warnings in ';
if (!data.includes(WARNINGS_KEY)) {
return 0;
}
const line = data.split('\n').find((l) => l.includes(WARNINGS_KEY));
const lineArr = line.split(' ');
const indexOfWarnings = lineArr.findIndex((i) => i === 'warnings');
return parseInt(lineArr[indexOfWarnings - 1]);
};
// parse one line from coverage-file
const parseOneLine = (line) => {
if (!line) {
return null;
}
const parsedLine = line.split(' ').filter((l) => l);
if (parsedLine.length < 4) {
return null;
}
const lastItem = parsedLine[parsedLine.length - 1];
const isFullCoverage = lastItem === '100%';
const cover = isFullCoverage
? '100%'
: parsedLine[parsedLine.length - 2].trimStart();
const missing = isFullCoverage
? null
: parsedLine[parsedLine.length - 1] &&
parsedLine[parsedLine.length - 1].split(', ');
return {
name: parsedLine[0],
stmts: parsedLine[1].trimStart(),
miss: parsedLine[2].trimStart(),
cover,
missing,
};
};
// parse total line from coverage-file
const parseTotalLine = (line) => {
if (!line) {
return null;
}
const parsedLine = line.split(' ').filter((l) => l);
if (parsedLine.length < 4) {
return null;
}
return {
name: parsedLine[0],
stmts: parsedLine[1].trimStart(),
miss: parsedLine[2].trimStart(),
cover: parsedLine[parsedLine.length - 1].trimStart(),
};
};
// parse coverage-file
const parse = (data) => {
const actualLines = getActualLines(data);
if (!actualLines) {
return null;
}
return actualLines.map(parseOneLine);
};
// collapse all lines to folders structure
const makeFolders = (coverage, options) => {
const folders = {};
for (const line of coverage) {
const parts = line.name.replace(options.prefix, '').split('/');
const folder = parts.slice(0, -1).join('/');
folders[folder] = folders[folder] || [];
folders[folder].push(line);
}
return folders;
};
// gets total coverage in percentage
const getTotalCoverage = (data) => {
const total = getTotal(data);
return total ? total.cover : '0';
};
// convert all data to html output
const toHtml = (data, options, dataFromXml = null) => {
const {
badgeTitle,
title,
hideBadge,
hideReport,
reportOnlyChangedFiles,
removeLinkFromBadge,
} = options;
const table = hideReport ? '' : toTable(data, options, dataFromXml);
const total = dataFromXml ? dataFromXml.total : getTotal(data);
const color = getCoverageColor(total.cover);
const onlyChnaged = reportOnlyChangedFiles ? '• ' : '';
const readmeHref = `${options.repoUrl}/blob/${options.commit}/README.md`;
const badge = `<img alt="${badgeTitle}" src="https://img.shields.io/badge/${badgeTitle}-${total.cover}25-${color}.svg" />`;
const badgeWithLink = removeLinkFromBadge
? badge
: `<a href="${readmeHref}">${badge}</a>`;
const badgeHtml = hideBadge ? '' : badgeWithLink;
const reportHtml = hideReport
? ''
: `<details><summary>${title} ${onlyChnaged}</summary>${table}</details>`;
return `${badgeHtml}${reportHtml}`;
};
// make html table from coverage-file
const toTable = (data, options, dataFromXml = null) => {
const coverage = dataFromXml ? dataFromXml.coverage : parse(data);
const { reportOnlyChangedFiles, changedFiles } = options;
if (!coverage) {
core.warning(`Coverage file not well formed`);
return null;
}
const totalLine = dataFromXml ? dataFromXml.total : getTotal(data);
options.hasMissing = coverage.some((c) => c.missing);
core.info(`Generating coverage report`);
const headTr = toHeadRow(options);
const totalTr = toTotalRow(totalLine, options);
const folders = makeFolders(coverage, options);
const rows = Object.keys(folders)
.sort()
.filter((folderPath) => {
if (!reportOnlyChangedFiles) {
return true;
}
const allFilesInFolder = Object.values(folders[folderPath]).map(
(f) => f.name
);
folders[folderPath] = folders[folderPath].filter((f) =>
changedFiles.all.some((c) => c.includes(f.name))
);
const fileExistsInFolder = allFilesInFolder.some((f) =>
changedFiles.all.some((c) => c.includes(f))
);
return fileExistsInFolder;
})
.reduce(
(acc, key) => [
...acc,
toFolderTd(key, options),
...folders[key].map((file) => toRow(file, key !== '', options)),
],
[]
);
const hasLines = rows.length > 0;
const isFilesChanged =
reportOnlyChangedFiles && !hasLines
? `<i>report-only-changed-files is enabled. No files were changed during this commit :)</i>`
: '';
// prettier-ignore
return `<table>${headTr}<tbody>${rows.join('')}${totalTr}</tbody></table>${isFilesChanged}`;
};
// make html head row - th
const toHeadRow = (options) => {
const lastTd = options.hasMissing ? '<th>Missing</th>' : '';
return `<tr><th>File</th><th>Stmts</th><th>Miss</th><th>Cover</th>${lastTd}</tr>`;
};
// make html row - tr
const toRow = (item, indent = false, options) => {
const { stmts, miss, cover } = item;
const name = toFileNameTd(item, indent, options);
const missing = toMissingTd(item, options);
const lastTd = options.hasMissing ? `<td>${missing}</td>` : '';
// prettier-ignore
return `<tr><td>${name.replace(/__/g, '\\_\\_')}</td><td>${stmts}</td><td>${miss}</td><td>${cover}</td>${lastTd}</tr>`;
};
// make summary row - tr
const toTotalRow = (item, options) => {
const { name, stmts, miss, cover } = item;
const emptyCell = options.hasMissing ? '<td> </td>' : '';
return `<tr><td><b>${name}</b></td><td><b>${stmts}</b></td><td><b>${miss}</b></td><td><b>${cover}</b></td>${emptyCell}</tr>`;
};
// make fileName cell - td
const toFileNameTd = (item, indent = false, options) => {
const relative = item.name.replace(options.prefix, '');
const href = `${options.repoUrl}/blob/${options.commit}/${options.pathPrefix}${relative}`;
const parts = relative.split('/');
const last = parts[parts.length - 1];
const space = indent ? ' ' : '';
return `${space}<a href="${href}">${last}</a>`;
};
// make folder row - tr
const toFolderTd = (path, options) => {
if (path === '') {
return '';
}
const colspan = options.hasMissing ? 5 : 4;
return `<tr><td colspan="${colspan}"><b>${path}</b></td></tr>`;
};
// make missing cell - td
const toMissingTd = (item, options) => {
if (!item.missing || !item.missing.length) {
return ' ';
}
return item.missing
.map((range) => {
const [start, end = start] = range.split('-');
const fragment = start === end ? `L${start}` : `L${start}-L${end}`;
const relative = item.name;
const href = `${options.repoUrl}/blob/${options.commit}/${options.pathPrefix}${relative}#${fragment}`;
const text = start === end ? start : `${start}–${end}`;
return `<a href="${href}">${text}</a>`;
})
.join(', ');
};
module.exports = { getCoverageReport, getCoverageColor, toTable, toHtml };