Behaviour on label field in jira

Katarzyna Kielbasa March 3, 2016

Hi,

 

I would like to add new labels to label field based on the issue type.

I want to use behaviour plugin to do so.

I already applied this script in a post function on first transition in a workflow (after create):

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.label.LabelManager
  
def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()

LabelManager labelManager = ComponentAccessor.getComponent(LabelManager)
def labels = labelManager.getLabels(issue.id).collect{it.getLabel()}
labels += 'Incident'
labelManager.setLabels(user,issue.id,labels.toSet(),false,false)

 

 

And it works fine.

But I want this functionality be set as a behavior of a field for few issue type (not associated with specific transition of the workflow, and also updated when the issue is moved to the different issue queue-there will be few categories of the labels depending on issue type, like "incident", "service request", "project"). Can you please advise how I can rewrite this code to be useful as a server site script?

Thanks,

Kasia

3 answers

1 vote
Borys Chernetskyi June 26, 2020

Solved:

//Set one value
String newValue = "111222"
Map<Integer, String> option = [(newValue.toInteger()):newValue];//ID and name value

def field = getFieldById("customfield_12021");
field.convertToMultiSelect();
field.setFieldOptions(option);
field.setFormValue(newValue); //name value
field.convertTo('com.atlassian.jira.issue.customfields.impl.LabelsCFType'); //convert again and fix bug

//Set more value
String newValue1 = "111222"
String newValue2 = "333444"
Map<Integer, String> options = [
(newValue1.toInteger()):newValue1,
(newValue2.toInteger()):newValue2,
];

def field = getFieldById("customfield_12021");
field.convertToMultiSelect();
field.setFieldOptions(options); //ID and name value
field.setFormValue([newValue1,newValue2]); //name value
field.convertTo('com.atlassian.jira.issue.customfields.impl.LabelsCFType'); //convert again and fix bug
0 votes
Matthew Beda June 3, 2019
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.label.LabelManager


def userManager = ComponentAccessor.userManager
def labelManager = ComponentAccessor.getComponent(LabelManager)

def contextIssue = issueManager.getIssueObject(getContextIssueId())

MutableIssue issue = contextIssue
//User to Run function
def anuser = userManager.getUserByKey("user")
//Get the labels for the issue, place them in a list.
def labels = labelManager.getLabels(issue.id)*.label
//These two are the labels you want to add to your list of labels, if you want to replace them use:
// labels = ['one', 'foo']
labels += ['one','foo']
labelManager.setLabels(anuser, issue.id, labels.toSet(), false, false)

 



Borys Chernetskyi June 26, 2020

Hi, @Matthew Beda  is not working.

at Script1.run(Script1.groovy:17)
java.lang.NullPointerException: Cannot get property 'id' on null object
2020-06-26 16:10:11,163 http-nio-8080-exec-25 ERROR admin 970x129483x1 35kaww 100.21.105.10,0:0:0:0:0:0:0:1 /rest/scriptrunner/behaviours/latest/runvalidator.json

17 line: labelManager.setLabels(anuser, issue.id, 12021L, labels.toSet(), false, false); 

contextIssue does not have an identifier

Matthew Beda October 30, 2020

What version of Jira are you using?

0 votes
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 31, 2016

Hi,

I have looked into this issue it turns out that it is not possible to set the value of the Labels field using the Behaviours module of Script Runner. I have enclosed a screenshot from the Script Runner documentation showing that this is not possible.

labels bug.png

A support ticket has been created for this issue can be viewed at GRV-918.

For the meantime I would suggest you keep using the code above using post functions to add labels. You could use the example if statement code in your post function to check if the issue is of a certain type and if so only add labels for that issue type.

// Repeat the if statement for each issue type that needs to be checked for
if (issue.issueTypeObject.name == "Task"){
// Add Label code here
}

I hope this helps

Kristian

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events