Adding Labels via a Post-Function

Greetings,

I am wondering whether it is possible to add a label to a Jira ticket while progressing through a work flow.

It seems like that would be something a post-function can do, but it seems I am unable to.

Thanks.

5 answers

3 votes

Here's some example code for script runner that does that:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.bc.issue.label.LabelService
import com.atlassian.jira.bc.issue.label.LabelService.AddLabelValidationResult
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.security.JiraAuthenticationContext
import com.opensymphony.user.User
import org.apache.log4j.Category

ComponentManager componentManager = ComponentManager.getInstance()
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
JiraAuthenticationContext authenticationContext = componentManager.getJiraAuthenticationContext()
User user = authenticationContext.getUser()
LabelService labelService = ComponentManager.getComponentInstanceOfType(LabelService.class)

MutableIssue myIssue = issue

["somelabel", "someotherlabel"].each {String labelName ->
    AddLabelValidationResult validationResult = labelService.validateAddLabel(user, myIssue.id, labelName)
    if (!validationResult.errorCollection.hasAnyErrors()) {
        labelService.addLabel(user, validationResult, false)
    }
}

For JIRA 7 you would use:

import com.atlassian.jira.bc.issue.label.LabelService
import com.atlassian.jira.bc.issue.label.LabelService.AddLabelValidationResult
import com.atlassian.jira.component.ComponentAccessor

def authenticationContext = ComponentAccessor.getJiraAuthenticationContext()
def user = authenticationContext.getLoggedInUser()
def labelService = ComponentAccessor.getComponent(LabelService)

["somelabel", "someotherlabel"].each {String labelName ->
    AddLabelValidationResult validationResult = labelService.validateAddLabel(user, issue.id, labelName)
    if (!validationResult.errorCollection.hasAnyErrors()) {
        labelService.addLabel(user, validationResult, false)
    }
}

 

 

It would be possible with the Script Runner plugin.

Here it is some documentation. But basically you can create a Script PostFunction that could add a label in the issue.

I tried this solution on both JIRA server and cloud. No need for script runner.

Hi!

Unfortunately this script seems not to work although i´ve configured it as described:

2015-09-08 17_59_50-Transition_ Delayed - JIRA.jpg

2015-09-08 18_00_15-Update Workflow Transition Script Post-Function Function - JIRA.jpg

What´s wrong here? Using JIRA 6.4.5...

Thanks & regards,
Hans-Hermann

can you see if there is anything in the logs...

import com.opensymphony.user.User
@ line 7, column 1.
Script18.groovy: 7: unable to resolve class com.opensymphony.user.User
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:

The User class in your code seems not present in the latest JIRA (I'm using 6.3.10)

That's why this code is worng now.

I've updated my answer for JIRA 7. It will probably work on JIRA 6.3... the main thing is to remove the old User import.

Try this 'Post functions' solution.

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
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

239 views 1 18
Join discussion

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