From 74ebfe7026044fd50bc0255a78173ab600cc5675 Mon Sep 17 00:00:00 2001 From: Frank Viernau Date: Tue, 10 Aug 2021 13:10:59 +0200 Subject: [PATCH 1/2] Make the package identifier optional for rule violations A rule violation does not necessarily correspond to a specific package. For example it would not make sense to require a package identifier for a rule violation which represents an issue with the (ORT result) labels. Signed-off-by: Frank Viernau --- evaluator/src/main/kotlin/Rule.kt | 8 ++++---- model/src/main/kotlin/RuleViolation.kt | 2 +- reporter/src/main/kotlin/model/EvaluatedModelMapper.kt | 5 ++++- reporter/src/main/kotlin/model/EvaluatedRuleViolation.kt | 2 +- reporter/src/main/kotlin/reporters/StaticHtmlReporter.kt | 2 +- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/evaluator/src/main/kotlin/Rule.kt b/evaluator/src/main/kotlin/Rule.kt index b925ea20380ec..c928e385cca6a 100644 --- a/evaluator/src/main/kotlin/Rule.kt +++ b/evaluator/src/main/kotlin/Rule.kt @@ -143,7 +143,7 @@ abstract class Rule( */ fun issue( severity: Severity, - pkgId: Identifier, + pkgId: Identifier?, license: SpdxSingleLicenseExpression?, licenseSource: LicenseSource?, message: String, @@ -164,7 +164,7 @@ abstract class Rule( * Add a [hint][Severity.HINT] to the list of [violations]. */ fun hint( - pkgId: Identifier, + pkgId: Identifier?, license: SpdxSingleLicenseExpression?, licenseSource: LicenseSource?, message: String, @@ -176,7 +176,7 @@ abstract class Rule( * Add a [warning][Severity.WARNING] to the list of [violations]. */ fun warning( - pkgId: Identifier, + pkgId: Identifier?, license: SpdxSingleLicenseExpression?, licenseSource: LicenseSource?, message: String, @@ -188,7 +188,7 @@ abstract class Rule( * Add an [error][Severity.ERROR] to the list of [violations]. */ fun error( - pkgId: Identifier, + pkgId: Identifier?, license: SpdxSingleLicenseExpression?, licenseSource: LicenseSource?, message: String, diff --git a/model/src/main/kotlin/RuleViolation.kt b/model/src/main/kotlin/RuleViolation.kt index dc9d8c774dfe9..c1e8eb9efc051 100644 --- a/model/src/main/kotlin/RuleViolation.kt +++ b/model/src/main/kotlin/RuleViolation.kt @@ -30,7 +30,7 @@ data class RuleViolation( /** * The identifier of the package that caused this rule violation. */ - val pkg: Identifier, + val pkg: Identifier?, /** * The name of the license that caused this rule violation. Can be null if the rule does not work on licenses. diff --git a/reporter/src/main/kotlin/model/EvaluatedModelMapper.kt b/reporter/src/main/kotlin/model/EvaluatedModelMapper.kt index 031ffd8a3b4ae..238f37b7d50e4 100644 --- a/reporter/src/main/kotlin/model/EvaluatedModelMapper.kt +++ b/reporter/src/main/kotlin/model/EvaluatedModelMapper.kt @@ -360,7 +360,10 @@ internal class EvaluatedModelMapper(private val input: ReporterInput) { private fun addRuleViolation(ruleViolation: RuleViolation) { val resolutions = addResolutions(ruleViolation) - val pkg = packages[ruleViolation.pkg] ?: createEmptyPackage(ruleViolation.pkg) + val pkg = ruleViolation.pkg?.let { id -> + packages[id] ?: createEmptyPackage(id) + } + val license = ruleViolation.license?.let { licenses.addIfRequired(LicenseId(it.toString())) } val evaluatedViolation = EvaluatedRuleViolation( diff --git a/reporter/src/main/kotlin/model/EvaluatedRuleViolation.kt b/reporter/src/main/kotlin/model/EvaluatedRuleViolation.kt index 4b4d2ea486a49..82300f21744d8 100644 --- a/reporter/src/main/kotlin/model/EvaluatedRuleViolation.kt +++ b/reporter/src/main/kotlin/model/EvaluatedRuleViolation.kt @@ -32,7 +32,7 @@ import org.ossreviewtoolkit.model.config.RuleViolationResolution data class EvaluatedRuleViolation( @JsonInclude(JsonInclude.Include.NON_EMPTY) val rule: String, - val pkg: EvaluatedPackage, + val pkg: EvaluatedPackage?, @JsonInclude(JsonInclude.Include.NON_NULL) val license: LicenseId?, @JsonInclude(JsonInclude.Include.NON_NULL) diff --git a/reporter/src/main/kotlin/reporters/StaticHtmlReporter.kt b/reporter/src/main/kotlin/reporters/StaticHtmlReporter.kt index 6f62bc465247c..5c4b1f7cbfd19 100644 --- a/reporter/src/main/kotlin/reporters/StaticHtmlReporter.kt +++ b/reporter/src/main/kotlin/reporters/StaticHtmlReporter.kt @@ -295,7 +295,7 @@ class StaticHtmlReporter : Reporter { } } td { +ruleViolation.violation.rule } - td { +ruleViolation.violation.pkg.toCoordinates() } + td { +(ruleViolation.violation.pkg?.toCoordinates() ?: "-") } td { +if (ruleViolation.violation.license != null) { "${ruleViolation.violation.licenseSource}: ${ruleViolation.violation.license}" From c4ff4649cec7656354e35df65f51e35126fcac5e Mon Sep 17 00:00:00 2001 From: Frank Viernau Date: Mon, 9 Aug 2021 14:30:24 +0200 Subject: [PATCH 2/2] evaluator: Add the new rule type `OrtResultRule` Having a generic rule which is executed once per ORT result helps with use cases for which the more specific rules aren't a good fit. For example, a rule for checking the (ORT result) labels or the repository configuration. Signed-off-by: Frank Viernau --- evaluator/src/main/kotlin/OrtResultRule.kt | 48 ++++++++++++++++++++++ evaluator/src/main/kotlin/RuleSet.kt | 10 +++++ 2 files changed, 58 insertions(+) create mode 100644 evaluator/src/main/kotlin/OrtResultRule.kt diff --git a/evaluator/src/main/kotlin/OrtResultRule.kt b/evaluator/src/main/kotlin/OrtResultRule.kt new file mode 100644 index 0000000000000..593a42de4da84 --- /dev/null +++ b/evaluator/src/main/kotlin/OrtResultRule.kt @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2021 HERE Europe B.V. + * + * 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.evaluator + +import org.ossreviewtoolkit.model.OrtResult + +/** + * A [Rule] to check an [OrtResult]. + */ +open class OrtResultRule( + ruleSet: RuleSet, + name: String, + + /** + * The [OrtResult] to check. + */ + val ortResult: OrtResult, +) : Rule(ruleSet, name) { + override val description = "Evaluating ORT result rule '$name'." + + override fun issueSource() = "$name - ORT result" + + fun error(message: String, howToFix: String): Unit = + error( + pkgId = null, + license = null, + licenseSource = null, + message = message, + howToFix = howToFix + ) +} diff --git a/evaluator/src/main/kotlin/RuleSet.kt b/evaluator/src/main/kotlin/RuleSet.kt index 039f075a1ae37..e805a25e5efe7 100644 --- a/evaluator/src/main/kotlin/RuleSet.kt +++ b/evaluator/src/main/kotlin/RuleSet.kt @@ -40,6 +40,16 @@ class RuleSet( */ val violations = mutableSetOf() + /** + * A DSL function to configure an [OrtResultRule]. The rule is applied once to [ortResult]. + */ + fun ortResultRule(name: String, configure: OrtResultRule.() -> Unit) { + OrtResultRule(this, name, ortResult).apply { + configure() + evaluate() + } + } + /** * A DSL function to configure a [PackageRule]. The rule is applied to each [Package] and [Project] contained in * [ortResult].