Skip to content

Commit

Permalink
Merge pull request finos#213 from CyberCitizen01/203-implement-code-c…
Browse files Browse the repository at this point in the history
…overage-reports-on-pull-requests

Generate code coverage reports on PRs
  • Loading branch information
coopernetes authored Jan 24, 2024
2 parents 0edfd4e + f0e0f7d commit 1bb1e21
Show file tree
Hide file tree
Showing 9 changed files with 1,207 additions and 109 deletions.
37 changes: 36 additions & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ jobs:
node-version: [18.x]
mongodb-version: [4.4]

permissions:
pull-requests: write

steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
with:
fetch-depth: 0

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
Expand All @@ -36,5 +41,35 @@ jobs:
run: npm i

- name: Test
run: npm test
id: test
run: |
npm run test-coverage-ci || echo "Silently ignoring coverage threshold limit..."
- name: Check if a valid lcov.info file is generated
id: check-lcov-non-empty
run: |
if [ -s "./coverage/lcov.info" ]; then
echo "lcov.info is not empty."
echo "isNotEmpty=true" >> $GITHUB_OUTPUT
else
echo "lcov.info is empty."
echo "isNotEmpty=false" >> $GITHUB_OUTPUT
fi
- name: Install LCOV
if: ${{ steps.check-lcov-non-empty.outputs.isNotEmpty == 'true'}}
run: |
sudo apt-get update && sudo apt-get install --assume-yes lcov
- name: Report code coverage
if: ${{ steps.check-lcov-non-empty.outputs.isNotEmpty == 'true' }}
uses: zgosalvez/github-actions-report-lcov@v3
with:
coverage-files: ./coverage/lcov.info
github-token: ${{ secrets.GITHUB_TOKEN }}
update-comment: true

# - name: Exit if coverage condition not met
# if: ${{ steps.test.outputs.exit_code }} != 0
# run: exit ${{ steps.test.outputs.exit_code }}

41 changes: 41 additions & 0 deletions nyc.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* eslint-disable max-len */
'use strict';

const { execFileSync } = require('child_process');

let opts = {
branches: 80,
lines: 80,
functions: 80,
statements: 80,
};

// Only generate coverage report for changed files in PR
// see: https://github.com/actions/checkout/issues/438#issuecomment-1446882066
// https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
if (process.env.GITHUB_BASE_REF !== undefined) {
console.log('Generating coverage report for changed files...');
try {
const baseRef = execFileSync('git', [
'rev-parse',
`origin/${process.env.GITHUB_BASE_REF}`,
])
.toString()
.replace('\n', '');
const headRef = process.env.GITHUB_SHA;
const stdout = execFileSync('git', [
'diff',
'--name-only',
`${baseRef}..${headRef}`,
]).toString();
opts = {
...opts,
include: stdout.split('\n'),
};
} catch (error) {
console.log('Error: ', error);
}
}

console.log('nyc config: ', opts);
module.exports = opts;
Loading

0 comments on commit 1bb1e21

Please sign in to comment.