Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make SARIF output specification compliant #2668

Merged
merged 4 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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