Skip to content

Add buildCause() #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
51 changes: 51 additions & 0 deletions vars/buildCause.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// vars/buildCause.groovy

/**
* Return the reason for the build
*/
Boolean call(Map config = [:]) {

String buildUser = "Unknown"
if (currentBuild.rawBuild.getCause(hudson.model.Cause$UserIdCause)) {
println "CAUSE ${currentBuild.rawBuild.getCause(hudson.model.Cause$UserIdCause).properties}"
} else {
println "currentBuild.rawBuild.getCause(hudson.model.Cause\$UserIdCause) is null"
println "so, " + currentBuild.rawBuild.getCause(hudson.triggers.TimerTrigger$TimerTriggerCause)
println "and " + currentBuild.getBuildCauses('hudson.triggers.TimerTrigger$TimerTriggerCause').isEmpty()
}
/* [userName:Brian J. Murrell,
userIdOrUnknown:bmurrell,
userId:bmurrell,
class:class hudson.model.Cause$UserIdCause,
userUrl:user/bmurrell,
shortDescription:Started by user Brian J. Murrell]
*/
def causes = currentBuild.getBuildCauses()

// Get a specific Cause type (in this case the user who kicked off the build),
// if present.
def specificCause = currentBuild.getBuildCauses('hudson.model.Cause$UserIdCause')
println "specificCause: " + specificCause

println currentBuild.getBuildCauses().shortDescription[0]


/*
def buildCauses = currentBuild.rawBuild.getCauses()
//echo buildCauses
echo buildCauses.getShortDescription()
echo buildCauses.getUserId()
echo buildCauses.getUserName()
echo buildCauses.getUserUrl()
echo buildCauses.hashCode()
if (buildCauses.contains("hudson.triggers.TimerTrigger")){
buildUser = "TimerTrigger"
}*/ /* else {
wrap([$class: 'BuildUser']) {
buildUser = "${BUILD_USER}"
}
}*/
echo "Initiated by: ${buildUser}"

return !currentBuild.getBuildCauses('hudson.triggers.TimerTrigger$TimerTriggerCause').isEmpty()
}
13 changes: 13 additions & 0 deletions vars/startedByTimer.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// vars/startedByTimer.groovy

/**
* Return the reason for the build
*
* This needs to be here rather than in pipeline-lib or Jenkinsfile due to
* Scripts not permitted to use method net.sf.json.JSON isEmpty.
*/
Boolean call(Map config = [:]) {

return !currentBuild.getBuildCauses('hudson.triggers.TimerTrigger$TimerTriggerCause').isEmpty()

}