Skip to content

Commit

Permalink
fix(fossid-webapp): Count snippets when enforcing the snippet limit
Browse files Browse the repository at this point in the history
The current logic is wrong: the snippet limit is applied to ORT snippet
findings. However, a snippet finding refers to a single source code
location and contains generally multiple snippets.
Hence, those snippets must be counted when applying the limit.
Please note that the new logic still garantees that all the snippets of a
snippet finding are returned, for unity.

This is a fixup for 9223e90.

Signed-off-by: Nicolas Nobelis <nicolas.nobelis@bosch.com>
  • Loading branch information
nnobelis authored and sschuberth committed May 23, 2024
1 parent ce2e767 commit 2bfeec1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
8 changes: 6 additions & 2 deletions plugins/scanners/fossid/src/main/kotlin/FossIdScanResults.kt
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,12 @@ internal suspend fun mapSnippetFindings(
snippetLicenseFindings
)

runningSnippetCount += mappedSnippets.size
results += mappedSnippets
val snippetFindingIterator = mappedSnippets.iterator()
while (runningSnippetCount < snippetsLimit && snippetFindingIterator.hasNext()) {
val snippetFinding = snippetFindingIterator.next()
runningSnippetCount += snippetFinding.snippets.size
results += snippetFinding
}
}

if (runningSnippetCount >= snippetsLimit) {
Expand Down
55 changes: 50 additions & 5 deletions plugins/scanners/fossid/src/test/kotlin/FossIdSnippetChoiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ class FossIdSnippetChoiceTest : WordSpec({
}
}

"respect the snippet limit when listing snippets" {
"respect the snippet limit when listing snippets (single snippet per finding)" {
val projectCode = projectCode(PROJECT)
val scanCode = scanCode(PROJECT, null)
val config = createConfig(deltaScans = false, fetchSnippetMatchedLines = true, snippetsLimit = 2)
Expand Down Expand Up @@ -766,6 +766,51 @@ class FossIdSnippetChoiceTest : WordSpec({
}
}

"respect the snippet limit when listing snippets (multiple snippets per finding)" {
val projectCode = projectCode(PROJECT)
val scanCode = scanCode(PROJECT, null)
val config = createConfig(deltaScans = false, fetchSnippetMatchedLines = true, snippetsLimit = 2)
val vcsInfo = createVcsInfo()
val scan = createScan(vcsInfo.url, "${vcsInfo.revision}_other", scanCode)
val pkgId = createIdentifier(index = 42)

FossIdRestService.create(config.serverUrl)
.expectProjectRequest(projectCode)
.expectListScans(projectCode, listOf(scan))
.expectCheckScanStatus(scanCode, ScanStatus.FINISHED)
.expectCreateScan(projectCode, scanCode, vcsInfo, "")
.expectDownload(scanCode)
.mockFiles(
scanCode,
pendingFiles = listOf(FILE_1, FILE_2),
snippets = listOf(
createSnippet(0, FILE_1, PURL_1),
createSnippet(1, FILE_1, PURL_2)
),
matchedLines = mapOf(
0 to MatchedLines.create((10..20).toList(), (10..20).toList()),
1 to MatchedLines.create((10..20).toList(), (20..30).toList())
)
)

val fossId = createFossId(config)

val summary = fossId.scan(createPackage(pkgId, vcsInfo)).summary

summary.snippetFindings shouldHaveSize 1
summary.snippetFindings.forEach {
it.sourceLocation.path shouldBe FILE_1
}

summary.snippetFindings.flatMap { it.snippets } shouldHaveSize 2

summary.issues.forAtLeastOne {
it.message shouldBe "The snippets limit of 2 has been reached. To see the possible remaining " +
"snippets, please perform a snippet choice for the snippets presents in the snippet report an " +
"rerun the scan."
}
}

"list all the snippets of a file even if if goes over the snippets limit" {
val projectCode = projectCode(PROJECT)
val scanCode = scanCode(PROJECT, null)
Expand Down Expand Up @@ -844,12 +889,12 @@ class FossIdSnippetChoiceTest : WordSpec({

val summary = fossId.scan(createPackage(pkgId, vcsInfo), snippetChoices = snippetChoices).summary

summary.snippetFindings shouldHaveSize 3
summary.snippetFindings.first().apply {
summary.snippetFindings shouldHaveSize 2
summary.snippetFindings.first().apply { // one snippet is remaining for file 1
sourceLocation.path shouldBe FILE_1
}
summary.snippetFindings.drop(1).forEach {
it.sourceLocation.path shouldBe FILE_2
summary.snippetFindings.last().apply { // one snippet for file 2, then the limit is reached
sourceLocation.path shouldBe FILE_2
}

summary.issues.forAtLeastOne {
Expand Down

0 comments on commit 2bfeec1

Please sign in to comment.