@@ -23,11 +23,17 @@ 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
30
+ import org.ossreviewtoolkit.model.RepositoryProvenance
29
31
import org.ossreviewtoolkit.model.ScanSummary
30
32
import org.ossreviewtoolkit.model.TextLocation
33
+ import org.ossreviewtoolkit.model.VcsInfo
34
+ import org.ossreviewtoolkit.model.VcsType
35
+ import org.ossreviewtoolkit.model.utils.Snippet
36
+ import org.ossreviewtoolkit.model.utils.SnippetFinding
31
37
import org.ossreviewtoolkit.utils.spdx.SpdxConstants
32
38
import org.ossreviewtoolkit.utils.spdx.SpdxExpression
33
39
import org.ossreviewtoolkit.utils.spdx.calculatePackageVerificationCode
@@ -64,11 +70,25 @@ internal fun generateSummary(
64
70
): ScanSummary {
65
71
val licenseFindings = mutableListOf<LicenseFinding >()
66
72
val copyrightFindings = mutableListOf<CopyrightFinding >()
73
+ val snippetFindings = mutableSetOf<SnippetFinding >()
67
74
68
75
result.forEach { (_, scanResponses) ->
69
76
scanResponses.forEach { scanResponse ->
70
- licenseFindings + = getLicenseFindings(scanResponse, detectedLicenseMapping)
71
- copyrightFindings + = getCopyrightFindings(scanResponse)
77
+ if (scanResponse.id == IdentificationType .FILE ) {
78
+ licenseFindings + = getLicenseFindings(scanResponse, detectedLicenseMapping)
79
+ copyrightFindings + = getCopyrightFindings(scanResponse)
80
+ }
81
+
82
+ if (scanResponse.id == IdentificationType .SNIPPET ) {
83
+ val file = requireNotNull(scanResponse.file)
84
+ val lines = requireNotNull(scanResponse.lines)
85
+ val sourceLocation = convertLines(file, lines)
86
+ val snippets = getSnippets(scanResponse)
87
+
88
+ snippets.forEach {
89
+ snippetFindings + = SnippetFinding (sourceLocation, it)
90
+ }
91
+ }
72
92
}
73
93
}
74
94
@@ -78,6 +98,7 @@ internal fun generateSummary(
78
98
packageVerificationCode = verificationCode,
79
99
licenseFindings = licenseFindings.toSortedSet(),
80
100
copyrightFindings = copyrightFindings.toSortedSet(),
101
+ snippetFindings = snippetFindings.toSortedSet(),
81
102
issues = emptyList()
82
103
)
83
104
}
@@ -131,3 +152,46 @@ private fun getCopyrightFindings(scanResponse: ScanResponse): List<CopyrightFind
131
152
)
132
153
}
133
154
}
155
+
156
+ /* *
157
+ * Get the snippet findings from the given [scanResponse]. If a snippet returned by ScanOSS contains several Purls,
158
+ * several snippets are created in ORT each containing a single Purl.
159
+ */
160
+ private fun getSnippets (scanResponse : ScanResponse ): Set <Snippet > {
161
+ val matched = requireNotNull(scanResponse.matched)
162
+ val fileUrl = requireNotNull(scanResponse.fileUrl)
163
+ val ossLines = requireNotNull(scanResponse.ossLines)
164
+ val url = requireNotNull(scanResponse.url)
165
+ val purls = requireNotNull(scanResponse.purl)
166
+
167
+ val licenses = scanResponse.licenses.map { license ->
168
+ SpdxExpression .parse(license.name)
169
+ }.toSet()
170
+
171
+ val score = matched.substringBeforeLast(" %" ).toFloat()
172
+ val snippetLocation = convertLines(fileUrl, ossLines)
173
+ // TODO: No resolved revision is available. Should a ArtifactProvenance be created instead ?
174
+ val snippetProvenance = RepositoryProvenance (VcsInfo (VcsType .UNKNOWN , url, " " ), " ." )
175
+
176
+ return purls.map {
177
+ Snippet (
178
+ score,
179
+ snippetLocation,
180
+ snippetProvenance,
181
+ it,
182
+ licenses.distinct().reduce(SpdxExpression ::and ).sorted()
183
+ )
184
+ }.toSortedSet()
185
+ }
186
+
187
+ /* *
188
+ * Split a [lineRange] returned by ScanOSS such as 1-321 into a [TextLocation] for the given [file].
189
+ */
190
+ private fun convertLines (file : String , lineRange : String ): TextLocation {
191
+ val splitLines = lineRange.split(" -" )
192
+ return if (splitLines.size == 2 ) {
193
+ TextLocation (file, splitLines.first().toInt(), splitLines.last().toInt())
194
+ } else {
195
+ TextLocation (file, splitLines.first().toInt())
196
+ }
197
+ }
0 commit comments