Skip to content

Commit b3370bf

Browse files
authored
feat(sarif): update the message format produced by convert_to_sarif.py (#28)
* Update convert_to_sarif.py * Update convert_to_sarif_test.py
1 parent 161bdf8 commit b3370bf

File tree

2 files changed

+23
-28
lines changed

2 files changed

+23
-28
lines changed

tools/convert_to_sarif.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
"""Convert the output of lintrunner json to SARIF."""
22

3+
from __future__ import annotations
4+
35
import argparse
46
import json
57
import os
6-
from typing import Iterable
8+
from typing import Any, Iterable
79

810

9-
def format_rule_name(lintrunner_result: dict) -> str:
11+
def format_rule_name(lintrunner_result: dict[str, Any]) -> str:
1012
return f"{lintrunner_result['code']}/{lintrunner_result['name']}"
1113

1214

@@ -16,7 +18,9 @@ def severity_to_github_level(severity: str) -> str:
1618
return severity
1719

1820

19-
def parse_single_lintrunner_result(lintrunner_result: dict) -> tuple:
21+
def parse_single_lintrunner_result(
22+
lintrunner_result: dict[str, Any]
23+
) -> tuple[dict[str, Any], dict[str, Any]]:
2024
r"""Parse a single lintrunner result.
2125
2226
A result looks like this:
@@ -42,9 +46,7 @@ def parse_single_lintrunner_result(lintrunner_result: dict) -> tuple:
4246
"ruleId": format_rule_name(lintrunner_result),
4347
"level": severity_to_github_level(lintrunner_result["severity"]),
4448
"message": {
45-
"text": format_rule_name(lintrunner_result)
46-
+ "\n"
47-
+ lintrunner_result["description"],
49+
"text": lintrunner_result["description"],
4850
},
4951
"locations": [
5052
{
@@ -66,11 +68,7 @@ def parse_single_lintrunner_result(lintrunner_result: dict) -> tuple:
6668
"rule": {
6769
"id": format_rule_name(lintrunner_result),
6870
"name": format_rule_name(lintrunner_result),
69-
"shortDescription": {
70-
"text": format_rule_name(lintrunner_result)
71-
+ ": "
72-
+ lintrunner_result["description"].split("\n")[0],
73-
},
71+
"shortDescription": {"text": format_rule_name(lintrunner_result)},
7472
"fullDescription": {
7573
"text": format_rule_name(lintrunner_result)
7674
+ "\n"
@@ -85,7 +83,7 @@ def parse_single_lintrunner_result(lintrunner_result: dict) -> tuple:
8583
return result, rule
8684

8785

88-
def produce_sarif(lintrunner_results: Iterable[dict]) -> dict:
86+
def produce_sarif(lintrunner_results: Iterable[dict[str, Any]]) -> dict[str, Any]:
8987
"""Convert the output of lintrunner json to SARIF."""
9088

9189
rules = {}
@@ -114,9 +112,8 @@ def produce_sarif(lintrunner_results: Iterable[dict]) -> dict:
114112
return sarif
115113

116114

117-
def main(args):
115+
def main(args: Any) -> None:
118116
"""Convert the output of lintrunner json to SARIF."""
119-
120117
with open(args.input, "r", encoding="utf-8") as f:
121118
lintrunner_jsons = [json.loads(line) for line in f]
122119

tools/convert_to_sarif_test.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
from __future__ import annotations
2+
13
import unittest
24

35
import convert_to_sarif
46

57

68
class TestConvertToSarif(unittest.TestCase):
7-
def test_produce_sarif_returns_correct_sarif_result(self):
9+
def test_produce_sarif_returns_correct_sarif_result(self) -> None:
810
lintrunner_results = [
911
{
1012
"path": "test.py",
@@ -47,9 +49,7 @@ def test_produce_sarif_returns_correct_sarif_result(self):
4749
{
4850
"id": "FLAKE8/test-code",
4951
"name": "FLAKE8/test-code",
50-
"shortDescription": {
51-
"text": "FLAKE8/test-code: test description"
52-
},
52+
"shortDescription": {"text": "FLAKE8/test-code"},
5353
"fullDescription": {
5454
"text": "FLAKE8/test-code\ntest description"
5555
},
@@ -58,9 +58,7 @@ def test_produce_sarif_returns_correct_sarif_result(self):
5858
{
5959
"id": "FLAKE8/test-code-2",
6060
"name": "FLAKE8/test-code-2",
61-
"shortDescription": {
62-
"text": "FLAKE8/test-code-2: test description"
63-
},
61+
"shortDescription": {"text": "FLAKE8/test-code-2"},
6462
"fullDescription": {
6563
"text": "FLAKE8/test-code-2\ntest description"
6664
},
@@ -73,7 +71,7 @@ def test_produce_sarif_returns_correct_sarif_result(self):
7371
{
7472
"ruleId": "FLAKE8/test-code",
7573
"level": "error",
76-
"message": {"text": "FLAKE8/test-code\ntest description"},
74+
"message": {"text": "test description"},
7775
"locations": [
7876
{
7977
"physicalLocation": {
@@ -86,7 +84,7 @@ def test_produce_sarif_returns_correct_sarif_result(self):
8684
{
8785
"ruleId": "FLAKE8/test-code-2",
8886
"level": "error",
89-
"message": {"text": "FLAKE8/test-code-2\ntest description"},
87+
"message": {"text": "test description"},
9088
"locations": [
9189
{
9290
"physicalLocation": {
@@ -99,7 +97,7 @@ def test_produce_sarif_returns_correct_sarif_result(self):
9997
{
10098
"ruleId": "FLAKE8/test-code",
10199
"level": "note",
102-
"message": {"text": "FLAKE8/test-code\ntest description"},
100+
"message": {"text": "test description"},
103101
"locations": [
104102
{
105103
"physicalLocation": {
@@ -116,7 +114,7 @@ def test_produce_sarif_returns_correct_sarif_result(self):
116114
self.maxDiff = None
117115
self.assertEqual(actual, expected)
118116

119-
def test_it_handles_relative_paths(self):
117+
def test_it_handles_relative_paths(self) -> None:
120118
lintrunner_results = [
121119
{
122120
"path": "test.py",
@@ -133,7 +131,7 @@ def test_it_handles_relative_paths(self):
133131
{
134132
"ruleId": "FLAKE8/test-code",
135133
"level": "error",
136-
"message": {"text": "FLAKE8/test-code\ntest description"},
134+
"message": {"text": "test description"},
137135
"locations": [
138136
{
139137
"physicalLocation": {
@@ -146,7 +144,7 @@ def test_it_handles_relative_paths(self):
146144
]
147145
self.assertEqual(actual["runs"][0]["results"], expected_results)
148146

149-
def test_it_handles_absolute_paths(self):
147+
def test_it_handles_absolute_paths(self) -> None:
150148
lintrunner_results = [
151149
{
152150
"path": "/path/to/test.py",
@@ -163,7 +161,7 @@ def test_it_handles_absolute_paths(self):
163161
{
164162
"ruleId": "FLAKE8/test-code",
165163
"level": "error",
166-
"message": {"text": "FLAKE8/test-code\ntest description"},
164+
"message": {"text": "test description"},
167165
"locations": [
168166
{
169167
"physicalLocation": {

0 commit comments

Comments
 (0)