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

gradle-plugin: Set user.home property if SARIF reporter is active #1426

Merged
merged 2 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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