|
11 | 11 | from libcst._position import CodeRange |
12 | 12 | from typing_extensions import Self |
13 | 13 |
|
14 | | -from codemodder.codetf import Finding |
| 14 | +from codemodder.codetf import Finding, Rule |
15 | 15 |
|
16 | 16 | from .utils.abc_dataclass import ABCDataclass |
17 | 17 |
|
@@ -86,6 +86,26 @@ class SarifResult(SASTResult, ABCDataclass): |
86 | 86 | def from_sarif( |
87 | 87 | cls, sarif_result, sarif_run, truncate_rule_id: bool = False |
88 | 88 | ) -> Self: |
| 89 | + rule_id = cls.extract_rule_id(sarif_result, sarif_run, truncate_rule_id) |
| 90 | + finding_id = cls.extract_finding_id(sarif_result) or rule_id |
| 91 | + return cls( |
| 92 | + rule_id=rule_id, |
| 93 | + locations=cls.extract_locations(sarif_result), |
| 94 | + codeflows=cls.extract_code_flows(sarif_result), |
| 95 | + related_locations=cls.extract_related_locations(sarif_result), |
| 96 | + finding_id=finding_id, |
| 97 | + finding=Finding( |
| 98 | + id=finding_id, |
| 99 | + rule=Rule( |
| 100 | + id=rule_id, |
| 101 | + name=rule_id, |
| 102 | + url=cls.rule_url_from_id(sarif_result, sarif_run, rule_id), |
| 103 | + ), |
| 104 | + ), |
| 105 | + ) |
| 106 | + |
| 107 | + @classmethod |
| 108 | + def rule_url_from_id(cls, result: dict, run: dict, rule_id: str) -> str: |
89 | 109 | raise NotImplementedError |
90 | 110 |
|
91 | 111 | @classmethod |
@@ -139,6 +159,10 @@ def extract_rule_id(cls, result, sarif_run, truncate_rule_id: bool = False) -> s |
139 | 159 |
|
140 | 160 | raise ValueError("Could not extract rule id from sarif result.") |
141 | 161 |
|
| 162 | + @classmethod |
| 163 | + def extract_finding_id(cls, result) -> str | None: |
| 164 | + return result.get("guid") or result.get("correlationGuid") |
| 165 | + |
142 | 166 |
|
143 | 167 | def same_line(pos: CodeRange, location: Location) -> bool: |
144 | 168 | return pos.start.line == location.start.line and pos.end.line == location.end.line |
|
0 commit comments