CodeclimateDiff
This tool lets you see how changes in your branch will affect the code quality (what issues you've added, fixed, and what issues are outstanding in the files you've touched that could be fixed while you're in the area.)
It runs the https://hub.docker.com/r/codeclimate/codeclimate docker image under the hood, which pays attention to all the normal Code Climate configurations.
Initial setup
-
Make sure docker is installed and running
-
Add a
.codeclimate.yml
config file eg:--- version: "2" plugins: rubocop: enabled: true channel: rubocop-1-36-0 reek: enabled: true exclude_patterns: - config/ - db/ - dist/ - features/ - public/ - "**/node_modules/" - script/ - "**/spec/" - "**/test/" - "**/tests/" - Tests/ - "**/vendor/" - "**/*_test.go" - "**/*.d.ts" - "**/*.min.js" - "**/*.min.css" - "**/__tests__/" - "**/__mocks__/" - "/.gitlab/" - coverage/ . # simple cov
-
Add a
.reek.yml
config file eg:See https://github.com/troessner/reek#working-with-rails
detectors: IrresponsibleModule: enabled: false NilCheck: enabled: false ManualDispatch: enabled: false LongParameterList: max_params: 4 # defaults to 3. You want this number realistic but stretchy so we can move it down TooManyStatements: max_statements: 10 # defaults to 5. You want this number realistic but stretchy so we can move it down UtilityFunction: public_methods_only: true UncommunicativeVariableName: accept: - e directories: "app/controllers": IrresponsibleModule: enabled: false NestedIterators: max_allowed_nesting: 2 UnusedPrivateMethod: enabled: false InstanceVariableAssumption: enabled: false "app/helpers": IrresponsibleModule: enabled: false UtilityFunction: enabled: false FeatureEnvy: enabled: false "app/mailers": InstanceVariableAssumption: enabled: false "app/models": InstanceVariableAssumption: enabled: false
-
Add a
.codeclimate_diff.yml
configuration filemain_branch_name: master # defaults to main threshold_to_run_on_all_files: 8 # when you reach a certain number of files changed, it becomes faster to analyze all files rather than analyze them one by one.
-
Install the gem
Add this line to your application's Gemfile:
gem 'codeclimate_diff', github: 'boost/codeclimate_diff'
Install the gem:
$ bundle install
Then generate the executable:
$ bundle binstubs codeclimate_diff
Add codeclimate_diff_baseline.json to .gitignore
Usage
-
Create a feature branch for your work
-
Do some work
-
Check if you've added any issues (about 10 secs per code file changed on your branch):
# runs on each file changed in your branch (about 10 secs per code file changed on your branch) ./bin/codeclimate_diff # baseline is now generated on first run, to generate new baseline, delete the existing. OR # filters the changed files in your branch futher by a grep pattern ./bin/codeclimate_diff --pattern places OR # only shows the new and fixed issues ./bin/codeclimate_diff --new-only OR # always analyzes all files rather than the changed files one by one, even if below the 'threshold_to_run_on_all_files' setting. # NOTE: similar code issues will only work 100% correctly if you use this setting (otherwise it might miss a similarity with a file you didn't change and think you fixed it) ./bin/codeclimate_diff --all
-
Now you have time to fix the issues, horray!
Setting it up to download the latest baseline from your CI Pipeline (Gitlab only)
Gitlab has a codeclimate template you can add to your pipeline that runs on main builds and then runs on your branch and outputs a difference (see https://docs.gitlab.com/ee/ci/testing/code_quality.html).
With a few tweaks to your CI configuration, we can pull down the main build baseline from the job so we don't have to do it locally.
-
In your Gitlab CI Configuration where you include the
Code-Quality.gitlab-ci.yml
template:include: - template: Code-Quality.gitlab-ci.yml # add this bit: code_quality: artifacts: paths: [gl-code-quality-report.json] . # without this, the artifact can't be downloaded
-
Add your project settings to the
.codecimate_diff.yml
configuration file:main_branch_name: main # settings to pull down the baseline from the pipeline in Gitlab before checking your branch gitlab: download_baseline_from_pipeline: true # If false or excluded, you will need to generate the baseline manually project_id: '<project id>' host: https://gitlab.digitalnz.org/ baseline_filename: 'gl-code-quality-report.json'
-
Create a personal access token with
read_api
access and save it in theCODECLIMATE_DIFF_GITLAB_PERSONAL_ACCESS_TOKEN
env variable
Now when you run it on the changed files in your branch, it will download the latest baseline first!
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and the created tag, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/codeclimate_diff.