-
Notifications
You must be signed in to change notification settings - Fork 314
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(model)!: Group snippets by source file matching lines
All snippets matching the same source file location, line ranges included, are now grouped in the snippet model. This commit contains the model change and the support in the FossID scanner. ScanOSS support has not been added but the current implementation is still functional. Signed-off-by: Nicolas Nobelis <[email protected]>
- Loading branch information
1 parent
2ca66d5
commit 641f520
Showing
10 changed files
with
222 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
plugins/scanners/fossid/src/test/kotlin/FossIdSnippetMappingTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* | ||
* Copyright (C) 2023 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* License-Filename: LICENSE | ||
*/ | ||
|
||
package org.ossreviewtoolkit.plugins.scanners.fossid | ||
|
||
import io.kotest.core.spec.style.WordSpec | ||
import io.kotest.matchers.collections.beEmpty | ||
import io.kotest.matchers.collections.containExactly | ||
import io.kotest.matchers.collections.shouldHaveSize | ||
import io.kotest.matchers.should | ||
import io.kotest.matchers.shouldBe | ||
|
||
import org.ossreviewtoolkit.clients.fossid.PolymorphicList | ||
import org.ossreviewtoolkit.clients.fossid.model.result.MatchType | ||
import org.ossreviewtoolkit.clients.fossid.model.result.MatchedLines | ||
import org.ossreviewtoolkit.clients.fossid.model.result.Snippet | ||
import org.ossreviewtoolkit.model.Issue | ||
import org.ossreviewtoolkit.model.TextLocation | ||
import org.ossreviewtoolkit.utils.test.shouldNotBeNull | ||
|
||
class FossIdSnippetMappingTest : WordSpec({ | ||
"mapSnippetFindings" should { | ||
"group snippets by source file location" { | ||
val issues = mutableListOf<Issue>() | ||
val listSnippets = mapOf( | ||
"src/main/java/Tokenizer.java" to setOf( | ||
createSnippet( | ||
1, | ||
MatchType.FULL, | ||
"pkg:github/vdurmont/[email protected]", | ||
"MIT", | ||
"src/main/java/com/vdurmont/semver4j/Tokenizer.java" | ||
), | ||
createSnippet( | ||
2, | ||
MatchType.FULL, | ||
"pkg:maven/com.vdurmont/[email protected]", | ||
"MIT", | ||
"com/vdurmont/semver4j/Tokenizer.java" | ||
) | ||
), | ||
"src/main/java/com/vdurmont/semver4j/Requirement.java" to setOf( | ||
createSnippet( | ||
3, | ||
MatchType.PARTIAL, | ||
"pkg:github/vdurmont/[email protected]", | ||
"MIT", | ||
"com/vdurmont/semver4j/Requirement.java" | ||
) | ||
) | ||
) | ||
val localFile = ((1..24) + (45..675)).toPolymorphicList() | ||
val remoteFile = (1..655).toPolymorphicList() | ||
// There is no matched line information for full match snippets. | ||
val snippetMatchedLines = mapOf(3 to MatchedLines(localFile, remoteFile)) | ||
val rawResults = RawResults( | ||
emptyList(), | ||
emptyList(), | ||
emptyList(), | ||
emptyList(), | ||
listSnippets, | ||
snippetMatchedLines | ||
) | ||
|
||
val mappedSnippets = mapSnippetFindings(rawResults, issues) | ||
|
||
issues should beEmpty() | ||
mappedSnippets shouldHaveSize 3 | ||
mappedSnippets.first().apply { | ||
sourceLocation shouldBe TextLocation("src/main/java/Tokenizer.java", TextLocation.UNKNOWN_LINE) | ||
snippets shouldHaveSize 2 | ||
snippets.map { it.purl } should containExactly( | ||
"pkg:github/vdurmont/[email protected]", | ||
"pkg:maven/com.vdurmont/[email protected]" | ||
) | ||
} | ||
mappedSnippets.elementAtOrNull(1) shouldNotBeNull { | ||
sourceLocation shouldBe TextLocation("src/main/java/com/vdurmont/semver4j/Requirement.java", 1, 24) | ||
snippets.map { it.purl } should containExactly("pkg:github/vdurmont/[email protected]") | ||
} | ||
mappedSnippets.elementAtOrNull(2) shouldNotBeNull { | ||
sourceLocation shouldBe TextLocation("src/main/java/com/vdurmont/semver4j/Requirement.java", 45, 675) | ||
snippets.map { it.purl } should containExactly("pkg:github/vdurmont/[email protected]") | ||
} | ||
} | ||
} | ||
}) | ||
|
||
private fun createSnippet(id: Int, matchType: MatchType, purl: String, license: String, file: String) = | ||
Snippet( | ||
id = id, | ||
created = "", | ||
scanId = 1, | ||
scanFileId = 1, | ||
fileId = 1, | ||
matchType = matchType, | ||
reason = null, | ||
author = null, | ||
artifact = null, | ||
version = null, | ||
purl = purl, | ||
artifactLicense = license, | ||
artifactLicenseCategory = null, | ||
releaseDate = null, | ||
mirror = null, | ||
file = file, | ||
fileLicense = null, | ||
url = "", | ||
hits = null, | ||
size = null, | ||
updated = null, | ||
cpe = null, | ||
score = "1.0", | ||
matchFileId = null, | ||
classification = null, | ||
highlighting = null | ||
) | ||
|
||
private fun IntRange.toPolymorphicList() = toList().toPolymorphicList() | ||
private fun List<Int>.toPolymorphicList() = PolymorphicList(this) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters