Stash Jira key pattern false positives

I am no expert on regular expressions. I have the following key pattern:

((?<!([A-Z]{3}[0-9]{4})-?)[A-Z0-9]+-\d+)

This is so our Jira project codes match out company coding system. However the following code in a commit is detected as a Jira Issue (CLC0001-1):

prepare release 4701-CLC0001-1.0

Can I improve the key pattern to ignore this kind of match?

2 answers

1 accepted

Hi again Mark,

I think this will do the trick:

(?<=^|[^a-zA-Z0-9-])[a-zA-Z0-9]+-\d+(?=$|[^a-zA-Z0-9-])

To explain, the first part:

(?<=^|[^a-zA-Z0-9-])

Is a positive lookbehind that means the matched key must be at the start of a line (^) or immediately after a character that isn't in the range a-z, A-Z, 0-9 or a -.

The middle part:

[a-zA-Z0-9]+-\d+

Matches one or more characters in the range a-z, A-Z or 0-9; followed by a dash; followed by one or more numbers.

The last part:

(?=$|[^a-zA-Z0-9-])

Is a positive lookahead that means the matched key must be at the of a line ($) or immediately before a character that again isn't in the range a-z, A-Z, 0-9 or a -.

If all of your issue keys are three letters followed by four numbers followed by a dash and then another number (i.e. LLL####-#) you can use a slightly stricter regular expression:

(?<=^|[^a-zA-Z0-9-])[a-zA-Z]{3}\d{4}-\d+(?=$|[^a-zA-Z0-9-])

These regular expressions are for Java applications (which most Atlassian applications are) as there are some differences between different language's implementations. There great docs on Java regular expressions if you'd like to know more, or if you want to test out other variations, you can use this neat online tool.

cheers,

Tim

Thanks again Tim, I had to put an extra set of brackets around expression for Stash to accept it.

-Dintegration.jira.key.pattern=((?&lt;=^|[^a-zA-Z0-9-])[a-zA-Z]{3}\d{4}-\d+(?=$|[^a-zA-Z0-9-]))

I did have to hack the Stash and Jira databases to remove the false positives; https://confluence.atlassian.com/display/STASHKB/Reindex+JIRA+issue+keysonly seemed helpful for triggering missed keys to be added.

Hi Mark,

You could use negative lookahead/lookbehinds, non-capturing groups and/or word boundary tokens to achieve this. I'm happy to craft you a suitable regular expression, but to be sure it suits your needs can you give a few examples of what issue keys you do want to match on and what strings you don't want to match on?

cheers,

Tim

Thanks Tim,

Here are some of our commits:

CNC0021-11: Only set the abort if..

2001-CNC0012: Build version...

[maven-release-plugin] prepare release 4701-CLC0001-1.0

Only the first one is a Jira Issue. (The four digits before the Jira code denotes a component, the digits after the code are a version number [when a dot is included])

Mark

Suggest an answer

Log in or Join to answer
Community showcase
Piotr Plewa
Published Dec 27, 2017 in Bitbucket

Recipe: Deploying AWS Lambda functions with Bitbucket Pipelines

Bitbucket Pipelines helps me manage and automate a number of serverless deployments to AWS Lambda and this is how I do it. I'm building Node.js Lambda functions using node-lambda&nbsp...

681 views 0 4
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot