Created
August 26, 2013 07:59
-
-
Save languitar/6339022 to your computer and use it in GitHub Desktop.
A trigger script for the jenkins (hudson) email_ext plugin which informs users in case their commits introduce new violations or warnings. In contrast to the limit options of various other plugins for code analysis reporting this does not fail the build.A simple but insecure way to share this script between different jobs is to execute the follo…
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
import jenkins.model.Jenkins | |
build = Jenkins.instance.items[0].builds[0] | |
println build | |
println("Available actions on the build: " + build.actions) | |
def analysisCoreActions = [] | |
// All static analysis actions based on static analysis core can be handled uniformly | |
analysisCoreActions.addAll(build.actions.findAll{it.class.name =~ "hudson.plugins.checkstyle.CheckStyle.*ResultAction"}) | |
analysisCoreActions.addAll(build.actions.findAll{it.class.name =~ "hudson.plugins.findbugs.FindBugs.*ResultAction"}) | |
analysisCoreActions.addAll(build.actions.findAll{it.class.name =~ "hudson.plugins.pmd.Pmd.*ResultAction"}) | |
analysisCoreActions.addAll(build.actions.findAll{it.class.name =~ "hudson.plugins.dry.Dry.*ResultAction"}) | |
analysisCoreActions.addAll(build.actions.findAll{it.class.name =~ "hudson.plugins.warnings.*ResultAction"}) | |
analysisCoreActions.removeAll([null]) | |
println "Found analysis core actions: " + analysisCoreActions | |
def newViolations = analysisCoreActions.sum{it.result.numberOfNewWarnings} | |
println "Found " + newViolations + " new violations in analysis core plugins" | |
// Specifically handle new tasks as we probably want to send something different in this case | |
def tasksAction = build.actions.find{it.class.name =~ "hudson.plugins.tasks.*ResultAction"} | |
def newTasks = 0 | |
if (tasksAction != null) { | |
newTasks = tasksAction.result.numberOfNewWarnings | |
println "Found " + newTasks + " new tasks" | |
} | |
// now there are some special rules for plugins that do not use analysis core | |
// violations plugin | |
def violationsStatus(build) { | |
def violationsAction = build.actions.find{it.class.name =~ "hudson.plugins.violations.ViolationsBuildAction"} | |
if (violationsAction == null) { | |
return false | |
} | |
def previousBuild = build.previousBuild | |
if (previousBuild == null) { | |
return false | |
} | |
def previousViolationsAction = previousBuild.actions.find{it.class.name =~ "hudson.plugins.violations.ViolationsBuildAction"} | |
if (previousViolationsAction == null) { | |
return false | |
} | |
for (currentViolationState in violationsAction.report.violations) { | |
println currentViolationState.key + ": " + currentViolationState.value | |
def previousState = previousViolationsAction.report.violations[currentViolationState.key] | |
if (previousState == null) { | |
continue | |
} | |
println "Previous: " + previousState | |
if (currentViolationState.value > previousState && previousState >= 0) { | |
return true | |
} | |
} | |
return false | |
} | |
def violationsAdded = violationsStatus(build) | |
println "Added violations: " + violationsAdded | |
// cppcheck | |
def cppcheckStatus(build) { | |
def cppcheckAction = build.actions.find{it.class.name =~ "org.jenkinsci.plugins.cppcheck.CppcheckBuildAction"} | |
if (cppcheckAction == null) { | |
return false | |
} | |
def previousBuild = build.previousBuild | |
println "Previous cppcheck build: " + previousBuild | |
if (previousBuild == null) { | |
return false | |
} | |
def previousCppcheckAction = previousBuild.actions.find{it.class.name =~ "org.jenkinsci.plugins.cppcheck.CppcheckBuildAction"} | |
println "Previous cppcheck action: " + previousCppcheckAction | |
if (previousCppcheckAction == null) { | |
return false | |
} | |
return cppcheckAction.result.report.allErrors.size() > previousCppcheckAction.result.report.allErrors.size() | |
} | |
def cppcheckAdded = cppcheckStatus(build) | |
println "Added cppcheck errors: " + cppcheckAdded | |
// send email if we have new warnings | |
newViolations > 0 || newTasks > 0 || violationsAdded || cppcheckAdded |
Do you know if there is any way to source a groovy script in that section of Jenkins (by giving it the file path) instead of copy and pasting the script in there?
Hi, I cant get the script to run in Jenkins correctly.
created the Editable Email Notification --> Advance Setting paste the above code into Pre-Send Script and save. ran Jenkins with warning and not calling the script is there something i doing wrong
Helpful code!
Using it in Post-build actions .. Editable Email Notification .. Advanced Settings .. Add Trigger .. Script .. Advanced .. Trigger Script.
One minor improvement to it is use
build = this.binding.build
instead of
build = Jenkins.instance.items[0].builds[0]
Works even if we create new jobs copying current settings.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@res1st, sorry, never saw your post. I have updated the description accordingly. There is a dedicated trigger to add under the advanced section. You need to use that.