Skip to content

Commit 5754512

Browse files
authored
Update fixed findings metadata to align with CodeTF spec (#941)
* Update Change.findings => fixedFindings to conform to spec * Add fixedFindings to ChangeSet per spec * Fix logic for gathering fixed findings
1 parent 95bd4b2 commit 5754512

17 files changed

+61
-54
lines changed

src/codemodder/codemods/imported_call_modifier.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def leave_Call(self, original_node: cst.Call, updated_node: cst.Call):
8181
Change(
8282
lineNumber=line_number,
8383
description=self.change_description,
84-
findings=self.file_context.get_findings_for_location(
84+
fixedFindings=self.file_context.get_findings_for_location(
8585
line_number
8686
),
8787
)

src/codemodder/codemods/libcst_transformer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def report_change_for_line(
126126
Change(
127127
lineNumber=line_number,
128128
description=description or self.change_description,
129-
findings=findings
129+
fixedFindings=findings
130130
or self.file_context.get_findings_for_location(line_number),
131131
)
132132
)

src/codemodder/codemods/regex_transformer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def _apply(self, original_lines, file_context, results):
4040
Change(
4141
lineNumber=lineno + 1,
4242
description=self.change_description,
43-
findings=file_context.get_findings_for_location(lineno),
43+
fixedFindings=file_context.get_findings_for_location(lineno),
4444
)
4545
)
4646
return changes, updated_lines
@@ -110,7 +110,7 @@ def _apply(self, original_lines, file_context, results):
110110
Change(
111111
lineNumber=lineno + 1,
112112
description=self.change_description,
113-
findings=file_context.get_findings_for_location(lineno),
113+
fixedFindings=file_context.get_findings_for_location(lineno),
114114
)
115115
)
116116

src/codemodder/codemods/test/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,5 +224,5 @@ def run_and_assert(
224224

225225
def assert_findings(self, changes: list[Change]):
226226
assert all(
227-
x.findings for x in changes
227+
x.fixedFindings for x in changes
228228
), f"Expected all changes to have findings: {changes}"

src/codemodder/codemods/xml_transformer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def add_change(self, line):
9696
Change(
9797
lineNumber=line,
9898
description=self.change_description or None,
99-
findings=self.file_context.get_findings_for_location(line),
99+
fixedFindings=self.file_context.get_findings_for_location(line),
100100
)
101101
)
102102

src/codemodder/codetf.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class Change(BaseModel):
6262
diffSide: DiffSide = DiffSide.RIGHT
6363
properties: Optional[dict] = None
6464
packageActions: Optional[list[PackageAction]] = None
65-
findings: Optional[list[Finding]] = None
65+
fixedFindings: Optional[list[Finding]] = None
6666

6767
@model_validator(mode="after")
6868
def validate_lineNumber(self):
@@ -83,7 +83,7 @@ def with_findings(self, findings: list[Finding] | None) -> Change:
8383
diffSide=self.diffSide,
8484
properties=self.properties,
8585
packageActions=self.packageActions,
86-
findings=findings,
86+
fixedFindings=findings,
8787
)
8888

8989

@@ -108,6 +108,8 @@ class ChangeSet(BaseModel):
108108
ai: Optional[AIMetadata] = None
109109
strategy: Optional[Strategy] = None
110110
provisional: Optional[bool] = False
111+
# For fixed findings that are not associated with a specific change
112+
fixedFindings: Optional[list[Finding]] = None
111113

112114
def with_changes(self, changes: list[Change]) -> ChangeSet:
113115
return ChangeSet(
@@ -117,6 +119,7 @@ def with_changes(self, changes: list[Change]) -> ChangeSet:
117119
ai=self.ai,
118120
strategy=self.strategy,
119121
provisional=self.provisional,
122+
fixedFindings=self.fixedFindings,
120123
)
121124

122125

src/codemodder/file_context.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,22 @@ def add_unfixed_findings(
5050
]
5151
)
5252

