简体   繁体   English

使用 groovy 从 GIT 提交消息中提取 Jira 票证

[英]Extracting Jira tickets from GIT commit message using groovy

I'm writing Jenkins pipeline in which I'm extracting Jira tickets from GIT commit message.I'm using JIRA ID regex.我正在编写 Jenkins 管道,其中我从 GIT 提交消息中提取 Jira 票证。我正在使用 JIRA ID 正则表达式。 How can I process a multiline string?如何处理多行字符串? I also have to display commit messages which do not contain any valid ticket ID's.我还必须显示不包含任何有效票证 ID 的提交消息。 How can I do that using the if-else loop in groovy?我如何使用 groovy 中的 if-else 循环来做到这一点? Below logic works for a single line, but not working for multi-line.下面的逻辑适用于单行,但不适用于多行。

def commit = """new change
CO-10389
SRE-1234"""

def regex = (/[\s|]?([A-Z]+-[0-9]+)[\s:|]?/) 

if(commit =~ regex){
    def jira = commit.readLines().findAll(/[\s|]?([A-Z]+-[0-9]+)[\s:|]?/)
    println jira
} else {
    println commit
}

Multiline regex won't help you if you want to process also mis-matching lines, so you should be processing your text line-by-line:如果您还想处理不匹配的行,多行正则表达式将无济于事,因此您应该逐行处理文本:

import java.util.regex.Matcher

def commit = """new change
CO-10389
CO-
SRE-1234"""

commit.eachLine{ l ->
  switch( l ){
    case ~/[\s|]?([A-Z]+-[0-9]+)[\s:|]?/:
      println "JIRA: ${Matcher.lastMatcher[0][1]}"
      break
    default:
      println "no JIRA $l"
  }
}

prints印刷

no JIRA new change
JIRA: CO-10389
no JIRA CO-
JIRA: SRE-1234

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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