Skip to content

Instantly share code, notes, and snippets.

@Cliabhach
Last active February 4, 2020 02:51
Show Gist options
  • Select an option

  • Save Cliabhach/7ac63973d083a3bde8438c16ece6b674 to your computer and use it in GitHub Desktop.

Select an option

Save Cliabhach/7ac63973d083a3bde8438c16ece6b674 to your computer and use it in GitHub Desktop.
Parser for Lizard Code Quality Analysis, compatible with the Jenkins 'Warnings Next Generation' plugin
// Based on the excellent Jenkins guide at https://github.com/jenkinsci/warnings-ng-plugin/blob/538747e1a14974be5/doc/Documentation.md#creating-a-new-tool-using-a-groovy-parser
// This file may be used in accordance with the MIT-style license found at https://github.com/jenkinsci/warnings-ng-plugin/blob/master/LICENSE
import edu.hm.hafner.analysis.Severity
// CSV format is as follows (c.f. https://github.com/terryyin/lizard/blob/debd579ef16ae3c649/test/testOutputCSV.py#L25 )
// "NLOC,CCN,token,PARAM,length,location,file,function,long_name,start,end"
//
// Regex should look something like
//
// (\d*),(\d*),(\d*),(\d*),(\d*),\"(.*)\",\"(.*)\",\"(.*)\",\"(.*)\",(\d*),(\d*)
//
Integer linesOfCode = Integer.parseInt(matcher.group(1))
Integer complexity = Integer.parseInt(matcher.group(2))
final String messageFormat
if (linesOfCode > 100) {
messageFormat = "This very long function is %s"
} else if (linesOfCode > 50) {
messageFormat = "This kinda long function is %s"
} else if (linesOfCode > 20) {
messageFormat = "This reasonably-sized function is %s"
} else {
messageFormat = "This short function is %s"
}
if (complexity >= 10) {
final Severity severity
final String ccnWarning
if (complexity < 15) {
severity = Severity.WARNING_LOW
ccnWarning = "a little complex."
} else if (complexity < 30) {
severity = Severity.WARNING_NORMAL
ccnWarning = "pretty complex."
} else if (complexity < 60) {
severity = Severity.WARNING_HIGH
ccnWarning = "very complex."
} else {
severity = Severity.ERROR
ccnWarning = "excessively complex."
}
builder.setFileName(matcher.group(7))
.setLineStart(matcher.group(10))
.setLineEnd(matcher.group(11))
.setSeverity(severity)
.setCategory('Code Complexity')
.setDescription(matcher.group(6))
.setType('CCN')
.setMessage(String.format(messageFormat, ccnWarning))
return builder.buildOptional()
} else {
return Optional.empty()
}
@Cliabhach
Copy link
Author

Note that this has hard-coded thresholds, which are usually a bad idea. It also doesn't sanitize any inputs....you'll want to build this into a proper plugin before using it on a production environment.

Even so, I think I did alright on my first attempt at understanding the APIs.

@Cliabhach
Copy link
Author

I should also mention that the lizard tool has a pretty good python API. I haven't tried it out yet, but that would be the next thing to build against.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment