-
-
Save tracphil/791e61a346280a75d91090f1dfa98ef9 to your computer and use it in GitHub Desktop.
Convert pylint output to simplified CodeClimate JSON suitable for Gitlab Code Quality checks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import hashlib | |
import html | |
import json | |
import sys | |
from typing import Optional | |
from pylint.reporters import JSONReporter | |
from pylint.lint import Run | |
# map pylint categories to CodeClimate severity | |
PYLINT_CATEGORY_TO_SEVERITY = { | |
"fatal": "blocker", | |
"error": "critical", | |
"warning": "major", | |
"refactor": "minor", | |
"convention": "minor", | |
} | |
class GitlabCodeClimateReporter(JSONReporter): | |
""" | |
Custom pylint reporter to convert pylint messages into a reduced CodeClimate JSON report | |
suitable for Gitlab's Code Quality. Only reports `description`, `fingerprint`, `severity`, | |
`location`. | |
See: | |
1. https://docs.gitlab.com/ee/user/project/merge_requests/code_quality.html#implementing-a-custom-tool. | |
2. https://github.com/PyCQA/pylint/blob/master/pylint/reporters/json_reporter.py | |
""" | |
name = "gitlabcodeclimate" | |
def display_messages(self, layout: Optional["Section"]) -> None: | |
""" | |
Convert the pylint messages into a reduced CodeClimate report dictionary and dump as JSON. | |
""" | |
codeclimate_dict = [ | |
{ | |
"description": html.escape(f"{msg.msg_id}: {msg.msg or ''}", quote=False), | |
"severity": PYLINT_CATEGORY_TO_SEVERITY[msg.category], | |
"location": {"path": msg.path, "lines": {"begin": msg.line}}, | |
"fingerprint": hashlib.sha1( | |
(msg.symbol + msg.path + str(msg.line)).encode() | |
).hexdigest(), | |
} | |
for msg in self.messages | |
] | |
print(json.dumps(codeclimate_dict, indent=4), file=self.out) | |
if __name__ == "__main__": | |
Run(sys.argv[1:], reporter=GitlabCodeClimateReporter()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment