Help using a groovy script listener when moving an issue between projects Edited

Hi everyone,

We are deploying JIRA across several teams, and I am looking for a way to streamline the process for moving issues between projects. For example, say an issue needs to go from Project A to Project B. Currently, the lead for A has to (or should) set the Assignee to 'Unassigned,' revert the Status to 'To Do,' and then do the move procedure.

I would like to have a listener take care of changing the Assignee and the Status fields, so that the lead only has to worry about moving the issue. This will resolve problems we are having with the Project B team receiving an issue from A but being unaware as it's showing as assigned and in progress with a Project A resource.

Currently this is the code I have in the listener and there are no errors in the log, but the changes are not being saved. I somehow need to use IssueManager.UpdateIssue(), but I can't get it to work. Any help is appreciated:

 

import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

IssueManager issueManager = ComponentAccessor.getIssueManager();
Issue issue = issueManager.getIssueObject(event.issue.id)

log.warn("Event: ${event.getEventTypeId()} fired for ${event.issue} of type ${issue.getIssueType().id}")
if (issue.getIssueType().id=="10200"){
log.warn("Issue type check passed")
issue.setStatusId("2")
issue.setAssignee(null)
} else (log.warn("Issue type checking failed"))

 

1 answer

I'm assuming your scripted listener is already listening for the "Issue Moved" event.

Your changing the issue object but not saving it back to the database

Your best off using issue service to do this sort of thing as it will also reindex the issue for you. To do this you can use the following example:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent

def event = event as IssueEvent
def issue = event.issue

def issueService = ComponentAccessor.getIssueService()

if (issue.getIssueType().id=="10200"){

    // do transition first
    def transitionParameters = issueService.newIssueInputParameters()
    // you'll need to have a valid transition setup in your workflow for this
    // replace with your id
    def transitionId = 2
    def user = event.user

    def validationResult = issueService.validateTransition(user, issue.id, transitionId, transitionParameters)

    if (validationResult.isValid()) {
        issueService.transition(user, validationResult)
    } else {
        log.warn validationResult.errorCollection.errors
    }

    // unassign last
    def updateParameters = issueService.newIssueInputParameters()

    updateParameters.setAssigneeId(null)

    validationResult = issueService.validateUpdate(user, issue.id, updateParameters)

    if (validationResult.isValid()) {
        issueService.update(user, validationResult)
    } else {
        log.warn validationResult.errorCollection.errors
    }
}

A few points to note are that you should replace transitionId with the id from your workflow to transition the issue back to "To Do". You can mark the transition as hidden with a condition if you don't want users to see it.

After it's transitioned the update to unassign will be done.

Let us know how you get on with that.

Thanks Adam,  we ended up changing our approach and handling this via the workflow and our integration platform.

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,198 views 13 19
Join discussion

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot