How to return a specific label that contains a substring from multiple labels?
Asked by Axel Kerksiek:

I am writing some automation for JIRA and I want to be able to return a specific label that contains a specific substring from a list of multiple labels.

For Example: There are two labels: abc123, 123456. I want to return the label with an "abc" substring. I have tried the following with no success:

{{issue.labels.first.contains("abc")}} -\> returns true or false but i want the actual label, not a boolean.

{{issue.labels.match(\*.abc.\*)}} -\> does not return anything.

I thought that i could just find the first element in the label list that contains a substring but it doesnt seem to be that easy. Any help would be great thanks!


Answers:
author: Mark Segall
body: Hi [@Axel Kerksiek](/t5/user/viewprofilepage/user-id/4844044) and welcome to the community!

Can you please provide a bit more context around what you are trying to do? Are you simply trying to evaluate issues where they contain labels that begin with "abc"? If so, what is your intended action that you plan to take with that information? This would help in providing a quality answer.

comments: author: Axel Kerksiek
body: Hey [@Mark Segall](/t5/user/viewprofilepage/user-id/4667637) thanks for the reply. So I am making some Automation in JIRA to send a slack message every time a certain issue type that has a label containing a specific string gets created. I already figured out all the filtering logic and slack integration logic, and I am on the step where I send out a message to slack. In the message body I want to be able to get the text value of the label that has a specific string.

I.E. If there is a label that has the string "abc123" I want to be able to access that label and place it in the message body, such as "the label abc123 exists".

The problem I seem to be having is that I cant return the label element based on the condition I am specifying. So when I try something like {{issue.labels.first.contains("abc")}} I get either true of false as the output value, but what I want is the label itself that passed the condition.

Hope that helps provide more context, and thanks for the help!


author: Mark Segall
body: ok... So the thing about labels is that they are lists. You could try playing around with some variation of {{#issues.labels}}...{{/}} or...

You could capture labels as a variable and branch off of that which is the route I took:

![2022-03-28_16-47-47.png](https://community.atlassian.com/t5/image/serverpage/image-id/190348i01AEEF01AB15C581/image-size/large?v=v2&px=999 "2022-03-28_16-47-47.png")

Here's further context:

**varLabelRaw**

```
{{issue.labels}}
```

**varLabel**

```
{{varLabelIterate.match("(abc\d+)")}}
```

Again, there may be a way to do this without branching into the labels, but this is what worked for me.


author: Axel Kerksiek
body: Thanks for the info! I think my situation is slightly different so I tried to take your advice and match it to what I'm trying to do. Based on your feedback I tried the following but was still unable to get it to work:  
![SampleAutomationRules.png](https://community.atlassian.com/t5/image/serverpage/image-id/190516iD1C997191BCF6421/image-size/large?v=v2&px=999 "SampleAutomationRules.png")

I created a foreach loop (set the smart value to {{issue.labels}} and gave it a variable name of varLabel).

Then I created an if statement that checked if varLabel contains a string.

Then if that succeeded I created a variable called targetLabel (Variable name is targetLabel and smart value is {{varLabel}}).

Then i tried to access {{targetLabel}} in my message body. However when I run this I dont get anything.

Do you know what I might be missing?

If it was code I would write the logic like this (assuming there is only one label containing the target substring):

```
var targetLabel;

foreach(label in labels)

{

? ?if(label.contains("regression"))

? ? {

? ? ? ?targetLabel = label;

? ? }

}

print(targetLabel);
```


author: Mark Segall
body: I believe there is a limitation in Jira that variables created in a branch only exist within the branch, so you may need to move your slack action into the branch for it to read targetLabel. If you still see nothing, I would add an audit log action right after your variable just to make sure it is in fact capturing something for targetLabel.


author: Axel Kerksiek
body: ah ok that worked, thanks for your feedback and help!


Tags: smart-value