Skip to content

Commit 4c5cfe6

Browse files
committed
Bugfixes and added parameters for generation metadata
1 parent fba9cff commit 4c5cfe6

File tree

2 files changed

+55
-12
lines changed

2 files changed

+55
-12
lines changed

src/codemodder/codetf/v3/codetf.py

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from ..v2.codetf import AIMetadata as AIMetadatav2
1212
from ..v2.codetf import ChangeSet as v2ChangeSet
1313
from ..v2.codetf import CodeTF as CodeTFv2
14+
from ..v2.codetf import Finding as v2Finding
1415
from ..v2.codetf import Result
1516
from ..v2.codetf import Run as Runv2
1617

@@ -151,31 +152,55 @@ def from_v2_aimetadata(ai_metadata: AIMetadatav2) -> AIMetadata:
151152
)
152153

153154

154-
def from_v2_result_per_finding(result: Result) -> FixResult | None:
155+
def from_v2_result_per_finding(
156+
result: Result,
157+
strategy: Strategy | None = None,
158+
ai_metadata: AIMetadata | None = None,
159+
provisional: bool | None = None,
160+
) -> FixResult | None:
155161
"""
156162
This transformation assumes that the v2 result will only contain a single fixedFinding for all changesets.
157163
"""
164+
165+
changeset: v2ChangeSet | None = None
166+
finding: v2Finding | None = None
158167
# Find the changeset with a fixedFinding
159-
try:
160-
changeset: v2ChangeSet = next(cs for cs in result.changeset if cs.fixedFindings)
161-
except StopIteration:
162-
logger.debug("Either no changesets or no fixedFinding in the given Result")
168+
for cs in result.changeset:
169+
if cs.fixedFindings:
170+
changeset = cs
171+
finding = cs.fixedFindings[0]
172+
break
173+
else:
174+
# check each individual change
175+
for change in cs.changes:
176+
if change.fixedFindings:
177+
changeset = cs
178+
finding = change.fixedFindings[0]
179+
break
180+
if changeset is None or finding is None:
181+
logger.debug("Either no changesets or fixed finding in the result")
163182
return None
164183

165-
assert changeset.fixedFindings
166-
finding = changeset.fixedFindings[0]
167-
168184
v3changesets = [
169185
ChangeSet(
170186
path=cs.path, diff=cs.diff, changes=[c.to_common() for c in cs.changes]
171187
)
172188
for cs in result.changeset
173189
]
174190

191+
# Generate the GenerationMetadata from the changeset if not passed as a parameter
192+
fix_result_strategy = strategy or (
193+
Strategy.ai if changeset.ai else Strategy.deterministic
194+
)
195+
fix_result_ai_metadata = ai_metadata or (
196+
from_v2_aimetadata(changeset.ai) if changeset.ai else None
197+
)
198+
fix_result_provisional = provisional or changeset.provisional or False
199+
175200
generation_metadata = GenerationMetadata(
176-
strategy=Strategy.ai if changeset.ai else Strategy.deterministic,
177-
ai=from_v2_aimetadata(changeset.ai) if changeset.ai else None,
178-
provisional=False,
201+
strategy=fix_result_strategy,
202+
ai=fix_result_ai_metadata,
203+
provisional=fix_result_provisional,
179204
)
180205

181206
fix_metadata = FixMetadata(

tests/test_codetf.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,15 @@
2323
PackageResult,
2424
Strategy,
2525
)
26+
from codemodder.codetf.v3.codetf import (
27+
AIMetadata,
28+
)
2629
from codemodder.codetf.v3.codetf import Finding as FindingV3
2730
from codemodder.codetf.v3.codetf import (
2831
FixStatusType,
32+
)
33+
from codemodder.codetf.v3.codetf import Strategy as StrategyV3
34+
from codemodder.codetf.v3.codetf import (
2935
from_v2,
3036
from_v2_result,
3137
from_v2_result_per_finding,
@@ -376,12 +382,24 @@ def test_v2_result_to_v3_per_finding():
376382
],
377383
unfixedFindings=[],
378384
)
379-
fix_result = from_v2_result_per_finding(result)
385+
fix_result = from_v2_result_per_finding(
386+
result,
387+
strategy=StrategyV3.ai,
388+
provisional=True,
389+
ai_metadata=AIMetadata(provider="pixee"),
390+
)
380391
assert fix_result
381392
assert len(fix_result.changeSets) == 3
382393
all_paths = {cs.path for cs in fix_result.changeSets}
383394
assert "app/pom.xml" in all_paths
384395
assert "pom.xml" in all_paths
396+
assert fix_result.fixMetadata
397+
# Assert that the metadata complies with the passed parameters
398+
assert fix_result.fixMetadata.generation.strategy == StrategyV3.ai
399+
assert fix_result.fixMetadata.generation.provisional
400+
assert fix_result.fixMetadata.generation.ai
401+
assert fix_result.fixMetadata.generation.ai.provider
402+
assert fix_result.fixMetadata.generation.ai.provider == "pixee"
385403

386404

387405
def test_v2_to_v3_conversion():

0 commit comments

Comments
 (0)