Skip to content
Permalink
Browse files

RuleViolation: Add the field `howToFix`

The field can be used to provide detailed information on how the rule
violation can be fixed. Support for showing the field in the reports
will be implemented in separate commits.

Signed-off-by: Martin Nonnenmacher <martin.nonnenmacher@here.com>
  • Loading branch information...
mnonnenmacher committed Jun 25, 2019
1 parent edfae98 commit e559b3eaa4a4351deb3afccac0a7979607c2b877
@@ -133,22 +133,23 @@ open class PackageRule(
}
}

fun issue(severity: Severity, message: String) = issue(severity, pkg.id, null, null, message)
fun issue(severity: Severity, message: String, howToFix: String) =
issue(severity, pkg.id, null, null, message, howToFix)

/**
* Add a [hint][Severity.HINT] to the list of [issues].
*/
fun hint(message: String) = hint(pkg.id, null, null, message)
fun hint(message: String, howToFix: String) = hint(pkg.id, null, null, message, howToFix)

/**
* Add a [warning][Severity.WARNING] to the list of [issues].
*/
fun warning(message: String) = warning(pkg.id, null, null, message)
fun warning(message: String, howToFix: String) = warning(pkg.id, null, null, message, howToFix)

/**
* Add an [error][Severity.ERROR] to the list of [issues].
*/
fun error(message: String) = error(pkg.id, null, null, message)
fun error(message: String, howToFix: String) = error(pkg.id, null, null, message, howToFix)

/**
* A [Rule] to check a single license of the [package][pkg].
@@ -211,21 +212,22 @@ open class PackageRule(
override fun matches() = SpdxLicense.forId(license) != null
}

fun issue(severity: Severity, message: String) = issue(severity, pkg.id, license, licenseSource, message)
fun issue(severity: Severity, message: String, howToFix: String) =
issue(severity, pkg.id, license, licenseSource, message, howToFix)

/**
* Add a [hint][Severity.HINT] to the list of [issues].
*/
fun hint(message: String) = hint(pkg.id, license, licenseSource, message)
fun hint(message: String, howToFix: String) = hint(pkg.id, license, licenseSource, message, howToFix)

/**
* Add a [warning][Severity.WARNING] to the list of [issues].
*/
fun warning(message: String) = warning(pkg.id, license, licenseSource, message)
fun warning(message: String, howToFix: String) = warning(pkg.id, license, licenseSource, message, howToFix)

