imports / getting started with groovy script runner

Celik Vedat December 15, 2017

Hi all,

 

i am about to learn scripting due to my subsidiary tasks as a jira admin. What I'm trying atm ist to calculate the days between two datefield values.

 

What kind of imports do I need before starting? I've seen some imports in other questions based on scriptrunner e.g.:

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;

 

what are these imports doing and is there a list of imports I need based on what I want to script?

 

br,

shippo

2 answers

1 accepted

1 vote
Answer accepted
MoroSystems Support
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 15, 2017

Hi, you need to import every class which is going to be used in your script except classes from java.lang package. It looks like you're not Java or Groovy programmer, right? So here is some information about import keyword in JAVA/Groovy:

https://en.wikibooks.org/wiki/Java_Programming/Keywords/import

For your task you will need to import at least:

  • com.atlassian.jira.issue.fields.CustomField
  • com.atlassian.jira.issue.CustomFieldManager
  • java.util.Date

You will probably need some more imports but you will find out from error which will be thrown by your script.

Feel free to write more details here...

Celik Vedat December 15, 2017

You're right i am neither a groovy nor a java programmer.

 

In the first step i want to calculate the days between two dates and return the value as days in a read-only text field. I've two custom field (date picker) called Start Date and End Date. After a user chooses these two dates i want my script field to display the days between these two dates. do you have an example how to picture this? In the next step i want to generate some more complex conditions e.g. showing a field based on the selection in a drop down field, and picture a field called complexitiy wich displays different complexitiy sizes from XS to XL based multiple field values. I would know how to start with java in eclipse hehe but i've difficulties with scripting in jira with groovy as mentioned

MoroSystems Support
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 15, 2017

Sorry, I don't have any script like that and I think that this community forum is much more about helping, not about solving issues which are often paid ;). So let's try to do it with "helping".

First you will have to prepare new scripted field:

https://scriptrunner.adaptavist.com/latest/jira/scripted-fields.html#_tips_tricks_and_gotchas

In your script you have to get insances of your both Date custom fieds. You can use ComponentAccessor#getCustomFieldManager() - https://docs.atlassian.com/software/jira/docs/api/7.6.1/com/atlassian/jira/component/ComponentAccessor.html#getCustomFieldManager--

Then you can get values of these fields using:

https://docs.atlassian.com/software/jira/docs/api/7.0.6/com/atlassian/jira/issue/fields/CustomField.html#getValue-com.atlassian.jira.issue.Issue-

You will get java.util.Date values from both fields. You can use following solution to get days between two days:

https://stackoverflow.com/a/30184795

Good luck and feel free to write again if you're stucked but after some research first, pls :).

Martin

Celik Vedat December 15, 2017

Thanks a lot, Appreciate your help :)

1 vote
Ivan Tovbin
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 12, 2018

Hi Celik,

If you just need the difference in days, then the script is rather simple. Create a scripted field which will be used in your issues to display your days difference. Assuming you know how to look up your custom fields' IDs use the below script for your scripted field:

import com.atlassian.jira.component.ComponentAccessor

def cfMgr = ComponentAccessor.getCustomFieldManager()
def startDate = cfMgr.getCustomFieldObject(11111.toLong())
def endDate = cfMgr.getCustomFieldObject(22222.toLong())

if (issue.getCustomFieldValue(startDate) != null && issue.getCustomFieldValue(endDate) != null){
return issue.getCustomFieldValue(endDate) - issue.getCustomFieldValue(startDate)
}
return null

Suggest an answer

Log in or Sign up to answer