Adding Labels via a Post-Function

Chris Prior May 7, 2012

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
JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 30, 2013

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

 

 

1 vote
Ramiro Pointis
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 7, 2012

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.

Ulrich Kuhnhardt [IzymesDev]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 5, 2018

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

Like Noah Hofmann likes this
0 votes
Ulrich Kuhnhardt [IzymesDev]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 5, 2018

Try this 'Post functions' solution.

0 votes
Hans-Hermann Hunfeld September 8, 2015

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

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 8, 2015

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

nicolas February 9, 2016

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.

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 9, 2016

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.

0 votes
Laszlo Kremer
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 30, 2013

Suggest an answer

Log in or Sign up to answer