Skip to content

Commit

Permalink
Fail builds based on code coverage (#7)
Browse files Browse the repository at this point in the history
* add coverage generation commands

* use local directory for action

* add actions checkout

* remove name

* update workflow with coverage comparision

* comment master branch running

* add minimun coverage ration checks

* update minimum-coverage-ratio build

* use default value as 10
  • Loading branch information
vitaliimelnychuk authored Dec 31, 2020
1 parent 5e4a18c commit c51e59d
Show file tree
Hide file tree
Showing 7 changed files with 749 additions and 32 deletions.
86 changes: 73 additions & 13 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,80 @@
on: [pull_request]
name: Code Coverage

on:
push:
branches:
- master
pull_request:

jobs:
generate_coverage_job:
base_branch_cov:
runs-on: ubuntu-latest
name: Example of using Code Coverage action
steps:
- name: Generate Code Coverage report
id: code_coverage
uses: barecheck/[email protected]
- uses: actions/checkout@v2
#TODO: uncommend after merge to master
# with:
# ref: master
- name: Use Node.js 12.13.1
uses: actions/setup-node@v1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
lcov-file: './data/lcov.info'
head-lcov-file: './data/head-lcov.info'
node-version: 12.13.1
- name: Cache node modules
uses: actions/cache@v1
with:
path: node_modules
key: node_modules-v1-${{ hashFiles('yarn.lock') }}
# loading an older version is fine here, since it will get an npm install
restore-keys: |
node_modules-
- name: Install dependencies
run: yarn

- name: Get the output percentage
run: echo "The percentage ${{ steps.code_coverage.outputs.percentage }}"
- name: Run test coverage
run: yarn coverage-lcov

- name: Get the output diff
run: echo "The percentage diff from head branch ${{ steps.code_coverage.outputs.diff }}"
- name: Upload code coverage for ref branch
uses: actions/upload-artifact@v2
with:
name: ref-lcov.info
path: ./coverage/lcov.info

checks:
runs-on: ubuntu-latest
needs: base_branch_cov
steps:
- uses: actions/checkout@v2
- name: Use Node.js 12.13.1
uses: actions/setup-node@v1
with:
node-version: 12.13.1
- name: Cache node modules
uses: actions/cache@v1
with:
path: node_modules
key: node_modules-v1-${{ hashFiles('yarn.lock') }}
# loading an older version is fine here, since it will get an npm install
restore-keys: |
node_modules-
- name: Download math result for job 2
uses: actions/download-artifact@v2
with:
name: ref-lcov.info

- name: Install dependencies
run: yarn

- name: Run test coverage
run: yarn coverage-lcov

# Testing beta of code coverage checks
# This part can be removed or optimized once barecheck code coverage will be out of beta
- name: Generate Code Coverage report
id: code-coverage
uses: ./
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
lcov-file: './coverage/lcov.info'
head-lcov-file: './lcov.info'
minimum-coverage-ratio: 0
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ inputs:
head-lcov-file:
description: 'Code coverage head branch report to compare with'
required: true
default: null
default: ''
minimum-coverage-ratio:
description: 'Minimum coverage ratio that would to be considered as a difference between based and head commits'
required: false
default: ''
outputs:
percentage:
description: 'Total Percentage coverage'
Expand Down
26 changes: 25 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5900,6 +5900,29 @@ function wrappy (fn, cb) {
}


/***/ }),

/***/ 880:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {

const core = __webpack_require__(186);

const checkCoverageRation = (coverageDiff) => {
const minCoverageRatio =
parseInt(core.getInput('minimum-coverage-ratio'), 10) || 0;

const coverageDiffAlert = coverageDiff + minCoverageRatio;

if (coverageDiffAlert < 0) {
throw new Error(`Your coverage is ${coverageDiffAlert}%`);
}
};

module.exports = {
checkCoverageRation
};


/***/ }),

/***/ 396:
Expand Down Expand Up @@ -5931,11 +5954,11 @@ module.exports = {
/***/ ((__unused_webpack_module, __unused_webpack_exports, __webpack_require__) => {

const fs = __webpack_require__(747);
const path = __webpack_require__(622);
const core = __webpack_require__(186);

const lcov = __webpack_require__(318);
const { sendComment } = __webpack_require__(396);
const { checkCoverageRation } = __webpack_require__(880);

async function main() {
const token = core.getInput('github-token');
Expand Down Expand Up @@ -5965,6 +5988,7 @@ async function main() {
const diff = basePercentage - headPercentage;

sendComment(token, diff, basePercentage);
checkCoverageRation(diff);

core.setOutput('percentage', basePercentage);
core.setOutput('diff', diff);
Expand Down
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@
"eslint": "^7.16.0",
"eslint-plugin-import": "^2.22.1",
"mocha": "^8.2.1",
"nyc": "^15.1.0",
"prettier": "^2.2.1",
"proxyquire": "^2.1.3",
"sinon": "^9.2.2"
},
"scripts": {
"format-check": "prettier --check src",
"build": "ncc build src/index.js --license licenses.txt",
"lint": "eslint --max-warnings=0 src",
"check-all": "yarn format-check && yarn lint",
"format": "prettier --write src",
"test": "mocha tests",
"build": "ncc build src/index.js --license licenses.txt"
"coverage": "nyc yarn test",
"coverage-lcov": "nyc --reporter=lcov yarn test",
"format-check": "prettier --check src",
"check-all": "yarn format-check && yarn lint"
},
"repository": {
"type": "git",
Expand Down
16 changes: 16 additions & 0 deletions src/features/minCoverageRatio.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const core = require('@actions/core');

const checkCoverageRation = (coverageDiff) => {
const minCoverageRatio =
parseInt(core.getInput('minimum-coverage-ratio'), 10) || 0;

const coverageDiffAlert = coverageDiff + minCoverageRatio;

if (coverageDiffAlert < 0) {
throw new Error(`Your coverage is ${coverageDiffAlert}%`);
}
};

module.exports = {
checkCoverageRation
};
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const fs = require('fs');
const path = require('path');
const core = require('@actions/core');

const lcov = require('./lcov');
const { sendComment } = require('./github');
const { checkCoverageRation } = require('./features/minCoverageRatio');

async function main() {
const token = core.getInput('github-token');
Expand Down Expand Up @@ -33,6 +33,7 @@ async function main() {
const diff = basePercentage - headPercentage;

sendComment(token, diff, basePercentage);
checkCoverageRation(diff);

core.setOutput('percentage', basePercentage);
core.setOutput('diff', diff);
Expand Down
Loading

0 comments on commit c51e59d

Please sign in to comment.