Skip to content

Instantly share code, notes, and snippets.

@caryan
Last active February 23, 2024 07:40
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save caryan/87bdadba4b6579ffed8a87d546364d72 to your computer and use it in GitHub Desktop.
Save caryan/87bdadba4b6579ffed8a87d546364d72 to your computer and use it in GitHub Desktop.
Convert pylint output to simplified CodeClimate JSON suitable for Gitlab Code Quality checks
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())
@quanterium
Copy link

@nyue I had the same issue with Python 3.6.8 and pylint 2.12.2. I found I had to override the handle_message method from the base class, since it was looking for the original JSON output fields as attributes, and not the new, differently-named ones in a dictionary. My version is here: https://gist.github.com/quanterium/53a277cab4136963374cf703dfe399bd

@ahogen
Copy link

ahogen commented Feb 2, 2022

Personally I ended up using pylint-gitlab. It basically worked out of the box, for me. Their README explains usage well.

Even though it stated Python >=3.7, it seems to work fine in 3.6.8.

@caryan
Copy link
Author

caryan commented Mar 7, 2022

Thanks for the reports and suggestions @nyue, @quanterium and @ahogen. pylint-gitlab indeed looks like the easiest option but for a lightweight option I've updated the gist so that it now works with Python 3.10 and pylint==2.12.2. As @nyue noted it's easiest now to just override display_messages because this commit makes it now assume messages contains a list of Message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment