Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Condition based on number of labels

Olga Videc
Community Champion
June 4, 2018

Hello, 

I have a question I would like to make a condition that wouldn't allow an issue to be closed if it has less than two labels. 

Any ideas how could I achieve that? I tried using script runner but I'm not familiar enough with groovy to do that.

BR, Olga

4 answers

2 accepted

2 votes
Answer accepted
Tansu Akdeniz
Community Champion
June 4, 2018

Hi @Olga Videc,

Add Script Condition -> Custom Script Condition, than paste the following code;

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.label.LabelManager;

passesCondition = false;

LabelManager labelManager = ComponentAccessor.getComponent(LabelManager);
def labels = labelManager.getLabels(issue.id).collect{it.getLabel()} 
   
if(labels.size() > 1){
    passesCondition = true;
}

So, users must add at least two labels in order to see the transition button. 

Tansu

2 votes
Answer accepted
Tarun Sapra
Community Champion
June 4, 2018

Please see samples here - https://scriptrunner.adaptavist.com/5.0.2/jira/recipes/workflow/conditions/all-subtasks-resolved.html

passesCondition = true
def labels = issue.getLabels()

if(labels && labels.size() < 2) {
passesCondition = false
}
Tarun Sapra
Community Champion
June 4, 2018

Put the above condition in the "condition" section of workflow script runner and that should do the trick. Now the workflow button for close will not be visible if issue has less than 2 labels. But I have a feeling that you should put in the "validation" phase instead of condition phase that's much better.

0 votes
Olga Videc
Community Champion
June 5, 2018

Managed to work around added my comment in the error that appears when I run simple validator script, anyway thanks for help :D both of your scripts work :D 

0 votes
Olga Videc
Community Champion
June 5, 2018

Hello, both solutions are correct only one correction for the first one I added !=null so because I didn't work for blank, both work for condition but not for custom validator it works for simple validator but with simple validator I cant change the error message, custom validator executes but doesn't block the transition any idea why?

passesCondition = true
def labels = issue.getLabels()

if(labels != null && labels.size() < 2) {
passesCondition = false
}

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events