It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

ScriptRunner Custom Listener Script errors after upgrade of ScriptRunner from 3.1.4 to 5.1.6.2 Edited

Could you please take a look if you have any suggestions? Thank you

Script updates FixVersions from parent story to its' subtasks.

After upgrade 2 lines give errors:

1. if(fix_Issue.fixVersions?.name && changeItems.any {it.get('field')=='Fix Version'} )

Looks like this part is having problem: fix_Issue.fixVersions?.name

 

2.  it.setFixVersions(fixVersions)issueManager.updateIssue(event.getUser(), it, EventDispatchOption.ISSUE_UPDATED, false)

 

Here is whole script. It works fine in Jira 6.4.12 with script runner 3.1.4, but not in Jira 7 with script runner 5.1.6.2

package listeners

import org.apache.log4j.Category
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
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.issueType.name == "Story" )
{
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.getSubTaskObjects();
SubTaskManager subTaskManager = ComponentAccessor.getComponent(SubTaskManager);
if (subTaskManager.subTasksEnabled && !subTasks.empty)
{
IssueManager issueManager = ComponentAccessor.getComponent(IssueManager)
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())
}
}
}

2 answers

1 accepted

0 votes
Answer accepted

Try

if(fix_Issue.fixVersions?.name && changeItems.any {it.field=='Fix Version'} )

and looks like your second line is missing a carriage return

it.setFixVersions(fixVersions)
issueManager.updateIssue(event.getUser(), it, EventDispatchOption.ISSUE_UPDATED, false)
 

Also, I don't see any re-indexing of your sub-tasks ... that will be a problem for searching. 

Actual working answer is at the bottom of the thread. 

Thank you for help, Peter-Dave

Thank you very much for suggestions. Unfortunately, errors did not go away. Please see screenshots of lines with errors. Errors are the same even after I implemented changes that you suggested.  Please take a look if you have any suggestions. Thank you

 

Error1.png

Newer versions of scriptrunner include a static type checker.

These errors just indicate that there "MIGHT" be errors in your code. But since groovy supports dynamic typing, this code should still run fine.

If you want to remove all static type checking errors you will have to strongly type all your variables (in effect, never use def) and include all the classes in your imports.  But that will make your code significantly more complicated.

Just try to run the script and test with a few cases that exercise all the code logic branches. 

Thank you for update. Unfortunately, with presence of only 3 static error, scrip stopped working in a new version of ScriptRunner.

 

This is what I did:

1. Original script ran fine with Original_Screenshot.pngold version of Jira/ScriptRunner

There were outdated syntax that I underlined in yellow. I made change as follows:

ReducedErrors_Screenshot.pngErrors went away. Only 3 static errors on lines 29, 42 and 43 stated.

And script does not work in it's original or changed version. 

I'm attaching scripts below. 

Please take a look if any suggestions.

Thank you in advance 

Original Script that worked fine in old version of ScriptRunner:

 

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.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 == "Story" )
{
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())
}
}
}

Edited script. Outdated syntax is replaced with new. It still does not work with new versions of Jira/ScriptRunner:

 

package listeners

import org.apache.log4j.Category
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
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.issueType.name == "Story" )
{
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.getSubTaskObjects();
SubTaskManager subTaskManager = ComponentAccessor.getComponent(SubTaskManager);
if (subTaskManager.subTasksEnabled && !subTasks.empty)
{
IssueManager issueManager = ComponentAccessor.getComponent(IssueManager)
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())
}
}
}

It would help if you could include the errors from the log.

Also, I noticed this link on the Adaptavist documentation page.

So perhaps converting your class to a simple script would fix your issues.

Try this:

import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.util.ImportUtils

def issueManager = ComponentAccessor.issueManager
def indexingService = ComponentAccessor.getComponent(IssueIndexingService.class)

def fixIssue = event.issue

if(fixIssue.issueType.name == "Story"){
   def changeItems = event.changeLog.getRelated("ChildChangeItem")
   if(fixIssue.fixVersions?.name && changeItems.any{it.field == 'Fix Version'}){
      fixIssue.subTaskObjects.each { subTask ->
         subTask.fixVersions = fixIssue.fixVersions
         issueManager.updateIssue(event.user, subTask,  EventDispatchOption.ISSUE_UPDATED, false)
         //Put JIRA in indexing mode
         def wasIndexing = ImportUtils.isIndexIssues()
         ImportUtils.setIndexIssues(true)
         //update the JIRA index
         indexingService.reIndex(subTask)
         ImportUtils.setIndexIssues(wasIndexing)
      }
   }
}

 

I added indexing. Your original script was not indexing the subtask. This means that the subtask would not show up in searches based on fix version.

Thank you very much for additions and for fix. This is working. 

For some reason script gives following error on some runs. Though functionality is working fine. Not sure if this should be just ignore. Thank you 

Script:

import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.util.ImportUtils

def issueManager = ComponentAccessor.issueManager
def indexingService = ComponentAccessor.getComponent(IssueIndexingService.class)

def fixIssue = event.issue

if(fixIssue.issueType.name == "Story"){
   def changeItems = event.changeLog.getRelated("ChildChangeItem")
   if(fixIssue.fixVersions?.name && changeItems.any{it.field == 'Fix Version'}){
      fixIssue.subTaskObjects.each { subTask ->
         subTask.fixVersions = fixIssue.fixVersions
         issueManager.updateIssue(event.user, subTask,  EventDispatchOption.ISSUE_UPDATED, false)
         //Put JIRA in indexing mode
         def wasIndexing = ImportUtils.isIndexIssues()
         ImportUtils.setIndexIssues(true)
         //update the JIRA index
         indexingService.reIndex(subTask)
         ImportUtils.setIndexIssues(wasIndexing)
      }
   }
}

Error:

Time (on server): Thu May 09 2019 11:05:55 GMT-0400 (Eastern Daylight Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2019-05-09 11:05:55,572 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2019-05-09 11:05:55,572 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script>
java.lang.NullPointerException: Cannot invoke method getRelated() on null object
 at Script7.run(Script7.groovy:12)

That's probably when the event generated doesn't include a changeLog.

You can either try the null-safe operator

def changeItems = event.changeLog?.getRelated("ChildChangeItem")

Or wrap the whole thing in an if block

if(event.changeLog){
def changeItems
= event.changeLog.getRelated("ChildChangeItem")
...
}

Thank you, appreciate all your help, this helped

def changeItems = event.changeLog?.getRelated("ChildChangeItem")

Suggest an answer

Log in or Sign up to answer
Community showcase
Asked in Opsgenie

Upcoming AMA with Serhat Can on everything Opsgenie

Hi there Community!  My name is Serhat Can and I’m a Technical Evangelist for Atlassian with a focus on Opsgenie, as well as an AWS Hero and DevOps Days Core Organizer.  I began my care...

215 views 5 4
View question

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you