Skip to content

Commit 918be70

Browse files
isasmendiagussschuberth
authored andcommitted
refactor(scanoss): Improve snippet location pairing with direct mapping
Replace separate collection iteration with explicit source-to-OSS location pairing using zip operation. Signed-off-by: Agustin Isasmendi <[email protected]>
1 parent bedb564 commit 918be70

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

plugins/scanners/scanoss/src/main/kotlin/ScanOssResultParser.kt

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,7 @@ internal fun generateSummary(startTime: Instant, endTime: Instant, results: List
6363

6464
MatchType.snippet -> {
6565
val localFile = requireNotNull(result.filePath)
66-
val localLines = requireNotNull(details.lines)
67-
val sourceLocations = convertLines(localFile, localLines)
68-
val snippets = getSnippets(details)
69-
70-
// The number of snippets should match the number of source locations.
71-
if (sourceLocations.size != snippets.size) {
72-
logger.warn {
73-
"Unexpected mismatch in '$localFile': " +
74-
"${sourceLocations.size} source locations vs ${snippets.size} snippets. " +
75-
"This indicates a potential issue with line range conversion."
76-
}
77-
}
78-
79-
// Associate each source location with its corresponding snippet.
80-
sourceLocations.zip(snippets).forEach { (location, snippet) ->
81-
snippetFindings += SnippetFinding(location, setOf(snippet))
82-
}
66+
snippetFindings += createSnippetFindings(details, localFile)
8367
}
8468

8569
MatchType.none -> {
@@ -144,33 +128,52 @@ private fun getCopyrightFindings(details: ScanFileDetails, path: String): List<C
144128
}
145129

146130
/**
147-
* Get the snippet findings from the given [details]. If a snippet returned by ScanOSS contains several PURLs,
148-
* the function extracts the first PURL as the primary identifier while storing the remaining PURLs in additionalData
149-
* to preserve the complete information.
131+
* Create snippet findings from given [details] and [localFilePath]. If a snippet returned by ScanOSS contains several
132+
* PURLs the function extracts the first PURL as the primary identifier while storing the remaining PURLs in
133+
* additionalData to preserve the complete information.
150134
*/
151-
private fun getSnippets(details: ScanFileDetails): List<Snippet> {
135+
private fun createSnippetFindings(details: ScanFileDetails, localFilePath: String): Set<SnippetFinding> {
152136
val matched = requireNotNull(details.matched)
153137
val ossFile = requireNotNull(details.file)
154138
val ossLines = requireNotNull(details.ossLines)
139+
val localLines = requireNotNull(details.lines)
155140
val url = requireNotNull(details.url)
156141
val purls = requireNotNull(details.purls).toMutableList()
142+
143+
val score = matched.substringBeforeLast("%").toFloat()
157144
val primaryPurl = purls.removeFirstOrNull().orEmpty()
158145

159146
val license = details.licenseDetails.orEmpty()
160147
.map { license -> SpdxExpression.parse(license.name) }
161148
.toExpression()?.sorted() ?: SpdxLicenseIdExpression(SpdxConstants.NOASSERTION)
162149

163-
val score = matched.substringBeforeLast("%").toFloat()
164-
val ossLocations = convertLines(ossFile, ossLines)
165150
// TODO: No resolved revision is available. Should a ArtifactProvenance be created instead ?
166151
val vcsInfo = VcsHost.parseUrl(url.takeUnless { it == "none" }.orEmpty())
167152
val provenance = RepositoryProvenance(vcsInfo, ".")
168153

169154
val additionalData = purls.associateWith { "" }
170155

171-
// Create one snippet per location, using the first PURL as the primary identifier.
172-
return ossLocations.map { snippetLocation ->
173-
Snippet(score, snippetLocation, provenance, primaryPurl, license, additionalData)
156+
// Convert both local and OSS line ranges to source locations.
157+
val sourceLocations = convertLines(localFilePath, localLines)
158+
val ossLocations = convertLines(ossFile, ossLines)
159+
160+
// The number of source locations should match the number of oss locations.
161+
if (sourceLocations.size != ossLocations.size) {
162+
logger.warn {
163+
"Unexpected mismatch in '$localFilePath': " +
164+
"${sourceLocations.size} source locations vs ${ossLocations.size} oss locations. " +
165+
"This indicates a potential issue with line range conversion."
166+
}
167+
}
168+
169+
// Directly pair source locations with their corresponding OSS locations and create a SnippetFinding.
170+
return sourceLocations.zip(ossLocations).mapTo(mutableSetOf()) { (sourceLocation, ossLocation) ->
171+
SnippetFinding(
172+
sourceLocation,
173+
setOf(
174+
Snippet(score, ossLocation, provenance, primaryPurl, license, additionalData)
175+
)
176+
)
174177
}
175178
}
176179

0 commit comments

Comments
 (0)