Skip to content

Commit

Permalink
Include level in SARIF results (#3758)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed Sep 20, 2023
1 parent c411c42 commit c2e86d9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/ansiblelint/formatters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,17 @@ def _to_sarif_rule(self, match: MatchError) -> dict[str, Any]:
return rule

def _to_sarif_result(self, match: MatchError) -> dict[str, Any]:
# https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/sarif-v2.1.0-errata01-os-complete.html#_Toc141790898
if match.level not in ("warning", "error", "note", "none"):
msg = "Unexpected failure to map '%s' level to SARIF."
raise RuntimeError(
msg,
match.level,
)

result: dict[str, Any] = {
"ruleId": match.tag,
"level": match.level,
"message": {
"text": str(match.details)
if str(match.details)
Expand Down
9 changes: 7 additions & 2 deletions test/test_formatter_sarif.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,16 @@ def test_single_match(self) -> None:
with pytest.raises(RuntimeError):
self.formatter.format_result(self.matches[0]) # type: ignore[arg-type]

def test_result_is_list(self) -> None:
"""Test if the return SARIF object contains the results with length of 2."""
def test_sarif_format(self) -> None:
"""Test if the return SARIF object contains the expected results."""
assert isinstance(self.formatter, SarifFormatter)
sarif = json.loads(self.formatter.format_result(self.matches))
assert len(sarif["runs"][0]["results"]) == 2
for result in sarif["runs"][0]["results"]:
# Ensure all reported entries have a level
assert "level" in result
# Ensure reported levels are either error or warning
assert result["level"] in ("error", "warning")

def test_validate_sarif_schema(self) -> None:
"""Test if the returned JSON is a valid SARIF report."""
Expand Down

0 comments on commit c2e86d9

Please sign in to comment.