Created
July 7, 2022 11:18
-
-
Save jprinet/059d30ba2d98c44f699e185c0e73eb9d to your computer and use it in GitHub Desktop.
Grab checkstyle issues in a configuration cache compatible way
This file contains 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
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 = [email protected]() | |
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