@@ -23,11 +23,13 @@ import java.io.File
23
23
import java.time.Instant
24
24
25
25
import org.ossreviewtoolkit.clients.scanoss.FullScanResponse
26
+ import org.ossreviewtoolkit.clients.scanoss.model.IdentificationType
26
27
import org.ossreviewtoolkit.clients.scanoss.model.ScanResponse
27
28
import org.ossreviewtoolkit.model.CopyrightFinding
28
29
import org.ossreviewtoolkit.model.LicenseFinding
29
30
import org.ossreviewtoolkit.model.ScanSummary
30
31
import org.ossreviewtoolkit.model.TextLocation
32
+ import org.ossreviewtoolkit.model.utils.SnippetFinding
31
33
import org.ossreviewtoolkit.utils.spdx.SpdxConstants
32
34
import org.ossreviewtoolkit.utils.spdx.SpdxExpression
33
35
import org.ossreviewtoolkit.utils.spdx.calculatePackageVerificationCode
@@ -64,11 +66,19 @@ internal fun generateSummary(
64
66
): ScanSummary {
65
67
val licenseFindings = mutableListOf<LicenseFinding >()
66
68
val copyrightFindings = mutableListOf<CopyrightFinding >()
69
+ val snippetFindings = mutableMapOf<String , MutableSet <SnippetFinding >>()
67
70
68
71
result.forEach { (_, scanResponses) ->
69
72
scanResponses.forEach { scanResponse ->
70
- licenseFindings + = getLicenseFindings(scanResponse, detectedLicenseMapping)
71
- copyrightFindings + = getCopyrightFindings(scanResponse)
73
+ if (scanResponse.id == IdentificationType .FILE ) {
74
+ licenseFindings + = getLicenseFindings(scanResponse, detectedLicenseMapping)
75
+ copyrightFindings + = getCopyrightFindings(scanResponse)
76
+ }
77
+ if (scanResponse.id == IdentificationType .SNIPPET ) {
78
+ val path = requireNotNull(scanResponse.file)
79
+ val findings = snippetFindings.getOrPut(path) { mutableSetOf () }
80
+ findings + = getSnippetFindings(scanResponse)
81
+ }
72
82
}
73
83
}
74
84
@@ -78,6 +88,7 @@ internal fun generateSummary(
78
88
packageVerificationCode = verificationCode,
79
89
licenseFindings = licenseFindings.toSortedSet(),
80
90
copyrightFindings = copyrightFindings.toSortedSet(),
91
+ snippetFindings = snippetFindings,
81
92
issues = emptyList()
82
93
)
83
94
}
@@ -131,3 +142,39 @@ private fun getCopyrightFindings(scanResponse: ScanResponse): List<CopyrightFind
131
142
)
132
143
}
133
144
}
145
+
146
+ /* *
147
+ * Get the snippet findings from the given [scanResponse].
148
+ */
149
+ private fun getSnippetFindings (scanResponse : ScanResponse ): Set <SnippetFinding > {
150
+ val vendor = requireNotNull(scanResponse.vendor)
151
+ val component = requireNotNull(scanResponse.component)
152
+ val version = requireNotNull(scanResponse.version)
153
+ val matched = requireNotNull(scanResponse.matched)
154
+ val file = requireNotNull(scanResponse.file)
155
+ val lines = requireNotNull(scanResponse.lines)
156
+ val fileUrl = requireNotNull(scanResponse.fileUrl)
157
+ val ossLines = requireNotNull(scanResponse.ossLines)
158
+
159
+ val licenses = scanResponse.licenses.map { license ->
160
+ SpdxExpression .parse(license.name)
161
+ }.toSet()
162
+
163
+ val score = matched.substringBeforeLast(" %" ).toFloat()
164
+ val sourceLocation = convertLines(file, lines)
165
+ val snippetLocation = convertLines(fileUrl, ossLines)
166
+
167
+ return setOf (SnippetFinding (vendor, component, version, licenses, score, sourceLocation, snippetLocation))
168
+ }
169
+
170
+ /* *
171
+ * Split a [lineRange] returned by ScanOSS such as 1-321 into a [TextLocation] for the given [file].
172
+ */
173
+ private fun convertLines (file : String , lineRange : String ): TextLocation {
174
+ val splitLines = lineRange.split(" -" )
175
+ return if (splitLines.size == 2 ) {
176
+ TextLocation (file, splitLines.first().toInt(), splitLines.last().toInt())
177
+ } else {
178
+ TextLocation (file, splitLines.first().toInt())
179
+ }
180
+ }
0 commit comments