Skip to content

Commit

Permalink
each sarif result should impl from_sarif
Browse files Browse the repository at this point in the history
  • Loading branch information
clavedeluna committed Jul 25, 2024
1 parent 0c297f1 commit b5951f2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 23 deletions.
26 changes: 26 additions & 0 deletions src/codemodder/codeql.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from typing_extensions import Self

from codemodder.codetf import Finding, Rule
from codemodder.result import LineInfo, ResultSet, SarifLocation, SarifResult
from codemodder.sarifs import AbstractSarifToolDetector

Expand Down Expand Up @@ -38,6 +39,31 @@ def from_sarif(cls, sarif_location) -> Self:
class CodeQLResult(SarifResult):
location_type = CodeQLLocation

@classmethod
def from_sarif(
cls, sarif_result, sarif_run, truncate_rule_id: bool = False
) -> Self:
return cls(
rule_id=(
rule_id := cls.extract_rule_id(
sarif_result, sarif_run, truncate_rule_id
)
),
locations=cls.extract_locations(sarif_result),
codeflows=cls.extract_code_flows(sarif_result),
related_locations=cls.extract_related_locations(sarif_result),
finding_id=rule_id,
finding=Finding(
id=rule_id,
rule=Rule(
id=rule_id,
name=rule_id,
# TODO: map to URL
# url=,
),
),
)


class CodeQLResultSet(ResultSet):
@classmethod
Expand Down
25 changes: 2 additions & 23 deletions src/codemodder/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from libcst._position import CodeRange
from typing_extensions import Self

from codemodder.codetf import Finding, Rule
from codemodder.codetf import Finding

from .utils.abc_dataclass import ABCDataclass

Expand Down Expand Up @@ -80,28 +80,7 @@ class SarifResult(SASTResult, ABCDataclass):
def from_sarif(
cls, sarif_result, sarif_run, truncate_rule_id: bool = False
) -> Self:
# avoid circular import
from core_codemods.semgrep.api import semgrep_url_from_id

return cls(
rule_id=(
rule_id := cls.extract_rule_id(
sarif_result, sarif_run, truncate_rule_id
)
),
locations=cls.extract_locations(sarif_result),
codeflows=cls.extract_code_flows(sarif_result),
related_locations=cls.extract_related_locations(sarif_result),
finding_id=rule_id,
finding=Finding(
id=rule_id,
rule=Rule(
id=rule_id,
name=rule_id,
url=semgrep_url_from_id(rule_id),
),
),
)
raise NotImplementedError

@classmethod
def extract_locations(cls, sarif_result) -> list[Location]:
Expand Down
28 changes: 28 additions & 0 deletions src/codemodder/semgrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from typing_extensions import Self, override

from codemodder.codetf import Finding, Rule
from codemodder.context import CodemodExecutionContext
from codemodder.logging import logger
from codemodder.result import LineInfo, Result, ResultSet, SarifLocation, SarifResult
Expand Down Expand Up @@ -43,6 +44,33 @@ def from_sarif(cls, sarif_location) -> Self:
class SemgrepResult(SarifResult):
location_type = SemgrepLocation

@classmethod
def from_sarif(
cls, sarif_result, sarif_run, truncate_rule_id: bool = False
) -> Self:
# avoid circular import
from core_codemods.semgrep.api import semgrep_url_from_id

return cls(
rule_id=(
rule_id := cls.extract_rule_id(
sarif_result, sarif_run, truncate_rule_id
)
),
locations=cls.extract_locations(sarif_result),
codeflows=cls.extract_code_flows(sarif_result),
related_locations=cls.extract_related_locations(sarif_result),
finding_id=rule_id,
finding=Finding(
id=rule_id,
rule=Rule(
id=rule_id,
name=rule_id,
url=semgrep_url_from_id(rule_id),
),
),
)


class SemgrepResultSet(ResultSet):
@classmethod
Expand Down

0 comments on commit b5951f2

Please sign in to comment.