/**
* Add an [error][Severity.ERROR] to the list of [issues].
*/
fun error(message: String) = error(pkg.id, license, licenseSource, message)
fun error(message: String, howToFix: String) = error(pkg.id, license, licenseSource, message, howToFix)
}
}
@@ -107,34 +107,42 @@ abstract class Rule(
*/
abstract fun issueSource(): String

fun issue(severity: Severity, pkgId: Identifier, license: String?, licenseSource: LicenseSource?, message: String) {
fun issue(
severity: Severity,
pkgId: Identifier,
license: String?,
licenseSource: LicenseSource?,
message: String,
howToFix: String
) {
violations += RuleViolation(
severity = severity,
rule = name,
pkg = pkgId,
license = license,
licenseSource = licenseSource,
message = message
message = message,
howToFix = howToFix
)
}

/**
* Add a [hint][Severity.HINT] to the list of [issues].
*/
fun hint(pkgId: Identifier, license: String?, licenseSource: LicenseSource?, message: String) =
issue(Severity.HINT, pkgId, license, licenseSource, message)
fun hint(pkgId: Identifier, license: String?, licenseSource: LicenseSource?, message: String, howToFix: String) =
issue(Severity.HINT, pkgId, license, licenseSource, message, howToFix)

/**
* Add a [warning][Severity.WARNING] to the list of [issues].
*/
fun warning(pkgId: Identifier, license: String?, licenseSource: LicenseSource?, message: String) =
issue(Severity.WARNING, pkgId, license, licenseSource, message)
fun warning(pkgId: Identifier, license: String?, licenseSource: LicenseSource?, message: String, howToFix: String) =
issue(Severity.WARNING, pkgId, license, licenseSource, message, howToFix)

/**
* Add an [error][Severity.ERROR] to the list of [issues].
*/
fun error(pkgId: Identifier, license: String?, licenseSource: LicenseSource?, message: String) =
issue(Severity.ERROR, pkgId, license, licenseSource, message)
fun error(pkgId: Identifier, license: String?, licenseSource: LicenseSource?, message: String, howToFix: String) =
issue(Severity.ERROR, pkgId, license, licenseSource, message, howToFix)

/**
* A DSL helper class, providing convenience functions for adding [RuleMatcher]s to this rule.
@@ -68,7 +68,8 @@ class EvaluatorTest : WordSpec() {
license = "license 1",
licenseSource = LicenseSource.DETECTED,
severity = Severity.ERROR,
message = "message 1"
message = "message 1",
howToFix = "how to fix 1"
)
ruleViolations += RuleViolation(
@@ -77,7 +78,8 @@ class EvaluatorTest : WordSpec() {
license = "license 2",
licenseSource = LicenseSource.DECLARED,
severity = Severity.WARNING,
message = "message 2"
message = "message 2",
howToFix = "how to fix 2"
)
""".trimIndent()
)
@@ -91,6 +93,7 @@ class EvaluatorTest : WordSpec() {
licenseSource shouldBe LicenseSource.DETECTED
severity shouldBe Severity.ERROR
message shouldBe "message 1"
howToFix shouldBe "how to fix 1"
}

with(result.violations[1]) {
@@ -100,6 +103,7 @@ class EvaluatorTest : WordSpec() {
licenseSource shouldBe LicenseSource.DECLARED
severity shouldBe Severity.WARNING
message shouldBe "message 2"
howToFix shouldBe "how to fix 2"
}
}
}
@@ -25,13 +25,14 @@ import io.kotlintest.specs.WordSpec

class RuleSetTest : WordSpec() {
private val errorMessage = "error message"
private val howToFix = "how to fix"

init {
"package rule" should {
"add errors if it has no requirements" {
val ruleSet = ruleSet(ortResult) {
packageRule("test") {
error(errorMessage)
error(errorMessage, howToFix)
}
}

@@ -45,7 +46,7 @@ class RuleSetTest : WordSpec() {
+isExcluded()
}

error(errorMessage)
error(errorMessage, howToFix)
}
}

@@ -65,7 +66,7 @@ class RuleSetTest : WordSpec() {
+isSpdxLicense()
}

error(errorMessage)
error(errorMessage, howToFix)
}
}
}
@@ -78,7 +79,7 @@ class RuleSetTest : WordSpec() {
"add errors if it has no requirements" {
val ruleSet = ruleSet(ortResult) {
dependencyRule("test") {
error(errorMessage)
error(errorMessage, howToFix)
}
}

@@ -92,7 +93,7 @@ class RuleSetTest : WordSpec() {
+isStaticallyLinked()
}

error(errorMessage)
error(errorMessage, howToFix)
}
}

@@ -111,7 +112,7 @@ class RuleSetTest : WordSpec() {
+isSpdxLicense()
}

error(errorMessage)
error(errorMessage, howToFix)
}

}
@@ -34,6 +34,7 @@ class RuleTest : WordSpec() {
private val license = "license"
private val licenseSource = LicenseSource.DECLARED
private val message = "violation message"
private val howToFix = "how to fix"

private fun createRule() = object : Rule(ruleSet, "test") {
override val description = "test"
@@ -45,7 +46,7 @@ class RuleTest : WordSpec() {
"add an issue with the correct severity" {
val rule = createRule()

rule.hint(id, license, licenseSource, message)
rule.hint(id, license, licenseSource, message, howToFix)

rule.violations should haveSize(1)
rule.violations.first().let { violation ->
@@ -55,6 +56,7 @@ class RuleTest : WordSpec() {
violation.licenseSource shouldBe licenseSource
violation.severity shouldBe Severity.HINT
violation.message shouldBe message
violation.howToFix shouldBe howToFix
}
}
}
@@ -63,7 +65,7 @@ class RuleTest : WordSpec() {
"add an issue with the correct severity" {
val rule = createRule()

rule.warning(id, license, licenseSource, message)
rule.warning(id, license, licenseSource, message, howToFix)

rule.violations should haveSize(1)
rule.violations.first().let { violation ->
@@ -73,6 +75,7 @@ class RuleTest : WordSpec() {
violation.licenseSource shouldBe licenseSource
violation.severity shouldBe Severity.WARNING
violation.message shouldBe message
violation.howToFix shouldBe howToFix
}
}
}
@@ -81,7 +84,7 @@ class RuleTest : WordSpec() {
"add an issue with the correct severity" {
val rule = createRule()

rule.error(id, license, licenseSource, message)
rule.error(id, license, licenseSource, message, howToFix)

rule.violations should haveSize(1)
rule.violations.first().let { violation ->
@@ -91,6 +94,7 @@ class RuleTest : WordSpec() {
violation.licenseSource shouldBe licenseSource
violation.severity shouldBe Severity.ERROR
violation.message shouldBe message
violation.howToFix shouldBe howToFix
}
}
}
@@ -48,5 +48,11 @@ data class RuleViolation(
/**
* A message explaining the rule violation.
*/
val message: String
val message: String,

/**
* A text explaining how the rule violation can be fixed. Reporters rendering this string should support the
* [Markdown syntax](https://daringfireball.net/projects/markdown/syntax) to allow formatting of the text.
*/
val howToFix: String
)
@@ -25,6 +25,7 @@ class RuleViolation {
this.licenseSource = '';
this.severity = '';
this.message = '';
this.howToFix = '';

if (obj instanceof Object) {
if (obj.rule) {
@@ -54,6 +55,10 @@ class RuleViolation {
if (obj.message) {
this.message = obj.message;
}

if (obj.howToFix) {
this.howToFix = obj.howToFix;
}
}
}
}
@@ -349,15 +349,18 @@ evaluator:
license_source: "DETECTED"
severity: "ERROR"
message: "EPL-1.0 error"
how_to_fix: "how to fix"
- rule: "rule 2"
pkg: "Maven:org.apache.commons:commons-text:1.1"
license: "Apache-2.0"
license_source: "DECLARED"
severity: "HINT"
message: "Apache-2.0 hint"
how_to_fix: "how to fix"
- rule: "rule 3"
pkg: "Maven:org.hamcrest:hamcrest-core:1.3"
license: "BSD-3-Clause"
license_source: "CONCLUDED"
severity: "WARNING"
message: "BSD-3-Clause warning"
how_to_fix: "how to fix"

0 comments on commit e559b3e

Please sign in to comment.
You can’t perform that action at this time.