Code Quality CLI is a tool for CI/CD, designed to run automatically whenever code is deployed. It reports Git information, and Code Coverage Results to Code Quality
for visualization.
It was built using Typescript.
First
$ git clone https://github.com/jmariomejiap/codeQualityCLI.git
install dependencies.
$ npm install
In order for the codeQuality Platform to receive and track the progress you make while developing your App you must add CodeQualityCLI
dependency to your project and enable a Continues Integration service of your choice.
The idea is to use CI to run your tests on every new commit to your repository. Once the tests are run a summary will be generated (make sure your the testing framework is configured to generate this report) and automatically sent to codeQuality.
- Add CodeQualityCLI as a dependency to the project for which you want to do codeQuality.
- Add two scripts to your package.json to generate coverage report and to send its results to codeQuality. (--coverage flag may vary across testing frameworks, example below uses jest)
"coverage-report": "npm test -— -—coverage",
"report-codeQuality": "cd ./node_modules/code-quality-cli && npm install && npm run start:dev"
CodeQualityCLI needs the output generated by istanbul (json-summary) to extract the data that will be send for visualization. You must specify the type of reporter your test framework will use.
- Add jest configuration to your package.json.
"jest": {
"coverageReporters": [
"text",
"json-summary"
]
}
- The output would be a
Post
request to the URL passed in your environment variables with a payload that looks like this.
{
"token" : "fBSSBSBS090-18d6-11e8-9XXX-6fXXXXXXX555xxx",
"commitJson" : {
"total" : {
"lines": {"total": 480, "covered": 345, "skipped": 0, "pct": 90},
"statements": { "total": 775, "covered": 623, "skipped": 19, "pct": 92.39},
"functions": {"total": 161, "covered": 85, "skipped": 0, "pct": 52.8},
"branches": { "total": 241, "covered": 149, "skipped": 11,"pct": 61.83},
"linesCovered": { "1": 82, "2": 69, "3": 37, "56": 6, "151": 3}
}
},
"author" : "mario mejia",
"branch" : "develop",
"commitHash" : "adfadfadf",
"message" : "adding improvements to code coverage. over 90%!!!",
"date" : "2018-01-08T21:20:50.388Z"
}
Since CodeQualityCLI is designed to run in the CI of your choice
Now, let's add a .yml
file and the scripts needed to send the report.
This is an example using Travis-CI
- Add a
.travis.yml
file to your project.
language: node_js
node_js:
- "9"
install:
- npm install
___
## Contributing
I welcome contributions! Please open an issue if you have any feature ideas or find any bugs. I also accept pull requests with open arms. I will go over the issues when I have time. :)
services: mongodb
script:
- npm run test
- npm run coverage-report
- npm run report-codeQuality
All set on this end.
- Login into your travis account.
- Select the your project.
- Go to Settings
- Under Environment Variables section.
- You must provide 3 environment variables to your CI/CD configuration.
CODE_QUALITY_SERVER_URL = https://<..........>/api/v1/commit
CODE_QUALITY_TOKEN = fa2331-dfadfa-1223fs
CODE_QUALITY_JSON_COVERAGE = path to coverage data. (ex. ../../coverage/coverage-summary.json)
What is CODE-QUALITY-SERVER-URL ?
The CodeQuality Application that you have already deployed and running, exposes an endpoint /api/v1/commit
.
You will need to add this to your domain so the your code coverage summary can be received and visualized
example.
https://code-quality.herokuapp.com/api/v1/commit
What is CODE-QUALITY-TOKEN ?
When you create a new project (follow instructions inside Code Quality
repo) a projectKey
is generated. This key is your CODE_QUALITY_TOKEN. Since you can manage and visualize multiple projects with CodeQuality, this token helps the platform know where to put an incoming report.
What is CODE-QUALITY-JSON-COVERAGE ?
Since CodeQualityCLI dependency is located inside your node_modules
we need to pass the correct path to the coverage folder that was generated when test were run.
Normally this path will be ../../coverage/coverage-summary.json
. Make sure the output generate is coverage-summary.json
, in case it is not, verify your jest config.
You can find more information in the CodeQuality Readme
I welcome contributions! Please open an issue if you have any feature ideas or find any bugs. I also accept pull requests with open arms. I will go over the issues when I have time. :)