53-
def get_findings_for_location(self, line_number: int):
53+
def get_findings_for_location(self, line_number: int) -> list[Finding]:
5454
return [
5555
result.finding
5656
for result in (self.results or [])
57-
if any(
58-
location.start.line <= line_number <= location.end.line
59-
for location in result.locations
60-
)
61-
or any(
62-
location.start.line <= line_number <= location.end.line
63-
for codeflow in result.codeflows
64-
for location in codeflow
57+
if result.finding is not None
58+
and (
59+
any(
60+
location.start.line <= line_number <= location.end.line
61+
for location in result.locations
62+
)
63+
or any(
64+
location.start.line <= line_number <= location.end.line
65+
for codeflow in result.codeflows
66+
for location in codeflow
67+
)
6568
)
66-
and result.finding is not None
6769
]
6870

6971
def match_findings(self, line_numbers):

src/codemodder/utils/update_finding_metadata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def update_finding_metadata(
2727
if finding.rule.id in tool_rule_map
2828
else finding
2929
)
30-
for finding in change.findings or []
30+
for finding in change.fixedFindings or []
3131
]
3232
or None
3333
)

src/core_codemods/file_resource_leak.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def _handle_block(
229229
Change(
230230
lineNumber=line_number,
231231
description=FileResourceLeakTransformer.change_description,
232-
findings=self.file_context.get_findings_for_location(
232+
fixedFindings=self.file_context.get_findings_for_location(
233233
line_number
234234
),
235235
)

tests/codemods/defectdojo/semgrep/test_avoid_insecure_deserialization.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ def test_yaml_load(self, tmpdir):
4646
)
4747

4848
assert changes is not None
49-
assert changes[0].changes[0].findings is not None
50-
assert changes[0].changes[0].findings[0].id == "1"
51-
assert changes[0].changes[0].findings[0].rule.id == RULE_ID
49+
assert changes[0].changes[0].fixedFindings is not None
50+
assert changes[0].changes[0].fixedFindings[0].id == "1"
51+
assert changes[0].changes[0].fixedFindings[0].rule.id == RULE_ID
5252

5353
@mock.patch("codemodder.codemods.api.FileContext.add_dependency")
5454
def test_pickle_load(self, adds_dependency, tmpdir):
@@ -80,9 +80,9 @@ def test_pickle_load(self, adds_dependency, tmpdir):
8080
adds_dependency.assert_called_once_with(Fickling)
8181

8282
assert changes is not None
83-
assert changes[0].changes[0].findings is not None
84-
assert changes[0].changes[0].findings[0].id == "2"
85-
assert changes[0].changes[0].findings[0].rule.id == RULE_ID
83+
assert changes[0].changes[0].fixedFindings is not None
84+
assert changes[0].changes[0].fixedFindings[0].id == "2"
85+
assert changes[0].changes[0].fixedFindings[0].rule.id == RULE_ID
8686

8787
@mock.patch("codemodder.codemods.api.FileContext.add_dependency")
8888
def test_pickle_and_yaml(self, adds_dependency, tmpdir):
@@ -128,12 +128,12 @@ def test_pickle_and_yaml(self, adds_dependency, tmpdir):
128128
adds_dependency.assert_called_once_with(Fickling)
129129

130130
assert changes is not None
131-
assert changes[0].changes[0].findings is not None
132-
assert changes[0].changes[0].findings[0].id == "4"
133-
assert changes[0].changes[0].findings[0].rule.id == RULE_ID
134-
assert changes[0].changes[1].findings is not None
135-
assert changes[0].changes[1].findings[0].id == "3"
136-
assert changes[0].changes[1].findings[0].rule.id == RULE_ID
131+
assert changes[0].changes[0].fixedFindings is not None
132+
assert changes[0].changes[0].fixedFindings[0].id == "4"
133+
assert changes[0].changes[0].fixedFindings[0].rule.id == RULE_ID
134+
assert changes[0].changes[1].fixedFindings is not None
135+
assert changes[0].changes[1].fixedFindings[0].id == "3"
136+
assert changes[0].changes[1].fixedFindings[0].rule.id == RULE_ID
137137

138138
@mock.patch("codemodder.codemods.api.FileContext.add_dependency")
139139
def test_pickle_loads(self, adds_dependency, tmpdir):

0 commit comments

Comments
 (0)