Gerrit: robust regular expression to create links from text automatically (commentlink)

When there is a link to a bug tracking ticket in a commit message, and Gerrit is already configured to make links out of ticket numbers automatically, it results in a mess. There is a bug report about this since quite a while already, but no progress yet.

A workaround is to use a lookbehind regular expression, to ignore ticket numbers that are followed by a double quote or

</a>

, because they are most likely part of a generated URL.

For Jira issue IDs, it would look like this:

[commentlink "bugtracking"]
       match = \\b([A-Z][A-Z0-9]+-\\d+)(?!(\"|))
       link = https://jira/path/$1

If you need to troubleshoot this kind of issue: it is very cumbersome to test the regex directly in Gerrit, because you have to modify the config file, stop Gerrit, flush the caches and restart it. Since the regex syntax is the one of Javascript, you can use a tool like https://regex101.com. Just enter as test string a set of lines you want to match, another set that you don’t want to match, enable the “g” option, and experiment with the regular expression until it works as expected. Then you need only to escape the \ and the ” and put it in the Gerrit config file.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s