Skip to content

Commit

Permalink
Added badge creation
Browse files Browse the repository at this point in the history
  • Loading branch information
prollandoc committed Sep 3, 2021
1 parent 489fd9f commit 687be33
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ The output of this action is a comment on the PR to simply see if the coverage h
|-----------|----------|------------|---------|
| `token` | Yes | No default | The action token. Will be used to push or read from _coverage_ branch |
| `action` | No | `update` | The action to be executed. Either `update` or `check` |
| `files` | No | `[{"coverage": "coverage.xml", "summary": "coverage-summary.json"}]` | An array of objects representing the **clover** file that will be generated by your test suite and the **json** file that will be created by this action and uploaded to the `coverage` branch |
| `files` | No | `[{"coverage": "coverage.xml", "summary": "coverage-summary.json", "label": "Coverage", "badge": "coverage.svg"}]` | An array of objects representing the **clover** file that will be generated by your test suite, the **json** file and **SVG badge** that will be created by this action and uploaded to the `coverage` branch, and the label to display on reports and badges |

## Contributing

Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ inputs:
files:
description: 'A JSON array of objects representing the coverage files to process, and the summaries to push on the coverage branch'
required: true
default: '[{"coverage": "coverage.xml", "summary": "coverage-summary.json"}]'
default: '[{"coverage": "coverage.xml", "summary": "coverage-summary.json", "label": "Coverage", "badge": "coverage.svg"}]'
action:
description: 'Do we want to update the base coverage, or to check it has not been degraded?'
required: true
Expand Down
30 changes: 28 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10758,7 +10758,7 @@ const clone = async () => {
} else {
console.log(`Coverage branch does not exist. Creating it.`);
await execute(`git checkout --orphan ${COVERAGE_BRANCH}`, {cwd: cloneInto});
await execute(`rm -rf *`, {cwd: cloneInto});
await execute(`rm -rf .`, {cwd: cloneInto});
}

return cloneInto;
Expand Down Expand Up @@ -10827,6 +10827,24 @@ const sumCoverages = coverages => {
return out;
}

const getBadgeUrl = (coverage, label) => `https://img.shields.io/static/v1?label=${encodeURIComponent(label)}&message=${encodeURIComponent(coverage)}%25&color=${processBadgeColor(coverage)}&style=for-the-badge`;

const processBadgeColor = coverage => {
const colors = [
{ threshold: 50, color: 'red' },
{ threshold: 75, color: 'orange' },
{ threshold: 95, color: 'yellow' }
];

for (let i = 0 ; i < colors.length ; i++) {
if (coverage < colors[i].threshold) {
return colors[i].color;
}
}

return 'green';
}

const postMessageOnPullRequest = async message => {
const context = github.context;

Expand Down Expand Up @@ -10882,8 +10900,16 @@ const update = async coverages => {
const workingDir = await clone();

for (const summaryFile of Object.keys(coverages)) {
const conf = COVERAGE_FILES.find(e => e.summary === summaryFile);

console.log(`Writing ${summaryFile} report (${workingDir}/${summaryFile})`);
fs.writeFileSync(`${workingDir}/${summaryFile}`, JSON.stringify(coverages[summaryFile]));

if (conf.badge) {
const badgeContent = await fetch(getBadgeUrl(coverages[summaryFile].coverage, conf.label));

fs.writeFileSync(`${workingDir}/${conf.badge}`, await badgeContent.text());
}
}

console.log('Pushing to coverage branch');
Expand All @@ -10907,7 +10933,7 @@ const check = async coverages => {

baseCoverages[summaryFile] = await baseCoverageResult.json();

messages.push('*' + summaryFile + '* \n\n' + buildResultMessage(baseCoverages[summaryFile], coverage));
messages.push('*' + COVERAGE_FILES.find(e => e.summary === summaryFile).label + '* \n\n' + buildResultMessage(baseCoverages[summaryFile], coverage));
}

if (Object.keys(coverages).length > 1) {
Expand Down
30 changes: 28 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const clone = async () => {
} else {
console.log(`Coverage branch does not exist. Creating it.`);
await execute(`git checkout --orphan ${COVERAGE_BRANCH}`, {cwd: cloneInto});
await execute(`rm -rf *`, {cwd: cloneInto});
await execute(`rm -rf .`, {cwd: cloneInto});
}

return cloneInto;
Expand Down Expand Up @@ -140,6 +140,24 @@ const sumCoverages = coverages => {
return out;
}

const getBadgeUrl = (coverage, label) => `https://img.shields.io/static/v1?label=${encodeURIComponent(label)}&message=${encodeURIComponent(coverage)}%25&color=${processBadgeColor(coverage)}&style=for-the-badge`;

const processBadgeColor = coverage => {
const colors = [
{ threshold: 50, color: 'red' },
{ threshold: 75, color: 'orange' },
{ threshold: 95, color: 'yellow' }
];

for (let i = 0 ; i < colors.length ; i++) {
if (coverage < colors[i].threshold) {
return colors[i].color;
}
}

return 'green';
}

const postMessageOnPullRequest = async message => {
const context = github.context;

Expand Down Expand Up @@ -195,8 +213,16 @@ const update = async coverages => {
const workingDir = await clone();

for (const summaryFile of Object.keys(coverages)) {
const conf = COVERAGE_FILES.find(e => e.summary === summaryFile);

console.log(`Writing ${summaryFile} report (${workingDir}/${summaryFile})`);
fs.writeFileSync(`${workingDir}/${summaryFile}`, JSON.stringify(coverages[summaryFile]));

if (conf.badge) {
const badgeContent = await fetch(getBadgeUrl(coverages[summaryFile].coverage, conf.label));

fs.writeFileSync(`${workingDir}/${conf.badge}`, await badgeContent.text());
}
}

console.log('Pushing to coverage branch');
Expand All @@ -220,7 +246,7 @@ const check = async coverages => {

baseCoverages[summaryFile] = await baseCoverageResult.json();

messages.push('*' + summaryFile + '* \n\n' + buildResultMessage(baseCoverages[summaryFile], coverage));
messages.push('*' + COVERAGE_FILES.find(e => e.summary === summaryFile).label + '* \n\n' + buildResultMessage(baseCoverages[summaryFile], coverage));
}

if (Object.keys(coverages).length > 1) {
Expand Down

0 comments on commit 687be33

Please sign in to comment.