Custom update listener to set subtask's fix-version Edited

Hi all,

I'm developing custom listener which will update subtask's fix version to same value as it's parent issue. 

Currently we are using post-function in workflow in order to set subtask's fix version according to parent on subtask creation. This however doesn't cover cases when subtask already exists and parent's fix version gets updated. New value from parent task is not propagated to subtask.

I'm using script runner and I'm creating 'Custom lisatener', for my specific project and specified Event: 'Issue Updated'. I added script as following:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.SubTaskManager
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.project.version.Version
import org.apache.log4j.Logger

class CopyFixVersionFromParentToChild extends AbstractIssueEventListener {
    Logger log = Logger.getLogger(CopyFixVersionFromParentToChild.class);
    SubTaskManager subTaskManager = ComponentAccessor.getComponent(SubTaskManager.class)
    IssueManager issueManager = ComponentAccessor.getComponent(IssueManager.class)

    @Override
    void issueUpdated(IssueEvent event) {
        log.warn("\nIssue updated!!!\n")
        try {
            Issue updatedIssue = event.getIssue()
            if (updatedIssue.issueTypeObject.name == "Parent issue type") {
                Collection<Version> fixVersions = new ArrayList<Version>()
                fixVersions = updatedIssue.getFixVersions()
                Collection<Issue> subTasks = updatedIssue.getSubTaskObjects()
                if (subTaskManager.subTasksEnabled && !subTasks.empty) {
                    subTasks.each {
                        if (it instanceof MutableIssue) {
                            ((MutableIssue) it).setFixVersions(fixVersions)
                            issueManager.updateIssue(event.getUser(), it, EventDispatchOption.ISSUE_UPDATED, false)
                        }
                    }
                }
            }
        } catch (ex) {
            log.debug "Event: ${event.getEventTypeId()} fired for ${event.issue} and caught by script 'CopyVersionFromParentToChild'"
            log.debug(ex.getMessage())
        }
    }
}

Problem is, that it doesn't work. I'm not sure whethe rit's problem that my script logic is encapsulated inside class. Do I have to register this in some specific way? Or am I using script runner completely wrong and I'm pasting this script to wrong section? I checked code against JIRA API and it looks like it should work, my IDE doesnt show any warnings/errors.

Also, could anyone give me hints on where to find logging output from custom scripts like this? Whatever message I put into logger, I seem to be unable to find anywhere in JIRA logs (although I'm aware that script might not work for now).

Any response is much appreciated guys, Thanks.

Martin

 

1 answer

1 accepted

Well, I figure it out.


Method I posted, which implements listener as groovy class is used in different way than I expected. These kind of script files were used to be located in to specific path in JIRA installation and ScriptRunner would register them into JIRA as listeners.
In in order to create 'simple' listener script which reacts to issue updated event, I had to strip it down to this code

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.project.version.Version

IssueManager issueManager = ComponentAccessor.getComponent(IssueManager.class)

Issue updatedIssue = event.getIssue()
Collection<Version> fixVersions = new ArrayList<Version>()
fixVersions = updatedIssue.getFixVersions()
Collection<Issue> subTasks = updatedIssue.getSubTaskObjects()
subTasks.each {
    if (it instanceof MutableIssue) {
        ((MutableIssue) it).setFixVersions(fixVersions)
        issueManager.updateIssue(event.getUser(), it, EventDispatchOption.ISSUE_UPDATED, false)
    }
}


You past this to script runner interface as custom listener and it works :-). Hope this helps anyone who's learning ScriptRunner. Cheers.

Matthew

Thanks, it helped me after moving to jira 7.

Though I didn't get while you have toc heck for instance of in the below line:

it instanceof MutableIssue) {
        ((MutableIssue) it).setFixVersions(fixVersions)

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,103 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