GitLab code climate not recognizing .rubocop.yml config

205 Views Asked by At

I am attempting to enable code quality reports for merge requests for a Ruby on Rails application managed in GitLab.

In my .gitlab-ci.yml file I have added the code quality template.

.gitlab-ci.yml:

include:
  - template: Jobs/SAST.gitlab-ci.yml
  - template: Jobs/Secret-Detection.gitlab-ci.yml
  - template: Code-Quality.gitlab-ci.yml

My .rubocop.yml file, stored in the root of the project has lots of configuration.

require:
  - rubocop-rspec
  - rubocop-rails
  - rubocop-performance
Style/HashEachMethods:
  Enabled: true
Style/HashTransformKeys:
  Enabled: true
Metrics/AbcSize:
  Enabled: false
…

And I have configured a .codeclimate.yml file as such.

version: "2"
plugins:
  rubocop:
    enabled: true
    channel: rubocop-1-48-1
    config:
      file: .rubocop.yml

If I run rubocop locally I get a clean bill of health. When GitLab runs it as part of a merge request pipeline, it has over 100 failures.

Major - Method get_state has 32 lines of code (exceeds 25 allowed). Consider refactoring.

in app/services/order/order_service.rb:62

I am basing my config on GitLab's Documentation and Code Climate's documentation.

Edit: Here is what I get when I run codeclimate locally, with the specific details of the failing code obfuscated.

✗ docker run \
  --interactive --tty --rm \
  --env CODECLIMATE_CODE="$PWD" \
  --volume "$PWD":/code \
  --volume /var/run/docker.sock:/var/run/docker.sock \
  --volume /tmp/cc:/tmp/cc \
  codeclimate/codeclimate analyze
Starting analysis
Running structure: Done!
Running duplication: Done!
Running rubocop: Done!

== app/controllers/foobar_controller.rb (1 issue) ==
18-29: Method `barbaz` has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. [structure]

…
1

There are 1 best solutions below

5
VonC On

I see in "Using rubocop's newer versions"

Using newer versions of RuboCop vs the Rubocop Plugin default

You might encounter errors if you rely on Code Climate to infer a RuboCop configuration file for you.

To avoid that, be sure to have a version-specific .rubocop.yml in your repo.

So start explicitly setting the RuboCop version in your .codeclimate.yml file (latest one is 1-50-2):

version: "2"
plugins:
  rubocop:
    enabled: true
    channel: rubocop-1-48-1
    config:
      file: .rubocop.yml
    checks:
      rubocop:
        enabled: true
        config: .rubocop.yml
  structure:
    enabled: true
    checks:
      cognitive-complexity:
        config:
          threshold: 10

Then try again, both locally and on GitLab.
But make sure your .rubocop.yml, .gitlab-ci.yml, and .codeclimate.yml files are committed and pushed to the GitLab repository. Code Climate will only use configuration files present in the repository when the pipeline is triggered.

Just in case, check locally if one of those file is ignored:

cd /path/to/repository
git check-ignore -v -- .rubocop.yml
git check-ignore -v -- .gitlab-ci.yml
git check-ignore -v -- .codeclimate.yml