From cef6a7fa5a08c8c3ead117723b13573f33c329b7 Mon Sep 17 00:00:00 2001 From: Andrei Titerlea <113932106+atiterlea@users.noreply.github.com> Date: Thu, 17 Nov 2022 14:14:33 -0800 Subject: [PATCH] Make SARIF output specification compliant (#2668) * Fix for issue 2667 - SARIF output specification * chore: auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: Andrei Titerlea Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Sorin Sbarnea --- src/ansiblelint/formatters/__init__.py | 12 ++++++------ test/test_formatter_sarif.py | 10 ++++++---- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/ansiblelint/formatters/__init__.py b/src/ansiblelint/formatters/__init__.py index 1e2eccf4fc..78b626c67a 100644 --- a/src/ansiblelint/formatters/__init__.py +++ b/src/ansiblelint/formatters/__init__.py @@ -255,17 +255,17 @@ def _extract_results( rules = {} results = [] for match in matches: - if match.rule.id not in rules: - rules[match.rule.id] = self._to_sarif_rule(match) + if match.tag not in rules: + rules[match.tag] = self._to_sarif_rule(match) results.append(self._to_sarif_result(match)) return list(rules.values()), results def _to_sarif_rule(self, match: MatchError) -> dict[str, Any]: rule: dict[str, Any] = { - "id": match.rule.id, + "id": match.tag, "name": match.tag, "shortDescription": { - "text": self.escape(str(match.message)), + "text": str(match.message), }, "defaultConfiguration": { "level": self._to_sarif_level(match), @@ -282,9 +282,9 @@ def _to_sarif_rule(self, match: MatchError) -> dict[str, Any]: def _to_sarif_result(self, match: MatchError) -> dict[str, Any]: result: dict[str, Any] = { - "ruleId": match.rule.id, + "ruleId": match.tag, "message": { - "text": match.details, + "text": str(match.message), }, "locations": [ { diff --git a/test/test_formatter_sarif.py b/test/test_formatter_sarif.py index 8937ef7928..9509d6ce2b 100644 --- a/test/test_formatter_sarif.py +++ b/test/test_formatter_sarif.py @@ -38,6 +38,7 @@ def setup_class(self) -> None: details="hello", filename=Lintable("filename.yml"), rule=self.rule, + tag="yaml[test]", ) ) self.matches.append( @@ -47,6 +48,7 @@ def setup_class(self) -> None: details="hello", filename=Lintable("filename.yml"), rule=self.rule, + tag="yaml[test]", ) ) self.formatter = SarifFormatter(pathlib.Path.cwd(), display_relative_path=True) @@ -84,8 +86,8 @@ def test_validate_sarif_schema(self) -> None: assert driver["informationUri"] == SarifFormatter.TOOL_URL rules = driver["rules"] assert len(rules) == 1 - assert rules[0]["id"] == self.matches[0].rule.id - assert rules[0]["name"] == self.matches[0].rule.id + assert rules[0]["id"] == self.matches[0].tag + assert rules[0]["name"] == self.matches[0].tag assert rules[0]["shortDescription"]["text"] == self.matches[0].message assert rules[0]["defaultConfiguration"]["level"] == "error" assert rules[0]["help"]["text"] == self.matches[0].rule.description @@ -94,8 +96,8 @@ def test_validate_sarif_schema(self) -> None: results = sarif["runs"][0]["results"] assert len(results) == 2 for i, result in enumerate(results): - assert result["ruleId"] == self.matches[i].rule.id - assert result["message"]["text"] == self.matches[i].details + assert result["ruleId"] == self.matches[i].tag + assert result["message"]["text"] == self.matches[0].message assert ( result["locations"][0]["physicalLocation"]["artifactLocation"]["uri"] == self.matches[i].filename