Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,463,474
Community Members
 
Community Events
176
Community Groups

A Groovy condition to compare a date custom field with the date attribute of an Insight object?

Hi all

 

Once again I turn to the community to see if the nightmare that is Groovy scripting has a solution for this requirement :D

 

Since there's no JQL issue functions that can compare a date field with a date attribute of an Insight object, I'm wondering if anyone has anything that will accomplish this.

 

I'm looking to have a condition on two transitions out of our CAB Approval status, one to the Awaiting Implementation status and another to the Change Freeze Approval status.

 

Based on Scheduled Start custom field being < Change Freeze Start Insight object attribute or > Change Freeze End Insight attribute.

 

Any ideas? thanks

1 answer

0 votes

One person's nightmare....

 

Here I just whipped this up (took about 15 minutes)

import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade
import com.riadalabs.jira.plugins.insight.services.model.ObjectBean

@WithPlugin('com.riadalabs.jira.plugins.insight')
@PluginModule ObjectFacade objectFacade

def dateFieldName = 'Scheduled Start'
def insightFieldName = 'unknown'
def startAttribute = 'Change Freeze Start'
def endAttribute = 'Change Freeze End'


def dateValue = cfValues[dateFieldName] as Date
def insightObjValue = cfValues[insightFieldName] as List<ObjectBean>


if(!dateValue || !insightObjValue){
return //missing some info to compare
}

//insight custom field always return an array even if they only allow single value. Assume there is only one
insightObjValue = insightObjValue.first()
def freezeStartAttr = objectFacade.loadObjectAttributeBean(insightObjValue.id, startAttribute)
def freezeEndAttr = objectFacade.loadObjectAttributeBean(insightObjValue.id, endAttribute)
def freezeStart = freezeStartAttr?.objectAttributeValueBeans && freezeStartAttr.objectAttributeValueBeans[0].dateValue
def freezeEnd = freezeEndAttr?.objectAttributeValueBeans && freezeEndAttr.objectAttributeValueBeans[0].dateValue

if(dateValue < freezeStart){
return false
}
if(dateValue > freezeEnd){
return false
}
return true

Other than adjusting the last 7 lines according to your actual requirement, this should get you close.

Awesome thanks, will tweak and give it some testing Tuesday :)

Testing results, seems to have a problem with the cfvalue

import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade
import com.riadalabs.jira.plugins.insight.services.model.ObjectBean

@WithPlugin('com.riadalabs.jira.plugins.insight')
@PluginModule ObjectFacade objectFacade

def dateFieldName = 'Scheduled Start'
def insightFieldName = 'Change Freeze Status'
def startAttribute = 'Change Freeze Start'
def endAttribute = 'Change Freeze End'


def dateValue = cfValues[dateFieldName] as Date
def insightObjValue = cfValues[insightFieldName] as List<ObjectBean>


if(!dateValue || !insightObjValue){
return //missing some info to compare
}

//insight custom field always return an array even if they only allow single value. Assume there is only one
insightObjValue = insightObjValue.first()
def freezeStartAttr = objectFacade.loadObjectAttributeBean(insightObjValue.id, startAttribute)
def freezeEndAttr = objectFacade.loadObjectAttributeBean(insightObjValue.id, endAttribute)
def freezeStart = freezeStartAttr?.objectAttributeValueBeans && freezeStartAttr.objectAttributeValueBeans[0].dateValue
def freezeEnd = freezeEndAttr?.objectAttributeValueBeans && freezeEndAttr.objectAttributeValueBeans[0].dateValue

if(dateValue < freezeStart){
return false
}
if(dateValue > freezeEnd){
return false
}
return true

 

There was an error during the execution of your script against issue CIT-836

Message:
groovy.lang.MissingPropertyException: No such property: cfValues for class: script_e508d908ba07d40a9f5832eac77c6894
Stack:
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:65)
org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:51)
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:309)
script_e508d908ba07d40a9f5832eac77c6894.run(script_e508d908ba07d40a9f5832eac77c6894.groovy:15)

 

The Scheduled start definitely has a value, but it may be because it's a Date time picker field, it would be nice if it could actually work with a Datetime Insight value and a Date Time field as I know these CAB guys have things they'd want to kick in at say 8pm and end at 7am.

 

Additionally, could you add a clause for the Scheduled End field and the scheduled end not being during the change freeze too?

 

Thanks

You have to use "simple scripted condition" to be able to leverage the cfValues.

Here is the same script with both the start and end date.

import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade
import com.riadalabs.jira.plugins.insight.services.model.ObjectBean

@WithPlugin('com.riadalabs.jira.plugins.insight')
@PluginModule ObjectFacade objectFacade

def startDateName = 'Scheduled Start'
def endDateName = 'Scheduled End'
def insightFieldName = 'Change Freeze Status'
def startAttribute = 'Change Freeze Start'
def endAttribute = 'Change Freeze End'


def startDateValue = cfValues[startDateName] as Date
def endDateValue = cfValues[endDateName] as Date
def insightObjValue = cfValues[insightFieldName] as List<ObjectBean>


if(!startDateValue || !endDateValue || !insightObjValue){
return false//missing some info to compare
}

//insight custom field always return an array even if they only allow single value. Assume there is only one
insightObjValue = insightObjValue.first()
def freezeStartAttr = objectFacade.loadObjectAttributeBean(insightObjValue.id, startAttribute)
def freezeEndAttr = objectFacade.loadObjectAttributeBean(insightObjValue.id, endAttribute)
def freezeStart = freezeStartAttr?.objectAttributeValueBeans && freezeStartAttr.objectAttributeValueBeans[0].dateValue
def freezeEnd = freezeEndAttr?.objectAttributeValueBeans && freezeEndAttr.objectAttributeValueBeans[0].dateValue

if(startDateValue <= freezeStart || startDateValue >= freezeEnd){
//start date is outside freeze window
return false
}
if(endDateValue <= freezeStart || endDateValue >= freezeEnd){
//end date is outside freeze window
return false
}
return true

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events