How do I add a label in post function using scriptrunner?

I tried using both scripts on https://answers.atlassian.com/questions/307827 but neither were successful. When adding the script, the error says "cannot find matching method...". Has anyone been successful with this? Thank you!

1 answer

1 accepted

2 votes
Accepted answer

"cannot find matching method..."

That's all about error or is there something else? Seems to be something more.

Try to debug both scripts in a script runner - works for me as well.

The API may have changed since that answer. As Aleks said, the useful part of the error you get is in the "...". 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.label.LabelManager
  
def user = ComponentAccessor.jiraAuthenticationContext.getUser()
  
LabelManager labelManager = ComponentAccessor.getComponent(LabelManager)
def labels = labelManager.getLabels(issue.id).collect{it.getLabel()}
labels += 'new_label'
labelManager.setLabels(user,issue.id,labels.toSet(),false,false)

The error I get is:
"Static Type Checking -Cannot find matching method: com.atlassian.jira.issue.label.LabelManager#setlabels(com.atlassian.jira.user.Ap
 java.lang.Long, java.util.Set , boolean, boolean). Please check if the declared type is right and the method exists"

The exact script I'm getting this error on is below (the code block above didn't work for some reason):
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.label.LabelManager

def user = ComponentAccessor.jiraAuthenticationContext.getUser()

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

Hm... Which JIRA version do you use? Maybe it is old enough and do not have this method

Yes, there is difference. In JIRA 7 method signature is 

setLabels(ApplicationUser remoteUser, Long issueId, Set<String> labels, boolean sendNotification, boolean causeChangeNotification)

but in 6.4.6

setLabels(com.atlassian.crowd.embedded.api.User remoteUser, Long issueId, Set<String> labels, boolean sendNotification, boolean causeChangeNotification)

Try to replace last string this way:

labelManager.setLabels(user.getDirectoryUser() ,issue.id,labels.toSet(),false,false)

that did the trick! Thank you so much!

Suggest an answer

Log in or Sign up to answer
Community showcase
Asked Thursday in Jira Ops

I'm John Allspaw, Ask Me Anything about incident analysis and postmortems

I'm John Allspaw, co-founder of   Adaptive Capacity Labs, where we help teams use their incidents to learn and improve. We bring research-driven methods and approaches to drive effective inciden...

5,355 views 21 17
View question

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you