Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

model: Improve and align OrtResult.getIssues() #8507

Merged
merged 9 commits into from
Apr 12, 2024
47 changes: 26 additions & 21 deletions model/src/main/kotlin/OrtResult.kt
Original file line number Diff line number Diff line change
Expand Up @@ -244,16 +244,31 @@ data class OrtResult(
}

/**
* Return a map of all de-duplicated [Issue]s associated by [Identifier].
* Return a map of all de-duplicated [Issue]s associated by [Identifier]. If [omitExcluded] is set to true, excluded
* issues are omitted from the result. If [omitResolved] is set to true, resolved issues are omitted from the
* result. Issues with [severity][Issue.severity] below [minSeverity] are omitted from the result.
*/
@JsonIgnore
fun getIssues(): Map<Identifier, Set<Issue>> {
fun getIssues(
omitExcluded: Boolean = false,
omitResolved: Boolean = false,
minSeverity: Severity = Severity.entries.min()
): Map<Identifier, Set<Issue>> {
val analyzerIssues = analyzer?.result?.getAllIssues().orEmpty()
val scannerIssues = scanner?.getAllIssues().orEmpty()
val advisorIssues = advisor?.results?.getIssues().orEmpty()

val analyzerAndScannerIssues = analyzerIssues.zipWithCollections(scannerIssues)
return analyzerAndScannerIssues.zipWithCollections(advisorIssues)
val allIssues = analyzerIssues.zipWithCollections(scannerIssues).zipWithCollections(advisorIssues)

return allIssues.mapNotNull { (id, issues) ->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This hunk seems to have gone to the wrong commit.

if (omitExcluded && isExcluded(id)) return@mapNotNull null

val filteredIssues = issues.filterTo(mutableSetOf()) {
(!omitResolved || !isResolved(it)) && it.severity >= minSeverity
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All in all, this could now look something like this:

    return allIssues.mapNotNull { (id, issues) ->
        if (omitExcluded && isExcluded(id)) return@mapNotNull null

        val filteredIssues = issues.filterTo(mutableSetOf()) {
            (!omitResolved || !isResolved(it)) && it.severity >= minSeverity
        }

        filteredIssues.takeUnless { it.isEmpty() }?.let { id to it }
    }.toMap()


filteredIssues.takeUnless { it.isEmpty() }?.let { id to it }
}.toMap()
}

/**
Expand All @@ -278,10 +293,7 @@ data class OrtResult(
*/
@JsonIgnore
fun getOpenIssues(minSeverity: Severity = Severity.WARNING) =
getIssues()
.mapNotNull { (id, issues) -> issues.takeUnless { isExcluded(id) } }
.flatten()
.filter { it.severity >= minSeverity && !isResolved(it) }
getIssues(omitExcluded = true, omitResolved = true, minSeverity = minSeverity).values.flatten().distinct()
sschuberth marked this conversation as resolved.
Show resolved Hide resolved

/**
* Return a list of [PackageConfiguration]s for the given [packageId] and [provenance].
Expand Down Expand Up @@ -436,20 +448,13 @@ data class OrtResult(
* [omitResolved] and remove violations below the [minSeverity].
*/
@JsonIgnore
fun getRuleViolations(omitResolved: Boolean = false, minSeverity: Severity? = null): List<RuleViolation> {
val allViolations = evaluator?.violations.orEmpty()

val severeViolations = when (minSeverity) {
null -> allViolations
else -> allViolations.filter { it.severity >= minSeverity }
}

return if (omitResolved) {
severeViolations.filter { !isResolved(it) }
} else {
severeViolations
fun getRuleViolations(
sschuberth marked this conversation as resolved.
Show resolved Hide resolved
omitResolved: Boolean = false,
minSeverity: Severity = Severity.entries.min()
): List<RuleViolation> =
evaluator?.violations.orEmpty().filter {
(!omitResolved || !isResolved(it)) && it.severity >= minSeverity
}
}

/**
* Return the list of [ScanResult]s for the given [id].
Expand Down
16 changes: 8 additions & 8 deletions model/src/test/kotlin/OrtResultTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import org.ossreviewtoolkit.model.config.RuleViolationResolutionReason
import org.ossreviewtoolkit.utils.test.readOrtResult

class OrtResultTest : WordSpec({
"collectDependencies" should {
"collectDependencies()" should {
"be able to get all direct dependencies of a package" {
val ortResult = readOrtResult("src/test/assets/sbt-multi-project-example-expected-output.yml")
val id = Identifier("Maven:com.typesafe.akka:akka-stream_2.12:2.5.6")
Expand All @@ -59,7 +59,7 @@ class OrtResultTest : WordSpec({
}
}

"collectProjectsAndPackages" should {
"collectProjectsAndPackages()" should {
"be able to get all ids except for ones for sub-projects" {
val ortResult = readOrtResult("src/test/assets/gradle-all-dependencies-expected-result.yml")
val ids = ortResult.getProjectsAndPackages()
Expand All @@ -72,7 +72,7 @@ class OrtResultTest : WordSpec({
}
}

"getDefinitionFilePathRelativeToAnalyzerRoot" should {
"getDefinitionFilePathRelativeToAnalyzerRoot()" should {
"use the correct vcs" {
val vcs = VcsInfo(type = VcsType.GIT, url = "https://example.com/git", revision = "")
val nestedVcs1 = VcsInfo(type = VcsType.GIT, url = "https://example.com/git1", revision = "")
Expand Down Expand Up @@ -143,7 +143,7 @@ class OrtResultTest : WordSpec({
}
}

"getOpenIssues" should {
"getOpenIssues()" should {
"omit resolved issues" {
val ortResult = OrtResult.EMPTY.copy(
analyzer = AnalyzerRun.EMPTY.copy(
Expand Down Expand Up @@ -269,8 +269,8 @@ class OrtResultTest : WordSpec({
}
}

"getRuleViolations" should {
"return unfiltered rule violations if omitResolved is false" {
"getRuleViolations()" should {
"return unfiltered rule violations if omitResolved is false and minSeverity is HINT" {
val ortResult = OrtResult.EMPTY.copy(
repository = Repository.EMPTY.copy(
config = RepositoryConfiguration(
Expand Down Expand Up @@ -300,11 +300,11 @@ class OrtResultTest : WordSpec({
)
)

ortResult.getRuleViolations(omitResolved = false, minSeverity = null).map { it.rule }
ortResult.getRuleViolations(omitResolved = false, minSeverity = Severity.entries.min()).map { it.rule }
.shouldContainExactly("rule id")
}

"drop resolved rule violations if omitResolved is true" {
"drop violations which are resolved or below minSeverity if omitResolved is true and minSeverity is WARNING" {
val ortResult = OrtResult.EMPTY.copy(
evaluator = EvaluatorRun.EMPTY.copy(
violations = listOf(
Expand Down
Loading