Script Runner: sync fix version from parent to sub-tasks

We are using Script Runner 2.1.17 with JIRA 6.2.

I tried to deploy 2 different scripts for custom listener to sync fix version value from parent to sub-tasks.

Both Scrip Runner custom listeners got deployed successfully.  

But fix version does not get populated to sub-task on any events that happening. 

 

this is one script that I used:

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.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.workflow.WorkflowManager;
import com.atlassian.jira.workflow.JiraWorkflow;
import java.util.ArrayList;
import java.util.Collection;

class SyncParentFixVerifiedVersionsToSubTask extends AbstractIssueEventListener
{
Category log = Category.getInstance("com.onresolve.jira.groovy");
SubTaskManager subTaskManager = ComponentManager.getInstance().getSubTaskManager();
WorkflowManager workflowManager = ComponentManager.getInstance().getWorkflowManager();
IssueManager issueManager = ComponentManager.getInstance().getIssueManager();
CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager();

Boolean changed = false;
Long workflowId;

@Override
void workflowEvent(IssueEvent event) {
try {
if (subTaskManager.isSubTasksEnabled()) {
Issue eventIssue = event.getIssue();
JiraWorkflow workflow = workflowManager.getWorkflow(eventIssue);
if (workflow.getName() == "Put Workflow Name Here") {
CustomField vvcf = customFieldManager.getCustomFieldObjectByName("Verified Version/s");
if ( !eventIssue.getIssueTypeObject().isSubTask() ) {
// Change is on a potential parent, sync to sub-tasks if there are any
Collection<Issue> subTasks = eventIssue.getSubTaskObjects();
List changeItems = event.getChangeLog().getRelated("ChildChangeItem");

// Sync FixVersion/s
if( changeItems.any {it.get('field')=='Fix Version'} ) {
changed = true;
// Collection<Version> fixVersions = new ArrayList<Version>();
// fixVersions = eventIssue.getFixVersions();
Collection<Version> fixVersions = eventIssue.getFixVersions();
if (!subTasks.isEmpty()) {
subTasks.each {
it.setFixVersions(fixVersions);
}
}
}

// Sync Verified Version/s
if( changeItems.any {it.get('field')=='Verified Version/s'} ) {
changed = true;
Collection<Version> verifiedVersions = new ArrayList<Version>();
verifiedVersions = eventIssue.getCustomFieldValue(vvcf);
if (!subTasks.isEmpty()) {
subTasks.each {
it.setCustomFieldValue(vvcf,verifiedVersions);
}
}
}

if (changed) {
subTasks.each {
issueManager.updateIssue(event.getUser(), it, EventDispatchOption.ISSUE_UPDATED, false);
}
}
} else {
// Change is on a sub-task, sync from the parent if they are out of sync
Issue parentIssue = eventIssue.getParentObject();

// Sync "Fix Version/s"
Collection<Version> parentFixVersions = parentIssue.getFixVersions();
if (parentFixVersions == null) parentFixVersions = new ArrayList<Version>();
Collection<Version> fixVersions = eventIssue.getFixVersions();
if (fixVersions == null) fixVersions = new ArrayList<Version>();
if(!(parentFixVersions.containsAll(fixVersions) && fixVersions.containsAll(parentFixVersions))) {
eventIssue.setFixVersions(parentFixVersions);
changed = true;
}

// Sync "Verified Version/s"
Collection<Version> parentVerifiedVersions = parentIssue.getCustomFieldValue(vvcf);
if (parentVerifiedVersions == null) parentVerifiedVersions = new ArrayList<Version>();
Collection<Version> verifiedVersions = eventIssue.getCustomFieldValue(vvcf);
if (verifiedVersions == null) verifiedVersions = new ArrayList<Version>();
if(!(parentVerifiedVersions.containsAll(verifiedVersions) && verifiedVersions.containsAll(parentVerifiedVersions))) {
eventIssue.setCustomFieldValue(vvcf,parentVerifiedVersions);
changed = true;
}
if (changed) {
issueManager.updateIssue(event.getUser(), eventIssue, EventDispatchOption.ISSUE_UPDATED, false);
}
}
}
}
}
catch (ex) {
log.debug "Event: ${event.getEventTypeId()} fired for ${event.issue} and caught by SyncParentFixVerifiedVersionsToSubTask"
log.debug (ex.getMessage())
}
}
}

 

