Skip to content

Commit

Permalink
Filter sonar issues by status
Browse files Browse the repository at this point in the history
  • Loading branch information
andrecsilva committed Feb 16, 2024
1 parent 10184d4 commit c20d18a
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/codemodder/sonar_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ def from_json(cls, json_file: str | Path) -> Self:

result_set = cls()
for issue in data.get("issues"):
result_set.add_result(SonarResult.from_issue(issue))
if issue["status"].lower() == "open":
result_set.add_result(SonarResult.from_issue(issue))

return result_set
except Exception:
Expand Down
1 change: 1 addition & 0 deletions tests/codemods/test_sonar_django_json_response_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def foo(request):
"issues": [
{
"rule": "pythonsecurity:S5131",
"status": "OPEN",
"component": f"{tmpdir / 'code.py'}",
"textRange": {
"startLine": 7,
Expand Down
1 change: 1 addition & 0 deletions tests/codemods/test_sonar_django_receiver_on_top.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def foo():
"issues": [
{
"rule": "python:S6552",
"status": "OPEN",
"component": f"{tmpdir / 'code.py'}",
"textRange": {
"startLine": 5,
Expand Down
1 change: 1 addition & 0 deletions tests/codemods/test_sonar_exception_without_raise.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def test_simple(self, tmpdir):
"issues": [
{
"rule": "python:S3984",
"status": "OPEN",
"component": f"{tmpdir / 'code.py'}",
"textRange": {
"startLine": 2,
Expand Down
1 change: 1 addition & 0 deletions tests/codemods/test_sonar_fix_assert_tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def test_simple(self, tmpdir):
"issues": [
{
"rule": "python:S5905",
"status": "OPEN",
"component": f"{tmpdir / 'code.py'}",
"textRange": {
"startLine": 2,
Expand Down
1 change: 1 addition & 0 deletions tests/codemods/test_sonar_flask_json_response_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def foo(request):
"issues": [
{
"rule": "pythonsecurity:S5131",
"status": "OPEN",
"component": f"{tmpdir / 'code.py'}",
"textRange": {
"startLine": 10,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def test_list(self, tmpdir):
"issues": [
{
"rule": "python:S5796",
"status": "OPEN",
"component": f"{tmpdir / 'code.py'}",
"textRange": {
"startLine": 2,
Expand Down
1 change: 1 addition & 0 deletions tests/codemods/test_sonar_numpy_nan_equality.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def test_simple(self, tmpdir):
"issues": [
{
"rule": "python:S6725",
"status": "OPEN",
"component": f"{tmpdir / 'code.py'}",
"textRange": {
"startLine": 3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def foo():
"issues": [
{
"rule": "python:S5915",
"status": "OPEN",
"component": f"{tmpdir / 'code.py'}",
"textRange": {
"startLine": 6,
Expand Down
2 changes: 1 addition & 1 deletion tests/samples/sonar_issues.json

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions tests/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def test_or(self, tmpdir):
"issues": [
{
"rule": "rule",
"status": "OPEN",
"component": "code.py",
"textRange": {
"startLine": 2,
Expand All @@ -23,6 +24,7 @@ def test_or(self, tmpdir):
"issues": [
{
"rule": "rule",
"status": "OPEN",
"component": "code.py",
"textRange": {
"startLine": 1,
Expand All @@ -46,6 +48,39 @@ def test_or(self, tmpdir):
assert result2["rule"][Path("code.py")][0] in combined["rule"][Path("code.py")]
assert result1["rule"][Path("code.py")][0] in combined["rule"][Path("code.py")]

def test_sonar_only_open_issues(self, tmpdir):
issues = {
"issues": [
{
"rule": "rule",
"status": "OPEN",
"component": "code.py",
"textRange": {
"startLine": 1,
"endLine": 1,
"startOffset": 1,
"endOffset": 1,
},
},
{
"rule": "rule",
"status": "RESOLVED",
"component": "code.py",
"textRange": {
"startLine": 1,
"endLine": 1,
"startOffset": 1,
"endOffset": 1,
},
},
]
}
sonar_json1 = Path(tmpdir) / "sonar1.json"
sonar_json1.write_text(json.dumps(issues))

result = SonarResultSet.from_json(sonar_json1)
assert len(result["rule"][Path("code.py")]) == 1

def test_sonar_robustness(self, tmpdir):
sonar_json = Path(tmpdir) / "sonar1.json"
# not a valid json
Expand Down

0 comments on commit c20d18a

Please sign in to comment.