Skip to content

Commit

Permalink
OrtResult: Enhance getRuleViolations to omit resolved violations
Browse files Browse the repository at this point in the history
Provide the possibility to remove resolved violations and violations
below a specified threshold.
This is not used in the ORT code, but is intended to be used in the
`rules.kts` and `notifications.kts` scripts.

Signed-off-by: Marcel Bochtler <marcel.bochtler@bosch.io>
  • Loading branch information
MarcelBochtler committed Sep 14, 2021
1 parent f503b31 commit d7083d6
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 2 deletions.
25 changes: 23 additions & 2 deletions model/src/main/kotlin/OrtResult.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2017-2021 HERE Europe B.V.
* Copyright (C) 2021 Bosch.IO GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -403,10 +404,30 @@ data class OrtResult(
fun getAdvisorResultsForId(id: Identifier): List<AdvisorResult> = advisorResultsById[id].orEmpty()

/**
* Return all [RuleViolation]s contained in this [OrtResult].
* Return all [RuleViolation]s contained in this [OrtResult]. Optionally exclude resolved violations with
* [omitResolved] and remove violations below the [minSeverity].
*/
@JsonIgnore
fun getRuleViolations(): List<RuleViolation> = evaluator?.violations.orEmpty()
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) {
val resolutions = getResolutions().ruleViolations

severeViolations.filter { violation ->
resolutions.none { resolution ->
resolution.matches(violation)
}
}
} else {
severeViolations
}
}

@JsonIgnore
fun getExcludes(): Excludes = repository.config.excludes
Expand Down
94 changes: 94 additions & 0 deletions model/src/test/kotlin/OrtResultTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2017-2019 HERE Europe B.V.
* Copyright (C) 2021 Bosch.IO GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +25,7 @@ import io.kotest.core.spec.style.WordSpec
import io.kotest.matchers.collections.containExactly
import io.kotest.matchers.collections.containExactlyInAnyOrder
import io.kotest.matchers.collections.haveSize
import io.kotest.matchers.collections.shouldContainExactly
import io.kotest.matchers.should
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldMatch
Expand All @@ -32,6 +34,10 @@ import io.kotest.matchers.types.beInstanceOf
import java.lang.IllegalArgumentException

import org.ossreviewtoolkit.model.config.AnalyzerConfiguration
import org.ossreviewtoolkit.model.config.RepositoryConfiguration
import org.ossreviewtoolkit.model.config.Resolutions
import org.ossreviewtoolkit.model.config.RuleViolationResolution
import org.ossreviewtoolkit.model.config.RuleViolationResolutionReason
import org.ossreviewtoolkit.utils.Environment
import org.ossreviewtoolkit.utils.test.readOrtResult

Expand Down Expand Up @@ -159,4 +165,92 @@ class OrtResultTest : WordSpec({
ortResult.dependencyNavigator should beInstanceOf<DependencyGraphNavigator>()
}
}

"getRuleViolations" should {
"return unfiltered rule violations if omitResolved is false" {
val ortResult = OrtResult.EMPTY.copy(
repository = Repository.EMPTY.copy(
config = RepositoryConfiguration(
resolutions = Resolutions(
ruleViolations = listOf(
RuleViolationResolution(
"Rule violation message to resolve",
RuleViolationResolutionReason.EXAMPLE_OF_EXCEPTION,
"comment"
)
)
)
)
),
evaluator = EvaluatorRun(
violations = listOf(
RuleViolation(
rule = "rule id",
pkg = Identifier("Maven", "org.ossreviewtoolkit", "resolved-violation", "0.8.15"),
license = null,
licenseSource = null,
severity = Severity.HINT,
message = "Rule violation message to resolve",
howToFix = ""
)
)
)
)

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

"drop resolved rule violations if omitResolved is true" {
val ortResult = OrtResult.EMPTY.copy(
repository = Repository.EMPTY.copy(
config = RepositoryConfiguration(
resolutions = Resolutions(
ruleViolations = listOf(
RuleViolationResolution(
"Rule violation message to resolve",
RuleViolationResolutionReason.EXAMPLE_OF_EXCEPTION,
"comment"
)
)
)
)
),
evaluator = EvaluatorRun(
violations = listOf(
RuleViolation(
rule = "Resolved rule violation",
pkg = Identifier("Maven", "org.ossreviewtoolkit", "resolved-violation", "0.8.15"),
license = null,
licenseSource = null,
severity = Severity.ERROR,
message = "Rule violation message to resolve",
howToFix = ""
),
RuleViolation(
rule = "Rule violation without resolution",
pkg = Identifier("Maven", "com.example", "package-without-resolution", "1.0.0"),
license = null,
licenseSource = null,
severity = Severity.WARNING,
message = "Message without any resolution",
howToFix = ""
),
RuleViolation(
rule = "Rule violation below minSeverity",
pkg = Identifier("Maven", "com.example", "violation-below-threshold", "3.14"),
license = null,
licenseSource = null,
severity = Severity.HINT,
message = "Message without any resolution",
howToFix = ""
)
)
)
)

ortResult.getRuleViolations(omitResolved = true, minSeverity = Severity.WARNING).map { it.rule }
.shouldContainExactly("Rule violation without resolution")
}
}
})

0 comments on commit d7083d6

Please sign in to comment.