Last active
April 28, 2024 12:58
-
-
Save iurysza/005c08ff7f1892097c2823b1786ac087 to your computer and use it in GitHub Desktop.
Gradle task that runs detekt only over changed files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
configurations { detekt } | |
dependencies { detekt "io.gitlab.arturbosch.detekt:detekt-cli:$detektVersion" } | |
task detektCi(type: JavaExec, group: "verification") { | |
description = "Run Kotlin static analysis on changed files." | |
group = "CI" | |
main = "io.gitlab.arturbosch.detekt.cli.Main" | |
classpath = configurations.detekt | |
doFirst { | |
def changedFilesList = getDiffedFilesFromBranch("main") | |
if (changedFilesList.isEmpty()) { | |
println("No kotlin files changed! Skipping task...") | |
// forces detekt to ignore all files | |
changedFilesList = "$rootDir/gradle" | |
} else { | |
println("Running detekt on the changed files:") | |
println(changedFilesList) | |
} | |
def reportDir = "${buildDir}/reports/detekt" | |
def params = [ | |
"--input", "$changedFilesList", | |
"--excludes", "!**/src/**/*Test.kt, **/spotless.kt", | |
"--config", "$rootDir/ci-cd/detekt-config.yml", | |
"--report", "xml:$reportDir/detekt-checkstyle.xml", | |
"--report", "html:$reportDir/report.html" | |
] | |
args(params) | |
} | |
} | |
private static String getDiffedFilesFromBranch(String branch) { | |
def outputStream = new ByteArrayOutputStream() | |
def cmd = "git diff --diff-filter=d --name-only origin/$branch --relative | grep '\\.kt\\?\$'" | |
execute(cmd, outputStream) | |
if (outputStream.toString().isEmpty()) return "" | |
// get comma separated string of files | |
return outputStream.toString() | |
.trim() | |
.replace("\n", ",") | |
} | |
private static def execute(cmd, output) { | |
['sh', '-c', cmd].execute().waitForProcessOutput(output, System.err) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for this nice little task! :) Just wanted to mention that some CIs only shallow clone and therefore
git diff
doesn't work. The result looks like:What helped was to fetch the needed branch before:
Maybe you like to add it if others stumble upon this problem too.