From 43225628786526462cd12832e67179a9a6de7037 Mon Sep 17 00:00:00 2001 From: "Brian J. Murrell" Date: Mon, 14 Dec 2020 17:25:02 -0500 Subject: [PATCH 1/2] Add getCommitComment Since we have it, leverage it for a pure groovy implementation of commitPragmaTrusted. Signed-off-by: Brian J. Murrell --- vars/commitPragmaTrusted.groovy | 22 +++++++++---------- vars/getFinalCommitComment.groovy | 36 +++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 vars/getFinalCommitComment.groovy 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..49ada66 --- /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 (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 + +} From 2ae17e3dae435b680ea10ffe5f1521058addca4d Mon Sep 17 00:00:00 2001 From: "Brian J. Murrell" Date: Tue, 15 Dec 2020 00:19:03 -0500 Subject: [PATCH 2/2] Force debug To debug the Build with Parameters case. Signed-off-by: Brian J. Murrell --- vars/getFinalCommitComment.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vars/getFinalCommitComment.groovy b/vars/getFinalCommitComment.groovy index 49ada66..fdb9e15 100644 --- a/vars/getFinalCommitComment.groovy +++ b/vars/getFinalCommitComment.groovy @@ -8,7 +8,7 @@ */ String call(Map config = [:]) { - if (config['debug']){ + if (true || config['debug']){ println("checking currentBuild.changeSets") String comment = "Not found"