Set Issue Label using last Status Name

 

4 answers

This widget could not be displayed.

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

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

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

This widget could not be displayed.

There's only half a question here...

This widget could not be displayed.

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

This widget could not be displayed.

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

 

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

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

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

526 views 3 19
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