-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add coverage report, cache node modules
- Loading branch information
Showing
9 changed files
with
272 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
name: 'Configure Node.js' | ||
description: 'Install Node.js and install Node.js modules or restore cache' | ||
|
||
inputs: | ||
node-version: | ||
description: 'NodeJS Version' | ||
default: '18' | ||
lookup-only: | ||
description: 'If true, only checks if cache entry exists and skips download. Does not change save cache behavior' | ||
default: 'false' | ||
|
||
runs: | ||
using: 'composite' | ||
steps: | ||
- uses: actions/setup-node@v4 | ||
with: | ||
node-version: ${{ inputs.node-version }} | ||
|
||
- name: Restore Node Modules from Cache | ||
id: cache-node-modules | ||
uses: actions/cache@v4 | ||
with: | ||
path: | | ||
node_modules | ||
packages/**/node_modules | ||
!node_modules/.cache | ||
key: node-modules-${{ inputs.node-version }}-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('package.json', 'package-lock.json', '**/package-lock.json') }} | ||
lookup-only: ${{ inputs.lookup-only }} | ||
|
||
- name: Install dependencies | ||
if: steps.cache-node-modules.outputs.cache-hit != 'true' | ||
shell: bash | ||
run: npm ci |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
name: 'Parse Coverage and Post Comment' | ||
description: 'Parses a coverage report and posts a comment on a PR' | ||
inputs: | ||
lcov-file: | ||
description: 'Path to the lcov.info file' | ||
required: true | ||
title: | ||
description: 'Title of the comment' | ||
default: 'Code Coverage Report' | ||
|
||
runs: | ||
using: 'composite' | ||
steps: | ||
- name: Parse Coverage | ||
shell: bash | ||
if: github.event_name == 'pull_request' | ||
id: parse | ||
run: | | ||
./scripts/parse-coverage.js ${{ inputs.lcov-file }} > coverage-summary.txt | ||
echo "coverage-summary<<EOF" >> $GITHUB_OUTPUT | ||
cat coverage-summary.txt >> $GITHUB_OUTPUT | ||
echo "EOF" >> $GITHUB_OUTPUT | ||
- name: Find Coverage Comment | ||
if: github.event_name == 'pull_request' | ||
uses: peter-evans/find-comment@v3 | ||
id: fc | ||
with: | ||
issue-number: ${{ github.event.pull_request.number }} | ||
comment-author: 'github-actions[bot]' | ||
body-includes: '### 📊 ${{ inputs.title }}' | ||
|
||
- name: Post Coverage Comment | ||
uses: peter-evans/create-or-update-comment@v4 | ||
with: | ||
comment-id: ${{ steps.fc.outputs.comment-id }} | ||
edit-mode: replace | ||
issue-number: ${{ github.event.pull_request.number }} | ||
body: | | ||
### 📊 ${{ inputs.title }} | ||
${{ steps.parse.outputs.coverage-summary }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#!/usr/bin/env node | ||
const lcovParse = require("lcov-parse"); | ||
const fs = require("fs"); | ||
|
||
const lcovPath = process.argv[2]; | ||
const needsImprovementBelow = parseFloat( | ||
process.argv.length >= 4 ? process.argv[3] : "90" | ||
); | ||
const poorBelow = parseFloat(process.argv[4] >= 5 ? process.argv[4] : "50"); | ||
|
||
if (!lcovPath || isNaN(needsImprovementBelow) || isNaN(poorBelow)) { | ||
console.error( | ||
"Please provide the path to the lcov.info file and the 'needs-improvement-below' and 'poor-below' percentages as command-line arguments." | ||
); | ||
process.exit(1); | ||
} | ||
|
||
if (!fs.existsSync(lcovPath)) { | ||
console.error( | ||
`The file ${lcovPath} does not exist. Please provide the path to the lcov.info file.` | ||
); | ||
process.exit(1); | ||
} | ||
|
||
const outputFormat = "markdown"; | ||
|
||
if (outputFormat === "markdown") { | ||
console.log( | ||
"| File | Lines | Lines Hit / Found | Uncovered Lines | Branches |" | ||
); | ||
console.log("| --- | --- | --- | --- | --- |"); | ||
} | ||
|
||
function shortenPath(path, maxLength) { | ||
if (path.length <= maxLength) { | ||
return path; | ||
} | ||
|
||
const start = path.substring(0, maxLength / 2 - 2); // -2 for the '..' in the middle | ||
const end = path.substring(path.length - maxLength / 2, path.length); | ||
|
||
return `${start}..${end}`; | ||
} | ||
|
||
function getEmoji(lineCoverage, needsImprovementBelow, poorBelow) { | ||
if (lineCoverage >= needsImprovementBelow) { | ||
return "✅"; // white-check emoji | ||
} else if ( | ||
lineCoverage < needsImprovementBelow && | ||
lineCoverage >= poorBelow | ||
) { | ||
return "🟡"; // yellow-ball emoji | ||
} else { | ||
return "❌"; // red-x emoji | ||
} | ||
} | ||
|
||
lcovParse(lcovPath, function (err, data) { | ||
if (err) { | ||
console.error(err); | ||
} else { | ||
let totalLinesHit = 0; | ||
let totalLinesFound = 0; | ||
let totalBranchesHit = 0; | ||
let totalBranchesFound = 0; | ||
|
||
data.forEach((file) => { | ||
totalLinesHit += file.lines.hit; | ||
totalLinesFound += file.lines.found; | ||
totalBranchesHit += file.branches.hit; | ||
totalBranchesFound += file.branches.found; | ||
const relativePath = shortenPath( | ||
file.file.replace(process.cwd(), ""), | ||
50 | ||
); | ||
const lineCoverage = ((file.lines.hit / file.lines.found) * 100).toFixed( | ||
1 | ||
); | ||
const branchCoverage = ( | ||
(file.branches.hit / file.branches.found) * | ||
100 | ||
).toFixed(1); | ||
let emoji = getEmoji(lineCoverage, needsImprovementBelow, poorBelow); | ||
if (outputFormat === "markdown") { | ||
console.log( | ||
`| ${relativePath} | ${emoji} ${lineCoverage}% | ${ | ||
file.lines.hit | ||
} / ${file.lines.found} | ${ | ||
file.lines.found - file.lines.hit | ||
} | ${file.branches.found === 0 ? "-" : `${branchCoverage}%`} |` | ||
); | ||
} else { | ||
console.log( | ||
`${emoji} File: ${relativePath}, Line Coverage: ${lineCoverage}%, Branch Coverage: ${branchCoverage}%` | ||
); | ||
} | ||
}); | ||
|
||
const overallLineCoverage = ( | ||
(totalLinesHit / totalLinesFound) * | ||
100 | ||
).toFixed(1); | ||
const overallBranchCoverage = ( | ||
(totalBranchesHit / totalBranchesFound) * | ||
100 | ||
).toFixed(1); | ||
const totalUncoveredLines = totalLinesFound - totalLinesHit; | ||
const overallEmoji = getEmoji( | ||
overallLineCoverage, | ||
needsImprovementBelow, | ||
poorBelow | ||
); | ||
|
||
if (outputFormat === "markdown") { | ||
console.log( | ||
`| Overall | ${overallEmoji} ${overallLineCoverage}% | ${totalLinesHit} / ${totalLinesFound} | ${totalUncoveredLines} | ${ | ||
totalBranchesFound === 0 ? "-" : `${overallBranchCoverage}%` | ||
} |` | ||
); | ||
} else { | ||
console.log( | ||
`Overall Line Coverage: ${overallLineCoverage}%, Overall Branch Coverage: ${overallBranchCoverage}%` | ||
); | ||
} | ||
} | ||
}); |