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

This widget could not be displayed.

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)

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

<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.

"

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 ?

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

Yes, scripts won't be empty.

@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.

@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 

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

91 views 1 0
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