Set Affects Version/s on subtasks if changed on parent task using Groovy script

Alex Christensen Community Champion Nov 19, 2015

In JIRA 6.4.6, I am trying to create a script listener using Script Runner using an inline script. Basically, if the Affects Version/s is updated on a parent issue, I want to update all of its subtasks with those same values in Affects Version/s, as well.

This is the script that I have so far. It works as is, but I don't know how to actually set the Affects Version/s field value on the subtasks.

import com.atlassian.jira.component.ComponentAccessor

def affectsVersions = issue.getAffectedVersions()
def commentManager = ComponentAccessor.getCommentManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getUser()
def subtasks = issue.getSubTaskObjects()

subtasks.each {
    commentManager.create(
        it,
        user,
        "New Affects Version/s:  " + affectsVersions,
        false)
    //works correctly up to here. don't know how to set Affects Version/s field value
}

The comment I create shows up on all of the subtasks correctly. What method(s) should I use to set the field value? I'm still fairly new to groovy scripting and the JIRA API documentation and I'm failing to locate the methods I need.

Thanks for your help!

1 answer

I recently did that for FixVersion, but using a groovy class file because my ScriptRunner/JIRA doesn't let me do inline custom listeners.  I modified the code from here, to give credit where it is due:

http://stackoverflow.com/questions/18428756/update-jira-subtask-fix-version-when-parent-issue-fix-version-updated

I had to modernize it a little, plus fixed it so it would null out values:

package com.myorg.listeners

import org.apache.log4j.Category
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.project.version.Version
import com.atlassian.jira.config.SubTaskManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.type.EventDispatchOption
import java.util.ArrayList
import java.util.Collection


class SyncParentFixVersionToSubTask extends AbstractIssueEventListener 
{
	Category log = Category.getInstance(SyncParentFixVersionToSubTask.class);
	SubTaskManager subTaskManager = ComponentManager.getInstance().getSubTaskManager();

	@Override
	void workflowEvent(IssueEvent event)
	{
		try
		{
			Issue eventIssue = event.getIssue();
			if ( !eventIssue.getIssueTypeObject().isSubTask() )
			{
				List changeItems = event.getChangeLog().getRelated("ChildChangeItem");
				if( changeItems.any {it.get('field')=='Fix Version'} )
				{
					Collection<Version> fixVersions = new ArrayList<Version>();
					fixVersions = eventIssue.getFixVersions();
					Collection<Issue> subTasks = eventIssue.getSubTaskObjects();

					if (subTaskManager.subTasksEnabled && !subTasks.isEmpty())
					{
						IssueManager issueManager = ComponentManager.getInstance().getIssueManager();
						Collection<Issue> _subTasks = eventIssue.getSubTaskObjects();
						_subTasks.each 
						{
							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 SyncParentFixVersionToSubTask"
			log.debug (ex.getMessage())
		}
	}
}

 

 

@Jeremy Gaudet Thanks for the above script but looks like there are changes to methods in JIRA 7.1.8. We are using 7.1.8 and I am not able to make the following code work:

What I am trying to do is pretty much the same what your script above does - Update the fixVersion on all the subtasks when the fixVersion on the parent changes.

 

it.setFixVersions(fixVersions);
issueManager.updateIssue(event.getUser(), it, EventDispatchOption.ISSUE_UPDATED, false);
It gives the following errors: 
Screen Shot 2016-08-26 at 3.24.05 PM.png
Screen Shot 2016-08-26 at 3.24.46 PM.png
I am new to groovy scripting so where can I find updated methods.
Thanks
Rishi Goel

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Sunday in Agility

You asked for it, so we delivered: images on issues have arrived

A picture tells a thousand words. And agility boards have just released their latest feature: cover images on issues – so now your board can tell a story at first glance. Upload attachmen...

170 views 1 10
Read article

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