This is second script that I tried:

 

package com.custom.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.opensymphony.workflow.InvalidInputException
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 FixVersionPBI_To_SBI_1 extends AbstractIssueEventListener
{

Category log = Category.getInstance(FixVersionPBI_To_SBI_1.class)

@Override
void issueUpdated(IssueEvent event)
{

try
{
Issue fix_Issue = event.getIssue()
if (fix_Issue.issueTypeObject.name == "Parent issue type" )
{
List changeItems = event.getChangeLog().getRelated("ChildChangeItem")
if( fix_Issue.fixVersions?.name && changeItems.any {it.get('field')=='Fix Version'} )
{
Collection<Version> fixVersions = new ArrayList<Version>();
fixVersions = fix_Issue.getFixVersions()
Collection subTasks = fix_Issue.getSubTasks();
SubTaskManager subTaskManager = ComponentManager.getInstance().getSubTaskManager();

if (subTaskManager.subTasksEnabled && !subTasks.empty)
{
IssueManager issueManager = ComponentManager.getInstance().getIssueManager()
Collection _subTasks = fix_Issue.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 FixVersionPBI_To_SBI_1"
log.debug (ex.getMessage())
}
}
}

 

I check log file for at a time when I updated fix version on story and did not see any changes on sub-task, and this is what I found:

 

ERROR testuser 982x2320x1 1obr72j   /secure/AjaxIssueAction.jspa [jirafisheyeplugin.perforce.listener.JobUpdateListener] Could not commit job: Job: PTINFRA-1076
User: testuser
Description:
test fix version for log 4:20 pm

com.perforce.api.CommitException
at com.perforce.api.Job.commit(Job.java:365)
at com.atlassian.jirafisheyeplugin.perforce.listener.JobUpdateListener.createJob(JobUpdateListener.java:42)
at com.atlassian.jirafisheyeplugin.perforce.listener.JobUpdateListener.workflowEvent(JobUpdateListener.java:30)
at com.atlassian.jira.event.issue.IssueEventListenerHandler$IssueEventInvoker.invoke(IssueEventListenerHandler.java:51)
at com.atlassian.event.internal.AsynchronousAbleEventDispatcher$2.run(AsynchronousAbleEventDispatcher.java:66)
at com.atlassian.event.internal.AsynchronousAbleEventDispatcher$1.execute(AsynchronousAbleEventDispatcher.java:32)
at com.atlassian.event.internal.AsynchronousAbleEventDispatcher.dispatch(AsynchronousAbleEventDispatcher.java:60)
at com.atlassian.event.internal.EventPublisherImpl.invokeListeners(EventPublisherImpl.java:160)
at com.atlassian.event.internal.EventPublisherImpl.publish(EventPublisherImpl.java:79)
at com.atlassian.jira.event.issue.DefaultIssueEventManager.publishEvent(DefaultIssueEventManager.java:124)
at com.atlassian.jira.event.issue.DefaultIssueEventManager.dispatchEvent(DefaultIssueEventManager.java:102)
at com.atlassian.jira.issue.util.DefaultIssueUpdater.storeModifiedFields(DefaultIssueUpdater.java:95)
at com.atlassian.jira.issue.util.DefaultIssueUpdater.doUpdate(DefaultIssueUpdater.java:49)
at com.atlassian.jira.issue.managers.DefaultIssueManager.doUpdate(DefaultIssueManager.java:782)
at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:768)
at com.atlassian.jira.bc.issue.DefaultIssueService.update(DefaultIssueService.java:273)
at com.atlassian.jira.bc.issue.DefaultIssueService.update(DefaultIssueService.java:247)
at sun.reflect.GeneratedMethodAccessor1253.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)

 

Please let me know if anyone have any suggestions for me. 

 

Thank you 

for JIRA: com.onresolve.jira.groovy.groovyrunner

1 answer

Following listener script works fine with Script Runner 2.1.17 and JIRA 6.2 for all events, but not for sub-task creation. Any suggestions? Thank you 

package 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())
}
}
}

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Sep 25, 2018 in Jira

Atlassian Research Workshop opportunity on Sep. 28th in Austin, TX

We're looking for participants for a workshop at Atlassian! We need Jira admins who have interesting custom workflows, issue views, or boards. Think you have a story to sha...

469 views 7 5
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