Automation for Jira: How to match a string to insert into different field?
Asked by Jenni J?rvinen:

My company uses Jira Cloud with GitLab integration. Every time merge request in GitLab has the Jira issue key added into it, the GitLab integration adds a "mentioned on" issue link, as shown below:

![gitlab-link.PNG](/t5/image/serverpage/image-id/93143i16DD9EDC51BB9F96/image-dimensions/433x61?v=v2 "gitlab-link.PNG")

This link points to an URL which is format of: https://\<my-company-url\>/merge_requests/1359

Our issues also have custom field for the merge request data (used in Confluence for Jira issue filters), shown as below:

![morko.PNG](/t5/image/serverpage/image-id/93144i29A373894BC0B05E/image-dimensions/374x99?v=v2 "morko.PNG")

Currently the custom field is manually filled with copy\&paste after the "mentioned on" link has appeared, but I'm looking for a way to automate this.

Basically what I need to do is to parse the last four digits of the URL where the "mentioned on" link points and insert them into the custom field (shown above) as in the picture shown above.  

Is this possible?


Answers:
author: Mehmet A _Bloompeak_
body: Hi [@Jenni J?rvinen](/t5/user/viewprofilepage/user-id/1174725) ,

I hope below "Issue commented" trigger works.

```
When: Issue commented
If: Compare two values
  First Value={{comment.body}}
  condition = contains
  second value = |https://my-company-url/merge_requests/

Then Edit issue fields
  Choose fields to set = your custom field
  Value = {{comment.body.replaceAll(".*(https://my-company-url/merge_requests/[0-9]+)\].*","$1")}}
  
```

![Screen Shot 2020-05-02 at 05.14.37.png](https://community.atlassian.com/t5/image/serverpage/image-id/93172i89C2CEF5EB753718/image-dimensions/567x352?v=v2 "Screen Shot 2020-05-02 at 05.14.37.png")


author: Jenni J?rvinen
body: Hi [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313) ,

and thanks for your answer. Still not sure how to proceed that, mostly because not sure how/where to trigger the parsing action.

The URL with the digits I need to parse is visible on the JSON data (below), but no idea how to get it from there?  

This is the JSON data of one ticket which has the GitLab mention, and it looks like this (URLs and usernames edited for privacy):

```
"comment": {"comments": [{"self": "https://mtvjira.atlassian.net/rest/api/2/issue/102106/comment/190277","id": "190277","author": {"self": "https://mtvjira.atlassian.net/rest/api/2/user?accountId=557058%3Ae1b49e38-24c3-4915-bcd3-dd4635b37a50","accountId": "557058:e1b49e38-24c3-4915-bcd3-dd4635b37a50","avatarUrls": {"48x48": "https://secure.gravatar.com/avatar/356a95e0d2e26887ad96b01aa621105b?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FGI-2.png&size=48&s=48","24x24": "https://secure.gravatar.com/avatar/356a95e0d2e26887ad96b01aa621105b?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FGI-2.png&size=24&s=24","16x16": "https://secure.gravatar.com/avatar/356a95e0d2e26887ad96b01aa621105b?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FGI-2.png&size=16&s=16","32x32": "https://secure.gravatar.com/avatar/356a95e0d2e26887ad96b01aa621105b?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FGI-2.png&size=32&s=32"},"displayName": "Gitlab Integration","active": true,"timeZone": "Europe/Helsinki","accountType": "atlassian"},"body": "[Author|https://my-company-url/author] mentioned this issue in [a merge request of repo/project|https://my-company-url/merge_requests/495]:\n'CMW-141: Add hayu code to user after registration'",
```

The problem is that I'm not sure how to handle that data. I've played around with Jira automation quite a lot but never anything this complicated.  

a) In which step of the automation is the data parsed, and how?  
b) How is the parsed data then included in the custom field?  

Those two things are the ones I'm struggling with. I've been reading pages and pages of docs but I could really use a clear example that I can then edit and modify on my own.

comments: author: Bill Sheboy
body: Hi [@Jenni J?rvinen](/t5/user/viewprofilepage/user-id/1174725)

Perhaps I misunderstood. I thought that you were already integrated to the point where you were updating a Jira issue with a comment for the mention.

Is your Jira issue getting such a comment with merge information? If so, please try a comment trigger, and then parse out the merge request data:

<https://support.atlassian.com/jira-software-cloud/docs/automation-triggers/>

Also please note that nesting of smart value functions does not appear to be possible. So if that entire JSON is landing in the Jira issue comment, you may need to parse it in steps using multiple custom fields and rules.


author: Bill Sheboy
body: Hi [@Jenni J?rvinen](/t5/user/viewprofilepage/user-id/1174725)

Have you tried automation rules, triggered on the mention, and then parse out the number using the function substringAfterLast, looking for the "/"?

Here is the complete help on that function: <https://support.atlassian.com/jira-software-cloud/docs/smart-values-text-fields/>

And here is the how-to for getting started with the automation:

<https://www.atlassian.com/software/jira/guides/expand-jira/automation>

Best regards,

Bill


Tags: cloud

jira-software