diff --git a/vars/commitPragmaTrusted.groovy b/vars/commitPragmaTrusted.groovy index 8abfc9e..09d4638 100755 --- a/vars/commitPragmaTrusted.groovy +++ b/vars/commitPragmaTrusted.groovy @@ -9,21 +9,21 @@ * config['pragma'] Pragma to get the value of * config['def_val'] Value to return if not found */ + +import java.util.regex.Pattern + def call(Map config = [:]) { def def_value = '' if (config['def_val']) { def_value = config['def_val'] } - def value = sh(script: '''b=$(git show -s --format=%B | - sed -ne 's/^''' + config['pragma'] + - ''': *\\(.*\\)/\\1/p') - if [ -n "$b" ]; then - echo "$b" - else - echo "''' + def_value + '''" - fi''', - returnStdout: true) - return value.trim() + msg = getFinalCommitComment() + try { + def (_,val) = (msg =~ /(?mi)^${config['pragma']}:\s*(.+)$/)[0] + return val + } catch (java.lang.IndexOutOfBoundsException e) { + return def_value + } -} \ No newline at end of file +} diff --git a/vars/getFinalCommitComment.groovy b/vars/getFinalCommitComment.groovy new file mode 100644 index 0000000..fdb9e15 --- /dev/null +++ b/vars/getFinalCommitComment.groovy @@ -0,0 +1,36 @@ +// vars/getFinalCommitComment.groovy + +/** + * Return the Comment of the last git commit + * + * This needs to be here rather than in pipeline-lib or Jenkinsfile due to + * Scripts not permitted to use method hudson.plugins.git.GitChangeSet getComment + */ +String call(Map config = [:]) { + + if (true || config['debug']){ + println("checking currentBuild.changeSets") + + String comment = "Not found" + if (currentBuild.changeSets != null) { + println("There are " + currentBuild.changeSets.size() + " changeSets") + for (changeSetList in currentBuild.changeSets) { + println("----- changeSetList -----") + println("There are " + changeSetList.size() + " changeSetLists") + for (changeSet in changeSetList) { + println("----- changeSet -----") + println(changeSet.comment) + comment = changeSet.comment + } + } + } else { + println("currentBuild.changeSets was null") + } + } + + def changeSetList = currentBuild.changeSets[currentBuild.changeSets.size() - 1] + def changeSet = changeSetList[changeSetList.size() - 1] + + return changeSet.comment + +}