1

I'm trying to check if commit-msg from git contains particular ticket number with project key of Jira using groovy in Jenkins pipeline

def string_array = ['CO', 'DEVOPSDESK', 'SEC', 'SRE', 'SRE00IN', 'SRE00EU', 'SRE00US', 'REL']
def string_msg = 'CO-10389, CO-10302 new commit'

To extract numbers I am using below logic.

findAll( /\d+/ )*.toInteger()

Not sure how to extract exact ticket number with project key. Thanks in advance.

2 Answers 2

1

You could use Groovy's find operator - =~, combined with a findAll() method to extract all matching elements. For that, you could create a pattern that matches CO-\d+ OR DEOPSDESK-\d+ OR ..., and so on. You could keep project IDs in a list and then dynamically create a regex pattern.

Consider the following example:

def projectKeys = ['CO', 'DEVOPSDESK', 'SEC', 'SRE', 'SRE00IN', 'SRE00EU', 'SRE00US', 'REL']
def commitMessage = 'CO-10389, CO-10302 new commit'

// Generate a pattern "CO-\d+|DEVOPSDEKS-\d+|SEC-\d+|...
def pattern = projectKeys.collect { /${it}-\d+/ }.join("|")

// Uses =~ (find) operator and extracts matching elements
def jiraIds = (commitMessage =~ pattern).findAll()

assert jiraIds == ["CO-10389","CO-10302"]

// Another example
assert ("SEC-1,REL-2001 some text here" =~ pattern).findAll() == ["SEC-1","REL-2001"]
Sign up to request clarification or add additional context in comments.

Comments

0

The regex can be assembled a bit simpler:

def projectKeys = ['CO', 'DEVOPSDESK', 'SEC', 'SRE', 'SRE00IN', 'SRE00EU', 'SRE00US', 'REL'] 
def commitMessage = 'CO-10389, REL-10302 new commit'

String regex = /(${projectKeys.join('|')})-\d+/

assert ['CO-10389', 'REL-10302'] == (commitMessage =~ regex).findAll()*.first()

You can have also another option with finer contol over matching:

def res = []
commitMessage.eachMatch( regex ){ res << it[ 0 ] }
assert ['CO-10389', 'REL-10302'] == res

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.