Skip to content

Commit

Permalink
Support parsing JSON output from mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmmacleod committed Nov 19, 2024
1 parent 19141c5 commit cba0d1a
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions mypy_gitlab_code_quality/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import hashlib
import json
import os
import re
from enum import Enum
from functools import reduce
Expand Down Expand Up @@ -33,29 +34,37 @@ class GitlabIssue(TypedDict):


def parse_issue(line: str) -> GitlabIssue | None:
match = re.fullmatch(
r"(?P<path>.+?)"
r":(?P<line_number>\d+)(?::\d+)?" # ignore column number if exists
r":\s(?P<error_level>\w+)"
r":\s(?P<description>.+?)"
r"(?:\s\s\[(?P<error_code>.*)])?",
line,
)
if line.startswith("{"):
try:
match = json.loads(line)
except json.JSONDecodeError:
match = None
if hint := match.get("hint"): # attach hint to message
match["message"] += os.linesep + hint
else:
match = re.fullmatch(
r"(?P<file>.+?)"
r":(?P<line>\d+)(?::\d+)?" # ignore column number if exists
r":\s(?P<severity>\w+)"
r":\s(?P<message>.+?)"
r"(?:\s\s\[(?P<code>.*)])?",
line,
)
if match is None:
return None
# TODO(soul-catcher): add usedforsecurity=False and remove noqa
# when python 3.8 will be discontinued
fingerprint = hashlib.md5(line.encode("utf-8")).hexdigest() # noqa: S324
error_levels_table = {"error": Severity.major, "note": Severity.info}
return {
"description": match["description"],
"check_name": match["error_code"],
"description": match["message"],
"check_name": match["code"],
"fingerprint": fingerprint,
"severity": error_levels_table.get(match["error_level"], Severity.unknown),
"severity": error_levels_table.get(match["severity"], Severity.unknown),
"location": {
"path": match["path"],
"path": match["file"],
"lines": {
"begin": int(match["line_number"]),
"begin": int(match["line"]),
},
},
}
Expand Down

0 comments on commit cba0d1a

Please sign in to comment.