Convert eslint JSON to codeclimate/gitlab format
I’m looking at adding an eslint report to the Code Quality tab of my gitlab pipelines. Eslint doesn’t ship with a gitlab formatter, and the one that I found on npm didn’t work with my particular setup.
My eslint stage was throwing the following error:
ESLint: 7.13.0
TypeError: Cannot destructure property `artifacts` of 'undefined' or 'null'.
at getOutputPath (/var/www/html/node_modules/eslint-formatter-gitlab/index.js:19:29)
at module.exports (/var/www/html/node_modules/eslint-formatter-gitlab/index.js:67:54)
at Object.format (/var/www/html/node_modules/eslint/lib/eslint/eslint.js:603:24)
at printResults (/var/www/html/node_modules/eslint/lib/cli.js:176:30)
at process._tickCallback (internal/process/next_tick.js:68:7)
at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
The above is due to the fact that I take multiple reports from a number of stages and combine them into a single codequality report artifact later on. This is around Gitlab only using the last codequality artifact on a pipeline. The package I was attempting to use, however, attempts to infer the report output location from the artifacts
node of the stage which doesn’t work in my scenario.
If, however, you’re generating all of your codequality reports within a single stage then I would definitely recommend the original package that I found ( https://gitlab.com/remcohaszing/eslint-formatter-gitlab).
Instead I used jq to convert the output from eslint’s built-in json formatter into a format that is compatible with gitlab:
node_modules/eslint/bin/eslint.js -c dev/tests/static/testsuite/Magento/Test/Js/_files/eslint/.eslintrc vendor/magento/module-catalog/**/*.js --format json| sed "s#$PWD/\?##g" | jq '[.[] | .filePath as $filename | .messages[] as $message | {description: $message.message, location: {path: $filename, lines:{begin:$message.line, end:$message.endLine}}}]'
Within my .gitlab-ci.yml this looks like:
static:eslint:
<<: *include_app
stage: static
allow_failure: true
script:
- |
node_modules/eslint/bin/eslint.js \
-c dev/tests/static/testsuite/Magento/Test/Js/_files/eslint/.eslintrc \
vendor/$PKG_NAME/**/*.js \
--format json | \
| sed "s#$PWD/\?##g" | \
jq '[.[] | .filePath as $filename | .messages[] as $message | \
{description: $message.message, location: { \
path: $filename, lines:{begin:$message.line, end:$message.endLine \
}}}]' > $EXT_DIR/codequality_eslint.json
artifacts:
when: always
paths:
- codequality_eslint.json
rules:
- exists:
- ./**/*.js