Vacation calculation in Jira with groovy
Holidays are managed by Jira issues in a project with the name „App Data”. You can download holidays from different websites as csv, and then import this csv in Jira.
These issues can be in the status „Active” or „Inactive”. That is the workflow I applied to them.
The groovy script below belongs to another type of issues: I did a Jira Service Desk project for applying vacation days. After those issues are created, it will display the number of working days between start and end.
This solution is realized with the great, open-source, reliable Jira add-on myGroovy.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.issue.search.SearchQuery
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.jql.query.IssueIdCollector
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.user.UserPropertyManager
import com.atlassian.jira.user.ApplicationUser
def getIssuesByJQL(String jql) {
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def query = jqlQueryParser.parseQuery(jql)
SearchQuery searchQuery = SearchQuery.create(query, user)
IssueIdCollector collector = new IssueIdCollector()
searchProvider.search(searchQuery, collector)
return collector.getIssueIds().collect { getIssue(it as Long) }
}
def getIssue(Long id) {
ComponentAccessor.issueManager.getIssueObject(id)
}
// Start Date
def cfStart = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10206)
def cfStartValue = issue.getCustomFieldValue(cfStart) as Date
// End Date
def cfEnd = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10207)
def cfEndValue = issue.getCustomFieldValue(cfEnd) as Date
// Days Coverage
def cfDaysCoverage = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10414)
def cfDaysCoverageValue = (Option)issue.getCustomFieldValue(cfDaysCoverage)
def counter = 0
def dayCounter = 0
if ( cfDaysCoverageValue.getValue().equals("Half days") ) { counter = 0.5 }
else { counter = 1 }
// State
def userPropertyManager = ComponentAccessor.getUserPropertyManager()
def user = issue.getReporter()
def cpState = userPropertyManager.getPropertySet(user).getString("jira.meta.State")
// JQL
def startDateYear = cfStartValue.format("YYYY").toInteger()
def jqlString = "project = \"App Data\" AND issuetype = Holiday AND status = Active AND State = "+cpState+" AND \"Start date\" >= "+String.format("%04d", startDateYear)+"-01-01 AND \"Start date\" < "+String.format("%04d", startDateYear + 1)+"-01-01 ORDER BY \"Start date\""
// Main
def allHolidays = getIssuesByJQL(jqlString).collect { (it as Issue).getCustomFieldValue(cfStart) as Date }
def maxDate = Date.parse("yyyy-MM-dd", cfStartValue.format("YYYY")+"-12-31")
if (cfEndValue.compareTo(maxDate) < 0) { maxDate = cfEndValue }
for (def tempDate = cfStartValue; tempDate <= maxDate; tempDate++) {
if (tempDate.format("EEE") != "Sun" &&
tempDate.format("EEE") != "Sat" &&
!(allHolidays.any { it == tempDate }))
{ dayCounter += counter }
}
dayCounter
Present all data with eazyBI, a wonderful Jira add-on for the creation of reports. The calendar is done with Flexible Calendar.
The calculated measure for all available vacation days. The number will be split over rows caused by the dimensions on the left side.
The workflow for a vacation request in Jira.
Reach out to me for professional support!