Custom update listener to set subtask's fix-version

Martin Hanus May 30, 2017

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

 

2 answers

1 accepted

3 votes
Answer accepted
Martin Hanus June 2, 2017

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

Srikanth M November 8, 2017

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)
Aisha M April 24, 2018

@Srikanth M Can you please help me on where you placed groovy file in order for this script to work ? 

Srikanth M April 24, 2018

<jira home>\scripts\com\custom\listeners\<listener script.groovy>

 

The vendor's site says:

"

The easiest, and recommended way, is to just write a script. You can use an inline script, or point to a file in your script roots, as usual.

"

Aisha M April 25, 2018

Thank you so much for your reply :) I have zero experience with this. Also, I have been assuming that we need to simply place an empty groovy file in that location, or do we need to write in some script in the file too ?

Srikanth M April 25, 2018

I think you can start reading ScriptRunner's documentation for better understanding of what scripts can do.

Yes, scripts won't be empty.

Aisha M April 26, 2018

@Srikanth M

Thank you.Will go through. Can I get an example of what the contents of the groovy file, if you happen to have one. Read somewhere, the above same code is added to the groovy file.

Aisha M May 24, 2018

@Martin Hanus Hello, thank you so much your script works fantastic. Just a help, how to modify the script so that the subtask acquires the Fix Version of the Parent when it is created ? Thanks 

Pete P
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.
February 7, 2019

@Martin Hanus

I have tried the abbreviated version and it is not working.  I am using it on the following events.  Any thoughts on what I am missing ?

Events: FixVersionCreatedInline, VersionCreateEvent, VersionUpdatedEvent
Pete P
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.
February 7, 2019

My bad, I changed it to Issue updated event and it is working perfectly.   Thanks so much @Martin Hanusfor taking the time to figure this one out.  

Cédric Pignon March 26, 2019

thanks @Martin Hanus to share it works perfectly for me as well.

Teemu Niemi April 29, 2019

@Martin Hanus How can I edit this code, so it activates only when specific field is updated? This should be activated only when Fix Version/s -field is updated, but I just can't get my idea to work.

0 votes
Casey Maynard May 13, 2019

It is not clear what I need to do to implement this as an option.  I have a similar question here >>> https://community.atlassian.com/t5/Jira-questions/How-can-I-autopopulate-the-Due-field-with-the-Fix-Version/qaq-p/1062121

Suggest an answer

Log in or Sign up to answer