Skip to content

Commit

Permalink
gradle-plugin: Set user.home property if SARIF reporter is active (#…
Browse files Browse the repository at this point in the history
…1426)

### What's done:
* Updated logic
* Improved logging
* Added tests

Closes #1269
  • Loading branch information
petertrr authored Jul 7, 2022
1 parent 00e0abf commit 0cd6d17
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,22 +170,32 @@ open class DiktatJavaExecTaskBase @Inject constructor(
return flag.toString()
}

@Suppress("SAY_NO_TO_VAR")
private fun setReporter(diktatExtension: DiktatExtension, flag: java.lang.StringBuilder) {
val name = diktatExtension.reporter.trim()
val validReporters = listOf("sarif", "plain", "json", "html")
var reporterFlag = if (name.isEmpty() || !validReporters.contains(name)) {
project.logger.warn("Reporter name $name was not specified or is invalid. Falling to 'plain' reporter")
"--reporter=plain"
} else {
"--reporter=$name"
val reporterFlag = when {
diktatExtension.githubActions -> {
if (diktatExtension.reporter.isNotEmpty()) {
// githubActions should have higher priority than custom input
project.logger.warn("`diktat.githubActions` is set to true, so custom reporter [$name] will be ignored and SARIF reporter will be used")
}
"--reporter=sarif"
}
name.isEmpty() -> {
project.logger.info("Reporter name was not set. Using 'plain' reporter")
"--reporter=plain"
}
name !in validReporters -> {
project.logger.warn("Reporter name is invalid (provided value: [$name]). Falling back to 'plain' reporter")
"--reporter=plain"
}
else -> "--reporter=$name"
}

// githubActions should have higher priority than a custom input
if (diktatExtension.githubActions) {
val isSarifReporterActive = reporterFlag.contains("sarif")
if (isSarifReporterActive) {
// need to set user.home specially for ktlint, so it will be able to put a relative path URI in SARIF
systemProperty("user.home", project.rootDir.toString())
reporterFlag = "--reporter=sarif"
}

flag.append(reporterFlag)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ class DiktatJavaExecTaskTest {
diktatConfigFile = project.file("../diktat-analysis.yml")
githubActions = true
}
val task = project.tasks.getByName(DIKTAT_CHECK_TASK) as DiktatJavaExecTaskBase
Assertions.assertEquals(
project.rootDir.toString(),
task.systemProperties["user.home"]
)
}

@Test
Expand All @@ -137,6 +142,22 @@ class DiktatJavaExecTaskTest {
}
}

@Test
fun `should set system property with SARIF reporter`() {
assertCommandLineEquals(
listOf(null, "--reporter=sarif")
) {
inputs { exclude("*") }
diktatConfigFile = project.file("../diktat-analysis.yml")
reporter = "sarif"
}
val task = project.tasks.getByName(DIKTAT_CHECK_TASK) as DiktatJavaExecTaskBase
Assertions.assertEquals(
project.rootDir.toString(),
task.systemProperties["user.home"]
)
}

@Test
fun `check system property with multiproject build with default config`() {
setupMultiProject()
Expand Down

0 comments on commit 0cd6d17

Please sign in to comment.