JIRA automation: Compare a string with components
Asked by William van Dalen:

Hi,

In the Description of an issue there is a text/string after the word 'subject:'. I want to compare that string with the existing components from the project. If the text/string does not exist in the components, then the field 'components' in the issue will be cleared.

For now i have something like this:

In a smart values condition:

{{issue.description.substringAfter("subject: ")}}

does not contain

{{project.components.name}}

then

{ "fields": { "components": \[\] } }

but this is not right. Can anyone help me please?

With kind regards, Will


Answers:
author: Rudy Holtkamp
body: Hi [@William van Dalen](/t5/user/viewprofilepage/user-id/892763) ,

If you create a (string) variable of the list {{project.components.name}} and compare that to the smart value {{issue.description.substringAfter("subject: ")}}, you should be Ok.

Take a look at the example below. Note that I reversed the condition and added an extra condition.

![image.png](https://community.atlassian.com/t5/image/serverpage/image-id/339434iDD2E61A167A47703/image-size/large?v=v2&px=999 "image.png")![image.png](https://community.atlassian.com/t5/image/serverpage/image-id/339435i0099D2E6E1FCC8B6/image-size/large?v=v2&px=999 "image.png")![image.png](https://community.atlassian.com/t5/image/serverpage/image-id/339436iA6E357347B0303C5/image-size/large?v=v2&px=999 "image.png")

comments: author: Rudy Holtkamp
body: Of course you should use the trigger you want and instead of the 'debug' logs, you can simply replace that by an edit issue with the components field. And leave the components field empty, like this:![image.png](https://community.atlassian.com/t5/image/serverpage/image-id/339438iB50A6DF30CC4EB12/image-size/large?v=v2&px=999 "image.png")


author: William van Dalen
body: Hi Rudy,

It's clear that i've got less experience with rule automation...at this moment :). Your recommendation, to use variables and add the results to the audit log, helped me a lot.

Also i reversed the condition and added an extra condition, as you told me.

Furthermore i experimented with the syntax of the rule......and now i'm very satisfied with the results of when triggering the automation rule. I send a message to the consultant, who asked me to develop this, so he can test it.

Many thanks for your help.

Regards, Will


author: William van Dalen
body: Hi Rudy,

In the audit log i found out that, when the subject {{issue.description.substringAfter("subject: ")}} has more than one word, the whole string/text will be added to the variable, but only the first part/word until a whitespace appears must be put into the variable {{subject}}.

I've experimented with several functions and smart values, but everything failed. May i ask you how can i use achieve this? Do you know what i mean?

Regards, Will


author: Rudy Holtkamp
body: Hi [@William van Dalen](/t5/user/viewprofilepage/user-id/892763) ,

So if you only want to have the first word after "subject:"

Change: {{issue.description.substringAfter("subject: ")}} to a find a match with a regular expression. In your case:

{{issue.description.match("(?\<=subject:\\s\*)\\w+")}}

Tip: If you are not familiar with regular expressions, then you can enter the rabbit hole [here](https://www.sitepoint.com/learn-regex/)

Pro tip: ignore the previous tip and just use chatgpt. Provide an example text and just ask the regex to get certain content. ;-)


author: William van Dalen
body: Hi Rudy,

It's very easy to use Chatgpt, but i prefer to learn/use Regex by myself :).

Thank you very much for your help. I'm going to figure this out.

Regards, Will


author: Bill Sheboy
body: Hi [@William van Dalen](/t5/user/viewprofilepage/user-id/892763)

I recommend always experimenting with *regular expressions in rules* to confirm what works for your needs, first trying the simplest thing that can possibly work, frequently writing results to the audit log, and incrementally improving it. My reason is...

This is what the documentation states, with emphasis added by me:
> The underlying implementation *is based on* Java's Pattern class and uses Matcher.find() to find matches.

<https://support.atlassian.com/cloud-automation/docs/jira-smart-values-text-fields/#match-->

The documentation does not state what is (and is not) supported for regular expressions with automation rules. In my experience, there are discrepancies in supported behaviors versus the linked Java references. And you may find community posts where customers become frustrated by trying a regular expression validated with an online regex testing tool, only to find later it returns nothing or errors in automation rules.

There also appears to be a parsing order problem with regular expressions in rules, particularly with the match() and replaceAll() functions. That is, some tokens such as escaped characters may not always work because they are escaped *after the expression validation is attempted*. The workaround for that is to create the regular expression in a created variable first (forcing early parsing) and then use that variable as a parameter for the regular expression. That technique also helps when a dynamic regular expression is needed.

Kind regards,  
Bill


author: William van Dalen
body: Hi Bill/Rudy,

Because to combine regular expressions with smart values is very frustrating i tried first to create a variable 'subject': {{issue.description.substringAfter("Subject: ")}}.

Then i create a variable 'subjecttrim': {{issue.description.trim(subject)}}.

Finally i set 'components'.

![image.png](https://community.atlassian.com/t5/image/serverpage/image-id/342156iCCD995DB5423B59D/image-size/large?v=v2&px=999 "image.png")

But that doesn't work either.

Do you have another suggestion?

Regards, Bill


author: Bill Sheboy
body: Without seeing an example of your Description field, please consider...

Let's assume your Description looks like this, and you want **findMe**:

```
some text
Subject: findMe text after subject
yet more text
the end of the description
```

Assuming the case for "Subject:" is consistent.

Parsing that with text functions, you could use this:

```
{{issue.description.substringAfter("Subject: ").split(" ").get(0)}}
```

That will grab the text after the prefix, split the remainder into a list of values, and get the first one.

Doing that with a match() needs an adjustment as there might be embedded newlines; using replace("\\n", " ") before the match() will eliminate them. Once that is added, please try the expression Rudy suggested.


author: William van Dalen
body: Hi Bill,

Something unexplained happens (at least to me)

For the time being (for test purposes) i created a variable 'compo' with a smart value to extract the desired component = {{project.components.name.get(1)}}. I wrote in the log. Of course the result is the expected value.

I created a variable 'subject' \< {{issue.description.substringAfter("Subject: ").split("\\n").first()}}.

In the rule i put an if-else-statement. In the if-statement i check the condition if {{compo}} equals ((subject}}.

In this rule every time the rue to the else-statement with the error 'The issue did not match the condition'. How is this possible, because in the audit log it wrote that {{compo}} exactly matches {{subject}}.?

Do you have explanation?

Regards, Will


author: Rudy Holtkamp
body: Is there perhaps a space in one of the strings? Is there a casing difference?


author: Bill Sheboy
body: Will, would you please post images of your current, complete rule, the create variable action, and the condition where it is tested? Those will help provide context.

And to help test Rudy's suggestion, consider writing the variables to the audit log to check what they contain \*exactly\*, such as like this:

```
compo: ZZZ{{compo}}ZZZ
```

That will make it easier to see any spacing differences.

Thanks!


author: William van Dalen
body: Hi Bill/Rudy,

Eventually the solution was easy.

Previously i already checked if there were spaces in the variable, but apparently it was not sufficient. The suggestion of Rudy triggered me. Then i checked again.......and what did i find in the audit log?

![screenshot auditlog false.png](https://community.atlassian.com/t5/image/serverpage/image-id/347117i074BFE446729033E/image-size/large?v=v2&px=999 "screenshot auditlog false.png")

So i created a variable called **subjecttmp** with the smart value **{{issue.description.substringAfter("Onderwerp:").split("\\n").first()}}**

then i created a variable called**subject** with the smart value **{{subjecttmp.trim()}}**

![screenshot autditlog.png](https://community.atlassian.com/t5/image/serverpage/image-id/347122iDB642FBBE5AC7114/image-size/large?v=v2&px=999 "screenshot autditlog.png")

So now the comparison between {{compo}} and {{subject}} was succesful.

I learned a lot. Thank you both of you.

Regards, Will


Tags: automation

cloud

jira-cloud