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

"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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

64 views 0 5
Read article

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