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

This widget could not be displayed.

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)
    }
}

 

 

This widget could not be displayed.

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.

This widget could not be displayed.

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.

This widget could not be displayed.

Try this 'Post functions' solution.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted 3 hours ago in Jira

Atlassian Research Workshop opportunity on Sep. 28th in Austin, TX

We're looking for participants for a workshop at Atlassian! We need Jira admins who have interesting custom workflows, issue views, or boards. Think you have a story to sha...

14 views 0 0
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