Script to transition an issue to the prior status

C_ Derek Fields
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 25, 2019

I have written a post-function script that is intended to transition the issue back to the prior status. The reason is that there are certain post-functions that I want to run without actually changing the status.

By way of example, I have another post-function that updates a database table with some information that the user enters on the transition screen. I want to be able to execute this script regardless of the current status of the issue and without changing the status of the issue. Instead of creating a special transition on every status just to handle this case, I want to create a special status that would be accessible from all statuses and would return to all statuses. This makes maintaining this functionality easier since I only have one real transition to worry about if I need to make a change.

If you have a better way than this, let me know. 

With that said, I created the following script, which works as expected with one problem. The status that is shown on the View screen doesn't match the status that I have transitioned to. For testing, I called the status and the transition to it "TEST" and run the script as the very last activity in the list of post-functions for the TEST transition. After running the script, the issue still shows TEST as the status even though the script reports that the current status is "In Progress". 

Here is the script"

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.config.StatusManager

// Return the previous Status (the one before this transition)
int getPreviousStatus(MutableIssue issue) {
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def history = changeHistoryManager.getChangeHistories(issue)
def hlist = history.collect {
it.getChangeItemBeans().collect {
[it.getField(), it.getFrom(), it.getTo(), it.getCreated()]
}
}.collect {
it.findAll {it[0] == 'status'}
}.collect {
it[0]
}.sort {it[3]}

hlist[-2][2] as int
}

def issue = ComponentAccessor.getIssueManager().getIssueObject("TP-41")
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

log.warn("Current Status before transition is ${issue.getStatus().getName()}")

// Retrieve the Step associated with the previous status
def status = ComponentAccessor.getComponent(StatusManager).getStatus(getPreviousStatus(issue).toString())
log.warn(status.getName())
// Get the action that will transition back to the previous status
def workflow = ComponentAccessor.getWorkflowManager().getWorkflow(issue)
def action = workflow.getActionsWithResult(workflow.getLinkedStep(status)).find {it.name == status.name}
log.warn(action)

// Transition the Issue. We assume that all conditions and validators will pass
def issueService = ComponentAccessor.getComponent(IssueService)
def validateTransition = issueService.validateTransition(currentUser, issue.getId(), action.id, issueService.newIssueInputParameters())
if (validateTransition.isValid()) {
def transitionResult = issueService.transition(currentUser, validateTransition)
if (transitionResult.isValid()) {
log.warn("Transition to $status succeeded")
log.warn("New Issue Status is ${transitionResult.getIssue().getStatus().getName()}")
} else {
log.warn("Transition to $status failed")
log.warn(transitionResult.getErrorCollection())
}
} else {
log.error("Transition to $status invalid")
log.error(validateTransition.getErrorCollection())
}

The log that this generated is

2019-09-25 18:00:50,257 WARN [workflow.ScriptWorkflowFunction]: Current Status before transition is Test
2019-09-25 18:00:50,265 WARN [workflow.ScriptWorkflowFunction]: In Progress
2019-09-25 18:00:50,265 WARN [workflow.ScriptWorkflowFunction]: In Progress (fieldscreen)
2019-09-25 18:00:50,294 WARN [workflow.ScriptWorkflowFunction]: Transition to IssueConstantImpl[[GenericEntity:Status][sequence,3][statuscategory,4][name,In Progress][iconurl,/images/icons/statuses/inprogress.png][description,This issue is being actively worked on at the moment by the assignee.][id,3]] succeeded
2019-09-25 18:00:50,294 WARN [workflow.ScriptWorkflowFunction]: New Issue Status is In Progress

As you can see, the status was Test and is changed to In Progress. This is what I want. But the View screen still shows "TEST" as the status. 

Suggestions?

2 answers

0 votes
Ravi Sagar _Sparxsys_
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 26, 2019

Hi @C_ Derek Fields 

Why not create a transition that comes back to the same step (status)? So no worrying about doing transitions using code.

Ravi

C_ Derek Fields
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 26, 2019

I have to do this for 3 different sets of post-functions for 12 different steps. It would be a maintenance nightmare to have all of these transitions to maintain. What I really want is a "button" that will execute a script without going through a transition at all. However, Jira doesn't seem to support this without going through a transition.

Ravi Sagar _Sparxsys_
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 26, 2019

Hi @C_ Derek Fields 

You can try using Script Fragments to insert a web item (like a button) as well with weight -10 or something so it appears before the More operation on the issue. Check this page. Take a look you might find it easier than running post functions in workflow.

Ravi

Prasad Nagarale April 2, 2020
  1. I have two statuses ACR and HOLD with all status transition.
  2. From status ACR and On Hold the transition should be come to previous status only.
  3. I need script for this.Could you please help me to find this solution?
0 votes
David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 26, 2019

This has to do with the way you programmatically transition the current issue during a transition. I know that, I’m JMWE, we had to use some sophisticated workarounds to make it work. I’ll see if I can dig out the code we used. 
Of course, you could also look into using JMWE to implement your requirement ;) 

C_ Derek Fields
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 26, 2019

If you can find the code, that would be awesome. The client doesn't have JMWE but I may look deeper into whether that would solve my problem since it may be cheaper to license it than to pay me for a one-off.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events