Skip to content

Instantly share code, notes, and snippets.

@jprinet
Created July 7, 2022 11:18
Show Gist options
  • Save jprinet/059d30ba2d98c44f699e185c0e73eb9d to your computer and use it in GitHub Desktop.
Save jprinet/059d30ba2d98c44f699e185c0e73eb9d to your computer and use it in GitHub Desktop.
Grab checkstyle issues in a configuration cache compatible way
abstract class CheckstyleReportAsCustomValueBuildService implements BuildService<CheckstyleReportAsCustomValueBuildService.Params>, OperationCompletionListener {
interface Params extends BuildServiceParameters {
ListProperty<FileSystemLocation> getReports()
Property<BuildScanExtension> getBuildScanApi()
}
@Override
void onFinish(FinishEvent finishEvent) {
if(finishEvent.getDescriptor().displayName.contains("checkstyle")){
println getParameters().getReports().get().forEach({
if(it.asFile.exists()) {
def checkstyle = new XmlSlurper().parse(it.asFile)
def errors = checkstyle.file.collect {
String filePath = it.@name.text()
it.error.collect { "${filePath}:${it.@line}:${it.@column} \u2192 ${it.@message}" }
}.flatten()
println errors
errors.each { getParameters().getBuildScanApi().get().value('Checkstyle Issue', it) }
}
})
}
}
}
BuildScanExtension buildScanApi = project.extensions.findByName('buildScan')
SetProperty<FileSystemLocation> reportPaths = project.getObjects().setProperty(FileSystemLocation.class)
tasks.withType(Checkstyle) {
reports {
xml.required = true
html.required = false
}
reportPaths.add(reports.xml.outputLocation.locationOnly)
}
def registry = project.services.get(BuildEventsListenerRegistry)
registry.onTaskCompletion(gradle.sharedServices.registerIfAbsent('checkstyleReportAsCustomValueBuildService', CheckstyleReportAsCustomValueBuildService.class, spec -> {
spec.parameters.reports.set(reportPaths)
spec.parameters.buildScanApi.set(buildScanApi)
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment