Attempting to set assignee to issue as part of a custom listener not working. Edited

I have been trying to figure out why the following script doesn't work as part of a transition in a workflow

/**
* Traverses history of changes made to the provided ticket determining the last 'Assignee' field update.
* If an 'Assignee' update is found, updates the current assignee to be that of the user previous to the last update
* to the 'Assignee' field.
*
* @author Tim Reiking
*/

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import org.apache.log4j.Level

log.setLevel(Level.DEBUG)

// === BEGIN PARAMETERS ===

String logPrefix = "[Update to previous Assignee] "
String issueKey = event.issue.key

// === END PARAMETERS ===

def issueManager = ComponentAccessor.getIssueManager()
def issueService = ComponentAccessor.getIssueService()
def mIssue = issueManager.getIssueObject(issueKey)
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// Traverses history of the ticket
log.debug("${logPrefix}Executing update for ${issueKey}")
def changedItems = changeHistoryManager.getAllChangeItems(mIssue)

boolean userUpdated = false
/*def starter = changedItems.size() > 5 ? changedItems.size()-6 : 0
for (int i = starter; i < changedItems.size(); i++) {
def item = changedItems[i] // Change Item event
log.debug("'${item.getCreated()}':\tOld - ${item.getFroms()}\tTo - ${item.getTos()} - Field: ${item.getField()}")
}*/

for (int i = changedItems.size()-1; i >= 0; i--) {
def item = changedItems[i] // Change Item event

// Finds the last change to the 'assignee' field and applies the previous person to that user.
if(item.getField().equalsIgnoreCase("assignee")) {
String previousUserName = item.getFroms().keySet().toArray()[0]
String currentUserName = mIssue.getAssigneeId()

mIssue.setAssigneeId(previousUserName)
def updatedIssue = issueManager.updateIssue(currentUser, mIssue, EventDispatchOption.DO_NOT_DISPATCH, false)
if(updatedIssue.getAssigneeId().equalsIgnoreCase(previousUserName)) {
String text = "${logPrefix}Updating 'Assingee' to be '${previousUserName}' from '${currentUserName}'"
//ComponentAccessor.getCommentManager().create(mIssue, currentUser, text, false)
log.debug(text)
userUpdated = true
} else {
log.debug("${logPrefix}Errors: Was not updated")
}
break
}
}

//changeHistoryManager.removeAllChangeItems(mIssue)
if(!userUpdated) {
log.debug("${logPrefix}Update could not be applied. No previous assignee change was found")
}

userUpdated

I developed it in the Scriptrunner console and everything is working as expected when I run it from the console. If I put the same code in a post-function or as listener triggered by a custom event associated to a workflow transition, it doesn't work. Strangely enough it adds the user update, with the update I expect, to the history of the ticket, but doesn't actually update the assignee. If I add an "Issue Committed" listener to the same script, it works as expected when I add a comment. Therefore, I believe it is related to something with the transition. I have tried this in my dev system and a vanilla system with the same result.

Any ideas would be appreciated.

Thanks,

Tim

1 answer

0 votes

Hi Tim,

Perhaps this is a dumb question, but I'm having trouble understanding the use-case for this script. From what I understand, if someone changes the Assignee on an issue, your listener will automatically undo the user's change and set the Assignee back to what it was. Is that correct? 

Your script seems like it aims to prevent users from changing theAassignee. Would it not be simpler to just disallow the user from editing the Assignee in the first place? Or do you have some sort of other script (not specifically a user) that is changing the Assignee field?

Thanks,

Josh

Josh,

Thank you for your reply.

The above script would only be triggered a workflow transition and is part of the workflow's QA flow. My use case is as follows:

  • A developer finishes requirement of ticket and selects transition "Ready for QA"
  • A transition screen appears asking the developer to change the assignee of the ticket to a member of the QA team.
  • The QA team finishes testing and selects transition "Approved"
  • The above script is triggered by a custom event called "QA Approved" as part of the post function of the "Approved" transition.
  • The script should scan the change history for last change of assignee field and determine the user prior to the change in "Ready for QA" transition. This assignee should be the developer.

The purpose of the script is to automatically reassign the ticket back to the developer after the QA team has finished testing and also not requiring an additional user custom field to track the developer after the hand off to the QA team.

The script is self works as desired when testing it in the console on a test ticket. The problem is that it doesn't work when it is configured as a listener of the custom event, even though the change history of the ticket is updated to show the proper reassigning of the ticket from the QA team back to the developer.

Please let me know if you have any additional questions. I will make sure to get a quicker response this time.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 29, 2018 in Marketplace Apps

How to set up an incident workflow from the VP of Engineering at Sentry

Hey Atlassian community, I help lead engineering at Sentry, an open-source error-tracking and monitoring tool that integrates with Jira. We started using Jira Software Cloud internally last year, a...

1,333 views 0 8
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