ScriptRunner script stopped working

Gabriel MOSCALU November 3, 2017

Hi,

 

I have created a little while a go a scripted field to compute the % of month duration a certain issue has lasted (with the end purpose of computing the month availability based on incident fields). It used to work, now it stops for some reason and I cannot understand why.

 

Old log:

2017-05-23 18:19:49,818 WARN [customfield.GroovyCustomField]: Script field run start....
2017-05-23 18:19:49,818 WARN [customfield.GroovyCustomField]: Runing on issue 50616/SAMSK-304....
2017-05-23 18:19:49,818 WARN [customfield.GroovyCustomField]: Issue created on 2017-04-27 09:17:21.22
2017-05-23 18:19:49,819 WARN [customfield.GroovyCustomField]: Actual outage duration = 366.0
2017-05-23 18:19:49,819 WARN [customfield.GroovyCustomField]: Year = 2017
2017-05-23 18:19:49,820 WARN [customfield.GroovyCustomField]: Month = 04
2017-05-23 18:19:49,820 WARN [customfield.GroovyCustomField]: m days = 30
2017-05-23 18:19:49,820 WARN [customfield.GroovyCustomField]: month length = 43200
2017-05-23 18:19:49,820 WARN [customfield.GroovyCustomField]: outage impact = 0.8472222222222223

Current log:

2017-11-03 20:40:22,441 WARN [customfield.GroovyCustomField]: Script field run start.... 
2017-11-03 20:40:22,441 WARN [customfield.GroovyCustomField]: Runing on issue 50616/SAMSK-304....
2017-11-03 20:40:22,441 WARN [customfield.GroovyCustomField]: Issue created on 2017-04-27 09:17:21.22
2017-11-03 20:40:22,443 WARN [customfield.GroovyCustomField]: Actual outage duration = 366.0
2017-11-03 20:40:22,443 WARN [customfield.GroovyCustomField]: Year = 2017.0
2017-11-03 20:40:22,443 WARN [customfield.GroovyCustomField]: Month = 4.0

 

Script

import com.atlassian.jira.issue.*;
import com.atlassian.jira.component.ComponentAccessor;
import java.sql.Timestamp;

log.warn ("Script field run start....")

// Get the current issue key
Issue issueKey = issue;

//get the current issue ID
def id=issueKey.getId();

log.warn ("Runing on issue "+ id + "/" + issue.getKey() +"....")
def createTime = issueKey.getCreated();
log.warn ("Issue created on "+ createTime.toString() )

// Get access to the Custom Field Manager
def customFieldManager = ComponentAccessor.getCustomFieldManager();

// Get the required custom field values, set to zero if null
def outageLength = getCustomFieldValue("Actual outage duration")?:0 as double;
log.warn ("Actual outage duration = "+ outageLength.toString() )

//convert the ticket's created date to desired YEAR and MONTH formats, to detect the leap years
def CreateYear = issueKey.getCreated().format('yyyy') as double;
log.warn ("Year = "+ CreateYear.toString() )
def CreateMonth = issueKey.getCreated().format('MM') as double;
log.warn ("Month = "+ CreateMonth.toString() )

//shall we use "Actual issue start" CF value instead of the ticket creation date ???
//def CreateYear = getCustomFieldValue("Actual issue start time")?:issueKey.getCreated().format('yyyy');
//def CreateMonth = getCustomFieldValue("Actual issue start time")?:issueKey.getCreated().format('MM');

def mdays=0 as double

switch (CreateMonth){
case "01": mdays=31
break
case "02":
//source: https://ro.wikipedia.org/wiki/An_bisect - Leap year detection
if (CreateYear % 4 != 0) {
//regular year
mdays=28
} else if (CreateYear % 100 != 0) {
//leap year
mdays=29
} else if (CreateYear % 400 != 0) {
//regular year
mdays=28
} else {
//leap year
mdays=29
}

break
case "03": mdays=31
break
case "04": mdays=30
break
case "05": mdays=31
break
case "06": mdays=30
break
case "07": mdays=31
break
case "08": mdays=31
break
case "09": mdays=30
break
case "10": mdays=31
break
case "11": mdays=30
break
case "12": mdays=31
break
default:
return null
}
log.warn ("m days = "+ mdays.toString() )

def mlength = mdays*24*60 as double;
log.warn ("month length = "+ mlength.toString() )

def outageImpact = outageLength / mlength * 100 as double;
log.warn ("outage impact = "+ outageImpact.toString() )


//push back computed value
return outageImpact

 

In the scripted field editing page, I have a "Cannot find matching method java.lang.Object#div(double) @ line 84, column 33 " warning for the line:

def outageImpact = outageLength / mlength * 100 as double;

but obviously (see log above), the execution stops way before that...

 

Does anyone have any hints about what may have happened and how to make this work again?

 

in the meanwhile, the only thing that changed is the ScriptRunner plugin upgrade

1 answer

1 accepted

0 votes
Answer accepted
Gonchik Tsymzhitov
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 4, 2017

 

Dear Gabriel,

Please, double converter in your expression like this:



def outageImpact = (double) outageLength/mlength*100 as double;

 

Cheers,

Gonchik Tsymzhitov

Gabriel MOSCALU November 5, 2017

Hi Gonchik,

 

I tried your solution, adding the "(double)" declaration, yet the log shows only:

 

2017-11-06 08:39:28,594 WARN [customfield.GroovyCustomField]: Runing on issue 50616/SAMSK-304.... 
2017-11-06 08:39:28,594 WARN [customfield.GroovyCustomField]: Issue created on 2017-04-27 09:17:21.22
2017-11-06 08:39:28,596 WARN [customfield.GroovyCustomField]: Actual outage duration = 366.0
2017-11-06 08:39:28,596 WARN [customfield.GroovyCustomField]: Year = 2017.0
2017-11-06 08:39:28,596 WARN [customfield.GroovyCustomField]: Month = 4.0

 

Still not processing the loop to detect leap year (as per log) 

 

I added the same "(double)" in the 

def mdays=0 as double

line, and resulted in an error:

 

2017-11-06 08:35:15,504 ERROR [customfield.GroovyCustomField]: *************************************************************************************
Script field failed on issue: TELSER-9, field: Outage percentage
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script41.groovy: 34: unexpected token: double @ line 34, column 6.
   def (double) mdays=0 as double;
        ^
1 error

 

any ideas?

 

Thanks

Gabriel

Gabriel MOSCALU November 12, 2017

Apart from casting the double type, in the above script there's been another "error": I was testing a double value against strings, causing the script to always exit through the "Default: case value, meaning "null".

 

now the script looks like this and it works fine:

 

import com.atlassian.jira.issue.*;
import com.atlassian.jira.component.ComponentAccessor;
import java.sql.Timestamp;

log.info ("OutagePercentage Script field run start....")

// Get the current issue key
Issue issueKey = issue;

//get the current issue ID
def id=issueKey.getId();

log.info ("Runing on issue "+ id + "/" + issue.getKey() +"....")
def createTime = issueKey.getCreated();
log.debug ("Issue created on "+ createTime.toString() )

// Get access to the Custom Field Manager
def customFieldManager = ComponentAccessor.getCustomFieldManager();

// Get the required custom field values, set to zero if null
def outageLength = getCustomFieldValue("Actual outage duration")?:0 as double;
log.debug ("Actual outage duration = "+ outageLength.toString() )

//convert the ticket's created date to desired YEAR and MONTH formats, to detect the leap years
def CreateYear = issueKey.getCreated().format('yyyy') as double;
log.debug ("Year = "+ CreateYear.toString() )
def CreateMonth = issueKey.getCreated().format('MM') as double;
log.debug ("Month = "+ CreateMonth.toString() )

//shall we use "Actual issue start" CF value instead of the ticket creation date ???
//def CreateYear = getCustomFieldValue("Actual issue start time")?:issueKey.getCreated().format('yyyy');
//def CreateMonth = getCustomFieldValue("Actual issue start time")?:issueKey.getCreated().format('MM');

def mdays=0 as double;
log.debug ("Detecting leap year...")

switch (CreateMonth){
case 1: mdays=31; break
case 2:
//source: https://ro.wikipedia.org/wiki/An_bisect - Leap year detection
if (CreateYear % 4 != 0) {
//regular year
mdays=28
} else if (CreateYear % 100 != 0) {
//leap year
mdays=29
} else if (CreateYear % 400 != 0) {
//regular year
mdays=28
} else {
//leap year
mdays=29
}
break
case 3: mdays=31; break
case 4: mdays=30 ; break
case 5: mdays=31; break
case 6: mdays=30; break
case 7: mdays=31 ; break
case 8: mdays=31 ; break
case 9: mdays=30 ; break
case 10: mdays=31; break
case 11: mdays=30; break
case 12: mdays=31; break
default:
log.warn ("OutagePercentage: Cannot detect leap year")
return null
}
log.debug ("m days = "+ mdays.toString() )

def mlength = mdays*24*60 as double;
log.debug ("month length = "+ mlength.toString() )

def outageImpact = (double) Math.round( (double) outageLength / mlength * 1000000)/10000 as double;
log.info ("outage impact = "+ outageImpact.toString() )


//push back computed value
return outageImpact

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events