Skip to content

Commit c3b6e2b

Browse files
duncanmmacleodsoul-catcher
authored andcommitted
Support parsing JSON output from mypy
1 parent dd2dbc9 commit c3b6e2b

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

mypy_gitlab_code_quality/__init__.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import hashlib
44
import json
5+
import os
56
import re
67
from enum import Enum
78
from functools import reduce
@@ -33,29 +34,37 @@ class GitlabIssue(TypedDict):
3334

3435

3536
def parse_issue(line: str) -> GitlabIssue | None:
36-
match = re.fullmatch(
37-
r"(?P<path>.+?)"
38-
r":(?P<line_number>\d+)(?::\d+)?" # ignore column number if exists
39-
r":\s(?P<error_level>\w+)"
40-
r":\s(?P<description>.+?)"
41-
r"(?:\s\s\[(?P<error_code>.*)])?",
42-
line,
43-
)
37+
if line.startswith("{"):
38+
try:
39+
match = json.loads(line)
40+
except json.JSONDecodeError:
41+
match = None
42+
if hint := match.get("hint"): # attach hint to message
43+
match["message"] += os.linesep + hint
44+
else:
45+
match = re.fullmatch(
46+
r"(?P<file>.+?)"
47+
r":(?P<line>\d+)(?::\d+)?" # ignore column number if exists
48+
r":\s(?P<severity>\w+)"
49+
r":\s(?P<message>.+?)"
50+
r"(?:\s\s\[(?P<code>.*)])?",
51+
line,
52+
)
4453
if match is None:
4554
return None
4655
# TODO(soul-catcher): add usedforsecurity=False and remove noqa
4756
# when python 3.8 will be discontinued
4857
fingerprint = hashlib.md5(line.encode("utf-8")).hexdigest() # noqa: S324
4958
error_levels_table = {"error": Severity.major, "note": Severity.info}
5059
return {
51-
"description": match["description"],
52-
"check_name": match["error_code"],
60+
"description": match["message"],
61+
"check_name": match["code"],
5362
"fingerprint": fingerprint,
54-
"severity": error_levels_table.get(match["error_level"], Severity.unknown),
63+
"severity": error_levels_table.get(match["severity"], Severity.unknown),
5564
"location": {
56-
"path": match["path"],
65+
"path": match["file"],
5766
"lines": {
58-
"begin": int(match["line_number"]),
67+
"begin": int(match["line"]),
5968
},
6069
},
6170
}

0 commit comments

Comments
 (0)