Skip to content

Commit 2473421

Browse files
committed
ci: stylizing the tests report
1 parent 398aa51 commit 2473421

File tree

1 file changed

+47
-11
lines changed

1 file changed

+47
-11
lines changed
Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,66 @@
1-
import { ciLocalRun, getRepoOwner } from "./helpers.js";
1+
import { ciLocalRun, createPrComment } from "./helpers.js";
22

33
/**
44
* Generates a new comment on the PR that triggered the workflow with the
55
* report of the tests runned by 'JEST' over the actions scripts
66
*/
77
export default async (github, context, steps) => {
88
const isLocalRun = ciLocalRun(context);
9-
console.log(`CTX: ${JSON.stringify(context, null, 2)}`);
9+
const prNumber = context.payload.number;
10+
1011
const marker = 'to show where the warning was created)';
1112
const output = steps.run_tests.outputs.tests_report;
1213
const sanitized = output.split(marker);
13-
const msg = (sanitized.length > 1) ? sanitized[1] : sanitized[0];
14+
15+
let msg = getTitleMsg();
16+
msg += (sanitized.length > 1) ? sanitized[1] : sanitized[0];
1417

15-
const prNumber = context.payload.number;
18+
const stylizedMsg = formatTestOutput(msg);
1619

1720
if (!isLocalRun && sanitized.length >= 1) {
18-
github.rest.issues.createComment({
19-
owner: context.repo.owner,
20-
repo: context.repo.repo,
21-
issue_number: prNumber,
22-
body: msg,
23-
});
21+
createPrComment(github, context, prNumber, stylizedMsg);
2422
} else {
2523
if (!isLocalRun)
2624
core.setFailed('No tests report data available.');
2725
else
2826
console.log(`PR message: ${msg}`);
2927
}
30-
};
28+
};
29+
30+
/**
31+
* @return {string} a title message for the automated comment on the target PR
32+
* that decorates such comment with a nice title
33+
*/
34+
function getTitleMsg() {
35+
return "This message is autogenerated, because there's changes in the " +
36+
"'actions-scripts' folder, which contains JS code that is used for by some " +
37+
"automations in our workflows.\n"
38+
}
39+
40+
/**
41+
* Gives formats and stylizes the generated tests reports of 'JEST' to be shown
42+
* on the PR comment to notify the user about the test results.
43+
* @param {string} textMsg the input data with the tests results as a report
44+
* @returns {string} the tests report formatted and stylized
45+
*/
46+
export function formatTestOutput(textMsg) {
47+
return textMsg
48+
.split('\n')
49+
.map(line => {
50+
if (line.includes('✓')) {
51+
return `\t- ✅ ${line.trim()}`;
52+
}
53+
if (line.includes('✗')) {
54+
return `\t- ❌ ${line.trim()}`;
55+
}
56+
if (line.startsWith('PASS')) {
57+
return `🎉 ${line}`;
58+
}
59+
if (line.startsWith('FAIL')) {
60+
return `💥 ${line}`;
61+
}
62+
return `\t ${line.trim()}`;
63+
})
64+
.join('\n');
65+
}
66+

0 commit comments

Comments
 (0)