简体   繁体   中英

How to extract git message as a variable and. regex match in a groovy switch block. (via jenkins library)

I have a var in a groovy library that is trying to match a commit message and set a variable to return however it is not matching properly.

Note that I have tried this in a bare script where I provide message as a hard coded String.

#!/usr/bin/env groovy

def call(String commitId) {
  message = sh(
      script: "git show -s  --format=%s ${commitId}",
      returnStdout: true
      )
    println "Based on the commit message: \n ${message}"
    switch("${message}") {
      case ~/.*Feature.*/:
        verType = 'minor'
        println "The Next version will be a ${verType} type"
        break
      case ~/^.*(Hot|Bug)fix.*$/:
        verType = 'patch'
        println "The Next version will be a ${verType} type"
        break
      case ~/^.*Release.*$/:
        verType = 'major'
        println "The Next version will be a ${verType} type"
        break
      default:
        verType = 'patch'
        println 'No matches using Patch'
        break
    }
  return verType
}

I have tried commit ID's of literal, "Feature" and "Release" and it always returns the default value. I get an output in the Jenkins console like this:

[Pipeline] sh
+ git show -s --format=%s 1c532309b04909dc4164ecf9b3276104bf5c2bc0
[Pipeline] echo
Based on the commit message: 
 Feature

[Pipeline] echo
No matches using Patch

When I run the bellow similar script on the command line it returns "Release" which shows me that its in how message is passed to the statement.

#!/usr/bin/env groovy

  message = 'Release, creating major release'
    switch(message) {
      case ~/.*Feature.*/:
        verType = 'minor'
        break
      case ~/^.*(Hot|Bug)fix.*$/:
        verType = 'patch'
        break
      case ~/^Release.*$/:
        verType = 'major'
        break
      default:
        verType = 'patch'
        break
    }
    println verType

that's because message contains multiline string

try message = '\nRelease, creating major release' on your second script to reproduce an issue

to fix it you could modify your regex like this:

/^\s*Release.*$/

Problems was as @dagget said in my message var. however I did not see his post by the time I found the answer. so I thought I would post the working version as the answer. I used theString.trim() function to remove the leading line break.

So here is my working code.

#!/usr/bin/env groovy

def call(String commitId) {
  String message = sh(
      script: "git show -s  --format=%s ${commitId}",
      returnStdout: true
      )
    println "Based on the commit message: \n ${message.trim()}"
    switch("${message.trim()}") {
      case ~/.*Feature.*/:
        verType = 'minor'
        println "The Next version will be a ${verType} type"
        break
      case ~/^.*(Hot|Bug)fix.*$/:
        verType = 'patch'
        println "The Next version will be a ${verType} type"
        break
      case ~/^.*Release.*$/:
        verType = 'major'
        println "The Next version will be a ${verType} type"
        break
      default:
        verType = 'patch'
        println 'No matches using Patch'
        break
    }
  return verType
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM