Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

Following script uses WorklogHelper service to get daily required work hours and daily actual work hours of the user for the timesheet period and checks the differences for totals. If user is submitted less than required work hours, it rejects timesheet submission.

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.security.roles.ProjectRoleManager;
import com.deniz.jira.worklog.services.*;
import com.deniz.jira.worklog.utils.*;
 
//We need to load WorklogPRO classes differently using getOSGiComponentInstanceOfType
def worklogHelper = ComponentAccessor.getOSGiComponentInstanceOfType(WorklogHelper.class);

//please enable logging for package "com.deniz.jira.worklog.scripting" from Administration/System/Logging and Profiling
log.debug("userKey, {}", timesheetApprovalRequest.userKey);
log.debug("action, {}", timesheetApprovalRequest.action);
log.debug("explanation, {}", timesheetApprovalRequest.explanation);
log.debug("startDate, {}", timesheetApprovalRequest.period.startDate);
log.debug("endDate, {}", timesheetApprovalRequest.period.endDate);

if (timesheetApprovalRequest.action == "SEND_TO_APPROVAL") {
  long[] dailyRequiredWorkInSeconds = worklogHelper.getDailyRequiredWorkInSeconds(timesheetApprovalRequest.userKey, timesheetApprovalRequest.period);
  log.debug("dailyRequiredWorkInSeconds:{}", dailyRequiredWorkInSeconds);
  
  long[] dailyActualWorkInSeconds = worklogHelper.getDailyActualWorkInSeconds(timesheetApprovalRequest.userKey, timesheetApprovalRequest.period);
  log.debug("dailyActualWorkInSeconds:{}", dailyActualWorkInSeconds);
  
  long requiredTotal = dailyRequiredWorkInSeconds.sum();
  log.debug("requiredTotal(seconds):{}", requiredTotal);
  long actualTotal = dailyActualWorkInSeconds.sum();
  log.debug("actualTotal(seconds):{}", actualTotal);
  
  if (actualTotal < requiredTotal) {
    return String.format("You can't submit timesheet. You have only provided %.2f of %.2f", actualTotal/3600, requiredTotal/3600);
  }
}

  • No labels