Set Issue Label using last Status Name

 

4 answers

1 vote
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 22, 2015

Hi David,

Based on the only requirement below of "Set Issue Label using last Status Name", i have enclosed a
simple script below which will get the previous status the issue was in and set it as a label on the issue.

Please note that the code must be placed as the first function in the list of post functions in order to get the previous
status and if it is placed after the Get current Destination Step post function then it will return the current status.

/*
* @Setup Notes:  This piece of code should be configured as a 
* script function and placed first in the list to get the previous status.
* @Setup Notes:  If this code is placed after the Get current Destination
* Step post function then it will return the current status.
*/

// Required Imports
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.label.LabelManager
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.issue.MutableIssue

// Get the current logged in user
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()//Get a to 

// Get a pointer the current Issue
MutableIssue myIssue = issue

// Get access to the Label Manager
LabelManager labelManager = ComponentAccessor.getComponent(LabelManager)

// Get the last status and replace any spaces with a - character as labels must be a single word
def previousStatus = issue.statusObject.name.replaceAll("\\s","-").toString()

// Check if the Issue has just been created and add a label only if the issue was not previously in the Create status.
if (previousStatus != "Create") {
// Set a Label with the previous Status Name on the issue
    labelManager.addLabel(user, issue.id, previousStatus, true)
}

// Save the issue
issue.store()


I hope this helps.

Thanks

Kristian

mel vega October 22, 2015

Hi, I'd be interested in that topic too. Could you please precise which kind of post-function we must use? Am i correct by understanding that we copy/paste the script above in the description box of the post-function selected? Thanks

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 22, 2015

This should be placed as a custom script post function on the transition of the workflow that you want it to execute for. For example if you have a transition going fro To Do -> In Progress then placing it on the transition in between those statuses then it will get the previous transition of To Do and set it to the label. It must be placed as the first post function in the list to get the previous status. Kristian

0 votes
mel vega October 22, 2015

Hi thanks for your prompt answer.

could you just precise where I can write the custom script? which post function, among the ones in the attached screen shot, do I have to select to feed it with the custom script.

I get it that I have to select the transition between 2 status, then the post function: but there several post-function types :

Many thanks

Melissa

 

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 22, 2015

Hi Mel, It looks as if you do not have the Script Runner add on installed. When you install Script Runner you will see a Script Post Function option and a Custom Script Post function below that. Kristian

mel vega October 26, 2015

No unfortunetaly I don't have it... but I asked for it (cross fingers!) ok thanks for your help anyway melissa

0 votes
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 20, 2015

The Script Runner documentation here shows how to get the previous workflow status and this code sample could be extended so you set the returned value as a label on the issue.

 

Kristian

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 20, 2015

There's only half a question here...

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events