Jira can be a perfect tool for managing any type of projects. However, out of the box, Jira doesn’t have a start date and end date fields and if you created these two fields as custom one you can’t link them directly to the time tracking functionality without a custom script.
I did some research and I found ScriptRunner for Jira App (add-on) from adaptavist. By using this app you can write your own custom script using groovy and run it to extend the functionalities of Jira.
I created a script to calculate the estimated days between two fields (Start date and End date) and it will update Jira time tracking fields (Estimated and Remaining). Also the script will take into consideration the previously logged time in the calculations.
You have to enable time tracking in Jira, create these two fields, install the app and add the script to Behavior from Administration menu on the End date field.
import java.text.DateFormat;
import java.util.Calendar;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.properties.ApplicationProperties
import com.atlassian.jira.util.JiraDurationUtils
JiraDurationUtils jiraDurationUtils = ComponentAccessor.getComponent(JiraDurationUtils.class)
ApplicationProperties applicationProperties = ComponentAccessor.getApplicationProperties()
// Get a pointer to my custom fields
def startDateField = getFieldByName("Start date")
def endDateField = getFieldByName("End date")
def originalEstimateField = getFieldById("timetracking_originalestimate")
def remainingEstimateField = getFieldById("timetracking_remainingestimate")
// Get the Values of My Date Fields
def startDateFieldVal = startDateField.getValue() as Date
def endDateFieldVal = endDateField.getValue() as Date
def value = (originalEstimateField.getValue()) as String
def remvalue = (remainingEstimateField.getValue()) as String
// Specify a variable used for storing the number of weekdays between two dates
int ellapsedTime;
// Check if End Date contains a valid value
if (endDateFieldVal) {
// Calculate the number of weekdays between the start and end date below
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(startDateFieldVal);
cal2.setTime(endDateFieldVal != null ? endDateFieldVal : startDateFieldVal);
if (cal2.before(cal1)) {
// Don't attempt to calculate days if end date is before the start date
return 0;
}
while (cal1.before(cal2)) {
// If the days include weekends skip those days and do not count them
if ((Calendar.SATURDAY != cal1.get(Calendar.DAY_OF_WEEK))
&& (Calendar.SUNDAY != cal1.get(Calendar.DAY_OF_WEEK))) {
ellapsedTime++;
}
cal1.add(Calendar.DATE, 1);
}
// End of code block for calculating the number of weekdays
// Work out the long value by multiplying the number of days against the number of hours in our work day
// that we have configured inside JIRA e.g 4 * 480 = L1920 which represents 4 days
long updatedEstimatedDays = ellapsedTime * 480 + 480 as Long // Plus 480 to add on the day that the current day that the date calculation chops off.
long longoriginalEstimateField = jiraDurationUtils.parseDuration(value, applicationProperties.getDefaultLocale()) / 3600 as Long
long longremainingEstimateField = jiraDurationUtils.parseDuration(remvalue, applicationProperties.getDefaultLocale()) / 3600 as Long
long originalEstimatedDays = longoriginalEstimateField ? longoriginalEstimateField : 0
long originalRemainingDays = longremainingEstimateField ? longremainingEstimateField : 0
long diffInDays = updatedEstimatedDays - originalEstimatedDays
if(diffInDays >= 0 ) {
originalEstimateField.setFormValue(updatedEstimatedDays)
remainingEstimateField.setFormValue(originalRemainingDays + diffInDays)
}
}