Skip to content

Commit

Permalink
feat(model): Handle excluded affected paths OrtResult.getIssues()
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Viernau <[email protected]>
  • Loading branch information
fviernau committed Apr 23, 2024
1 parent da6e769 commit ba6dc0f
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
27 changes: 26 additions & 1 deletion model/src/main/kotlin/OrtResult.kt
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,29 @@ data class OrtResult(
resolvedConfiguration.packageConfigurations.orEmpty().groupBy { it.id }
}

private val excludedAffectedPathIssuesForId: Map<Identifier, Set<Issue>> by lazy {
buildMap<Identifier, MutableSet<Issue>> {
scanner?.getAllScanResults().orEmpty().forEach { (id, scanResults) ->
scanResults.forEach { scanResult ->
val pathExcludes = getPathExcludes(id, scanResult.provenance)

scanResult.summary.issues.forEach { issue ->
if (issue.affectedPath != null && pathExcludes.any { it.matches(issue.affectedPath) }) {
getOrPut(id, { mutableSetOf() }) += issue

Check notice on line 164 in model/src/main/kotlin/OrtResult.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Lambda argument inside parentheses

Lambda argument should be moved out of parentheses
}
}
}
}
}
}

private fun getPathExcludes(id: Identifier, provenance: Provenance) =
if (isProject(id)) {
repository.config.excludes.paths
} else {
getPackageConfigurations(id, provenance).flatMapTo(mutableSetOf()) { it.pathExcludes }
}

/**
* A map of projects and their excluded state. Calculating this map once brings massive performance improvements
* when querying projects in large analyzer results.
Expand Down Expand Up @@ -264,7 +287,9 @@ data class OrtResult(
if (omitExcluded && isExcluded(id)) return@mapNotNull null

val filteredIssues = issues.filterTo(mutableSetOf()) {
(!omitResolved || !isResolved(it)) && it.severity >= minSeverity
(!omitResolved || !isResolved(it))
&& it.severity >= minSeverity
&& it !in excludedAffectedPathIssuesForId[id].orEmpty()
}

filteredIssues.takeUnless { it.isEmpty() }?.let { id to it }
Expand Down
74 changes: 74 additions & 0 deletions model/src/test/kotlin/OrtResultTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package org.ossreviewtoolkit.model

import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.WordSpec
import io.kotest.matchers.collections.beEmpty
import io.kotest.matchers.collections.containExactlyInAnyOrder
import io.kotest.matchers.collections.haveSize
import io.kotest.matchers.collections.shouldContain
Expand Down Expand Up @@ -247,6 +248,79 @@ class OrtResultTest : WordSpec({

openIssues.map { it.message } shouldHaveSingleElement "Included issue"
}

"omit scan issues with excluded affected path" {
val projectId = Identifier("Maven:org.oss-review-toolkit:example-project:1.0")
val vcs = VcsInfo(
type = VcsType.GIT,
url = "https:/github.com/example.project.git",
revision = "0000000000000000000000000000000000000000",
path = ""
)

val ortResult = OrtResult.EMPTY.copy(
repository = Repository.EMPTY.copy(
vcs = vcs,
vcsProcessed = vcs,
config = RepositoryConfiguration(
excludes = Excludes(
paths = listOf(
PathExclude(
pattern = "test/**",
reason = PathExcludeReason.TEST_OF
)
)
)
)
),
analyzer = AnalyzerRun.EMPTY.copy(
result = AnalyzerResult.EMPTY.copy(
projects = setOf(
Project.EMPTY.copy(
id = projectId,
definitionFilePath = "pom.xml",
declaredLicenses = emptySet(),
vcsProcessed = vcs
)
)
)
),
scanner = ScannerRun.EMPTY.copy(
scanners = mapOf(projectId to setOf("ScanCode")),
provenances = setOf(
ProvenanceResolutionResult(
id = projectId,
packageProvenance = RepositoryProvenance(
vcsInfo = vcs,
resolvedRevision = vcs.revision
)
)
),
scanResults = setOf(
ScanResult(
provenance = RepositoryProvenance(
vcsInfo = vcs,
resolvedRevision = vcs.revision
),
scanner = ScannerDetails.EMPTY.copy(name = "ScanCode"),
summary = ScanSummary.EMPTY.copy(
issues = listOf(
Issue(
message = "Included issue",
source = "ScanCode",
affectedPath = "test/assets/asset.json"
)
)
)
)
)
)
)

val issues = ortResult.getOpenIssues()

issues should beEmpty()
}
}

"dependencyNavigator" should {
Expand Down

0 comments on commit ba6dc0f

Please sign in to comment.