-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathindex.js
More file actions
89 lines (75 loc) · 2.58 KB
/
index.js
File metadata and controls
89 lines (75 loc) · 2.58 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
const core = require('@actions/core');
const http = require('https');
const fs = require('fs')
try {
const label = core.getInput('label');
const path = core.getInput('path');
const color = core.getInput('color');
const gistFilename = core.getInput('gist-filename');
const gistId = core.getInput('gist-id');
const gistAuthToken = core.getInput('gist-auth-token');
let testReport = readFile(path);
let coveragePercentage = extractSummaryFromOpencover(testReport);
let badgeData = createBadgeData(label, coveragePercentage, color);
publishBadge(badgeData, gistFilename, gistId, gistAuthToken);
core.setOutput("badge", badgeData);
core.setOutput("percentage", coveragePercentage);
} catch (error) {
core.setFailed(error);
}
function publishBadge(badgeData, gistFilename, gistId, gistAuthToken) {
if (gistFilename == null || gistId == null || gistAuthToken == null) { //TODO: Test for null or undefined?
console.log("Could not publish badge. Gist filename, id and auth token are required to post shields.io data");
}
else {
console.log("Posting shields.io data to Gist");
postGist(badgeData, gistFilename, gistId, gistAuthToken);
}
}
function postGist(badgeData, gistFilename, gistId, gistAuthToken) {
const request = JSON.stringify({
files: {[gistFilename]: {content: JSON.stringify(badgeData)}}
});
const req = http.request(
{
host: 'api.github.com',
path: '/gists/' + gistId,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': request.length,
'User-Agent': 'simon-k',
'Authorization': 'token ' + gistAuthToken,
}
},
res => {
let body = '';
res.on('data', data => body += data);
res.on('end', () => console.log('result:' + body));
});
req.write(request);
req.end();
}
function createBadgeData(label, coveragePercentage, color) {
let badgeData = {
schemaVersion: 1,
label: label,
message: coveragePercentage + '%',
color: color
};
return badgeData;
}
function readFile(path) {
if (!fs.existsSync(path)) {
throw new Error('The file was not found at the location: "' + path + '"');
}
return fs.readFileSync(path, 'utf8');
}
function extractSummaryFromOpencover(content) {
let rx = /(?<=sequenceCoverage=")\d*\.*\d*(?=")/m;
let arr = rx.exec(content);
if (arr == null) {
throw new Error('No code coverage percentage was found in the provided opencover report. Was looking for an xml elemet named Summary with the attribute sequenceCoverage');
}
return arr[0];
}