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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

88 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