Skip to content

Commit

Permalink
Fix sonar metadata (#824)
Browse files Browse the repository at this point in the history
* Fix issue name and finding ID in Sonar metadata

* Update fixed findings with metadata from ToolRule

* Hardening suggestions for codemodder-python / fix-sonar-metadata (#825)

Use Assignment Expression (Walrus) In Conditional

Co-authored-by: pixeebot[bot] <104101892+pixeebot[bot]@users.noreply.github.com>

---------

Co-authored-by: pixeebot[bot] <104101892+pixeebot[bot]@users.noreply.github.com>
  • Loading branch information
drdavella and pixeebot[bot] authored Sep 5, 2024
1 parent 51344e5 commit b557d7b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/codemodder/codemods/base_codemod.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ def detection_tool(self) -> DetectionTool | None:
name=self._metadata.tool.name,
)

@property
def detection_tool_rules(self) -> list[ToolRule]:
return self._metadata.tool.rules if self._metadata.tool else []

@cached_property
def docs_module(self) -> Traversable:
return importlib.resources.files(self.docs_module_path)
Expand Down
8 changes: 7 additions & 1 deletion src/codemodder/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from codemodder.registry import CodemodRegistry
from codemodder.result import ResultSet
from codemodder.utils.timer import Timer
from codemodder.utils.update_finding_metadata import update_finding_metadata

if TYPE_CHECKING:
from openai import OpenAI
Expand Down Expand Up @@ -180,6 +181,11 @@ def process_results(self, codemod_id: str, results: Iterator[FileContext]):
def compile_results(self, codemods: list[BaseCodemod]) -> list[CodeTFResult]:
results = []
for codemod in codemods:
changesets = update_finding_metadata(
codemod.detection_tool_rules,
self.get_changesets(codemod.id),
)

result = CodeTFResult(
codemod=codemod.id,
summary=codemod.summary,
Expand All @@ -188,7 +194,7 @@ def compile_results(self, codemods: list[BaseCodemod]) -> list[CodeTFResult]:
references=codemod.references,
properties={},
failedFiles=[str(file) for file in self.get_failures(codemod.id)],
changeset=self.get_changesets(codemod.id),
changeset=changesets,
unfixedFindings=self.get_unfixed_findings(codemod.id),
)

Expand Down
26 changes: 26 additions & 0 deletions src/codemodder/utils/update_finding_metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from __future__ import annotations

import typing

if typing.TYPE_CHECKING:
from codemodder.codemods.base_codemod import ToolRule

from codemodder.codetf import ChangeSet


def update_finding_metadata(
tool_rules: list[ToolRule],
changesets: list[ChangeSet],
) -> list[ChangeSet]:
if not (tool_rule_map := {rule.id: (rule.name, rule.url) for rule in tool_rules}):
return changesets

for changeset in changesets:
for change in changeset.changes:
for finding in change.findings or []:
if finding.id in tool_rule_map:
finding.rule.name = tool_rule_map[finding.id][0]
finding.rule.url = tool_rule_map[finding.id][1]

# TODO: eventually make this functional and return a new list
return changesets
9 changes: 7 additions & 2 deletions src/core_codemods/sonar/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,21 @@ def from_result(cls, result: dict) -> Self:
for flow in result.get("flows", [])
]

finding_id = result.get("key", rule_id)

# Both issues and hotspots have a `message` key
name = result.get("message", None) or rule_id

return cls(
finding_id=rule_id,
finding_id=finding_id,
rule_id=rule_id,
locations=locations,
codeflows=all_flows,
finding=Finding(
id=rule_id,
rule=Rule(
id=rule_id,
name=rule_id,
name=name,
url=sonar_url_from_id(rule_id),
),
),
Expand Down

0 comments on commit b557d7b

Please sign in to comment.