Transition issue upon issue update with JMWE/Scriptrunner

- March 19, 2021

One of the users in our Jira instance requested an automation process, where an issue auto-transitions from the status Done to the status With Team if a user in the "Customer" role posts a comment on the ticket that's listed as Done.

I know this would be easily doable through Code Barrel's Automation for Jira add-on, but given that our Data Center instance doesn't have this, I was wondering if this was possible with either JMWE or Scriptrunner?

1 answer

1 accepted

1 vote
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 19, 2021

Yes that's possible with scriptrunner.
But dealing with workflows and transition is one of the slighly more involved/complex scripts.

You'll want to use a custom scripted listener with the Issue Commented event

It will look something like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.workflow.TransitionOptions
import com.atlassian.jira.issue.IssueInputParametersImpl
import com.atlassian.jira.security.roles.ProjectRoleManager

def issueService = ComponentAccessor.issueService
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)

def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def custRole = projectRoleManager.getProjectRole('Customer')

Issue issue = event.issue

if(projectRoleManager.isUserInProjectRole(currentUser, custRole, issue.projectObject)){
def transitionOptions = new TransitionOptions.Builder()
transitionOptions.skipConditions() //remove this if you want the conditions to be evaluated against the current user
transitionOptions.skipValidators() //remove this if you want your validators to fire and possibly prevent the completion

def inputParams = new IssueInputParametersImpl()

/* use inputParams to set any values that must be set during the transition if you don't have other defaults in the workflow
inputParams.addCustomFieldValue('customfield_123456', 'string value')
*/
def actionId = 123 //this is the id of the transition you want to execute
def transitionValidationResult = issueService.validateTransition(currentUser, issue.id, actionId, inputParams, transitionOptions.build())
if (transitionValidationResult.isValid()) {
def transitionResult = issueService.transition(currentUser, transitionValidationResult)
if(transitionResult.isValid()){
log.debug "The transition was succesful"
} else {
log.error "The transition failed: ${transitionResult.getErrorCollection()*.errorMessages}"
}
} else {
log.error "The transition failed the validation: ${transitionValidationResult.getErrorCollection()*.errorMessages}"
}
}

If you need to examine the content of the comment to decide whether or not to trigger the transition, you'll want to look into event.comment.body

- March 22, 2021

Many thanks @Peter-Dave Sheehan !

The listener script works like a breeze. All I did to change this up a bit was adding a couple other conditions to line 15 to make sure that the status is initially 'Done' and that the issue type is 'Support Request' to completely fulfill our specific use case.

Thank you once again for providing your insight like you have done so before in the past :)

 

-Ian

Like Peter-Dave Sheehan likes this

Suggest an answer

Log in or Sign up to answer