Skip to content

Commit

Permalink
Make SARIF output specification compliant (#2668)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Sorin Sbarnea <[email protected]>
  • Loading branch information
4 people authored Nov 17, 2022
1 parent fa20acf commit cef6a7f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
12 changes: 6 additions & 6 deletions src/ansiblelint/formatters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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": [
{
Expand Down
10 changes: 6 additions & 4 deletions test/test_formatter_sarif.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def setup_class(self) -> None:
details="hello",
filename=Lintable("filename.yml"),
rule=self.rule,
tag="yaml[test]",
)
)
self.matches.append(
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit cef6a7f

Please sign in to comment.