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?
Why not create a transition that comes back to the same step (status)? So no worrying about doing transitions using code.
Ravi
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 ;)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.