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

Blake Jones May 30, 2017

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

1 vote
adammarkham
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.
June 6, 2017

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.

Blake Jones June 19, 2017

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

Suggest an answer

Log in or Sign up to answer