Skip to content

Commit 59bdf49

Browse files
committed
Add GitHub reporter
A new `github` reporter has been added. This reporter automatically annotates code on GitHub's UI with the messages found during linting. Closes #9443.
1 parent 6a09d29 commit 59bdf49

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
A new `github` reporter has been added. This reporter automatically annotates code on GitHub's user interface
2+
with the messages found during linting. Use it with `pylint --output-format=github`.
3+
4+
Closes #9443.

pylint/reporters/text.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,32 @@ def handle_message(self, msg: Message) -> None:
255255
self.write_message(msg)
256256

257257

258+
class GithubReporter(TextReporter):
259+
"""Report messages in GitHub's special format to annotate code in their UI."""
260+
261+
name = "github"
262+
line_format = "::{category} file={path},line={line},endline={end_line},col={column},title={msg_id}::{msg}"
263+
264+
def write_message(self, msg: Message) -> None:
265+
self_dict = asdict(msg)
266+
for key in ("end_line", "end_column"):
267+
self_dict[key] = self_dict[key] or ""
268+
269+
msg_type = msg.msg_id[:1]
270+
category = "error"
271+
if msg_type == "W":
272+
category = "warning"
273+
elif msg_type in {"C", "R", "I"}:
274+
category = "notice"
275+
276+
self_dict["category"] = category
277+
self.writeln(self._fixed_template.format(**self_dict))
278+
279+
258280
def register(linter: PyLinter) -> None:
259281
linter.register_reporter(TextReporter)
260282
linter.register_reporter(NoHeaderReporter)
261283
linter.register_reporter(ParseableTextReporter)
262284
linter.register_reporter(VSTextReporter)
263285
linter.register_reporter(ColorizedTextReporter)
286+
linter.register_reporter(GithubReporter)

0 commit comments

Comments
 (0)