Convert phpmd JSON to codeclimate/gitlab format

Daniel Ness
Nov 14, 2020

--

phpmd doesn’t appear to support custom renderers like most other analysis tools. The following command may be used to convert the output of the phpmd JSON renderer to a format compatible with gitlab/codeclimate code quality analysis.

jq '[.files | .[] | .file as $filename | .violations | {type: "issue", "categories": ["Style", "PHP"], "check_name":.[].rule, "fingerprint": ("\($filename):\(.[].beginLine):\(.[].rule)"), "description":.[].description, "location": {"path": $filename, "lines": {"begin":.[].beginLine, "end":.[].endLine }}} ]'

I’m using the above within a gitlab pipeline as follows:

static:phpmd:
<<: *include_app
stage: static
script:
- |
vendor/bin/phpmd json vendor/$PKG_NAME dev/tests/static/testsuite/Magento/Test/Php/_files/phpmd/ruleset.xml |
jq '[.files | .[] | .file as $filename | .violations | {type: "issue", "categories": ["Style", "PHP"],
"check_name":.[].rule, "fingerprint": ("\($filename):\(.[].beginLine):\(.[].rule)"),
"description":.[].description, "location": {"path": $filename, "lines": {"begin":.[].beginLine,
"end":.[].endLine }}} ]' > codequality_phpmd.json
allow_failure: true
artifacts:
when: always
paths:
- codequality_phpmd.json
needs:
- job: build:app
artifacts: true

A subsequent stage in the pipeline is responsible for merging multiple codequality reports into a single file:

code_quality:
stage: report
script:
- jq -s '[.[][]]' codequality_*.json > codequality.json
needs:
- job: static:phpcs
artifacts: true
- job: static:phpmd
artifacts: true
- job: static:phpstan
artifacts: true
- job: static:jscs
artifacts: true
- job: static:eslint
artifacts: true
- job: static:less
artifacts: true
artifacts:
reports:
codequality: codequality.json

--

--

Daniel Ness
Daniel Ness

Responses (1)