Skip to content

Commit f95ea64

Browse files
committed
Add ConvertTargetToSimpleJson for per-target conversion
This method allows converting a single TargetResults to SimpleJson format without flattening multiple targets together. Useful for tools like Frogbot that need to process each auto-detected target separately to maintain working directory associations when applying fixes.
1 parent 6ea4636 commit f95ea64

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

utils/results/conversion/convertor.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,25 @@ func (c *CommandResultsConvertor) ConvertToSimpleJson(cmdResults *results.Securi
8484
return parseCommandResults(c.Params, parser, cmdResults)
8585
}
8686

87+
// ConvertTargetToSimpleJson converts a single TargetResults to SimpleJson format.
88+
// This is useful when you want to process individual targets separately without flattening multiple targets together.
89+
// The function wraps the single target in a SecurityCommandResults and delegates to ConvertToSimpleJson.
90+
func (c *CommandResultsConvertor) ConvertTargetToSimpleJson(target *results.TargetResults, cmdResults *results.SecurityCommandResults) (simpleJsonResults formats.SimpleJsonResults, err error) {
91+
if target == nil {
92+
return formats.SimpleJsonResults{}, nil
93+
}
94+
95+
// Create a SecurityCommandResults with just this one target
96+
// Copy the metadata from the original command results to preserve context
97+
singleTargetResults := &results.SecurityCommandResults{
98+
ResultsMetaData: cmdResults.ResultsMetaData,
99+
Targets: []*results.TargetResults{target},
100+
}
101+
102+
// Use existing ConvertToSimpleJson logic
103+
return c.ConvertToSimpleJson(singleTargetResults)
104+
}
105+
87106
func (c *CommandResultsConvertor) ConvertToSarif(cmdResults *results.SecurityCommandResults) (sarifReport *sarif.Report, err error) {
88107
parser := sarifparser.NewCmdResultsSarifConverter(c.Params.PlatformUrl, c.Params.PatchBinaryPaths)
89108
return parseCommandResults(c.Params, parser, cmdResults)

utils/results/conversion/convertor_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,3 +799,77 @@ func getDockerScanTestResults(unique bool) (*results.SecurityCommandResults, val
799799
})
800800
return cmdResults, expected
801801
}
802+
803+
func TestConvertTargetToSimpleJson(t *testing.T) {
804+
target1 := &results.TargetResults{
805+
ScanTarget: results.ScanTarget{Target: "/path/to/project1"},
806+
ScaResults: &results.ScaScanResults{
807+
DeprecatedXrayResults: []services.ScanResponse{{
808+
Vulnerabilities: []services.Vulnerability{
809+
{
810+
Cves: []services.Cve{{Id: "CVE-2023-1234"}},
811+
Severity: "High",
812+
Components: map[string]services.Component{
813+
"pkg1": {
814+
FixedVersions: []string{"1.2.0"},
815+
ImpactPaths: [][]services.ImpactPathNode{{{ComponentId: "root"}, {ComponentId: "pkg1"}}},
816+
},
817+
},
818+
},
819+
},
820+
}},
821+
},
822+
}
823+
824+
target2 := &results.TargetResults{
825+
ScanTarget: results.ScanTarget{Target: "/path/to/project2"},
826+
ScaResults: &results.ScaScanResults{
827+
DeprecatedXrayResults: []services.ScanResponse{{
828+
Vulnerabilities: []services.Vulnerability{
829+
{
830+
Cves: []services.Cve{{Id: "CVE-2023-5678"}},
831+
Severity: "Critical",
832+
Components: map[string]services.Component{
833+
"pkg2": {
834+
FixedVersions: []string{"2.0.0"},
835+
ImpactPaths: [][]services.ImpactPathNode{{{ComponentId: "root"}, {ComponentId: "pkg2"}}},
836+
},
837+
},
838+
},
839+
},
840+
}},
841+
},
842+
}
843+
844+
cmdResults := &results.SecurityCommandResults{
845+
ResultsMetaData: results.ResultsMetaData{
846+
CmdType: utils.SourceCode,
847+
ResultContext: results.ResultContext{
848+
IncludeVulnerabilities: true,
849+
},
850+
},
851+
Targets: []*results.TargetResults{target1, target2},
852+
}
853+
854+
convertor := NewCommandResultsConvertor(ResultConvertParams{
855+
IncludeVulnerabilities: true,
856+
HasViolationContext: false,
857+
})
858+
859+
// Test converting first target only
860+
result1, err := convertor.ConvertTargetToSimpleJson(target1, cmdResults)
861+
assert.NoError(t, err)
862+
assert.NotEmpty(t, result1.Vulnerabilities)
863+
assert.Equal(t, 1, len(result1.Vulnerabilities))
864+
assert.Equal(t, "CVE-2023-1234", result1.Vulnerabilities[0].Cves[0].Id)
865+
866+
// Test converting second target only
867+
result2, err := convertor.ConvertTargetToSimpleJson(target2, cmdResults)
868+
assert.NoError(t, err)
869+
assert.NotEmpty(t, result2.Vulnerabilities)
870+
assert.Equal(t, 1, len(result2.Vulnerabilities))
871+
assert.Equal(t, "CVE-2023-5678", result2.Vulnerabilities[0].Cves[0].Id)
872+
873+
// Verify no flattening occurred
874+
assert.NotEqual(t, result1.Vulnerabilities[0].Cves[0].Id, result2.Vulnerabilities[0].Cves[0].Id)
875+
}

0 commit comments

Comments
 (0)