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 5 Next »

You can execute execute a Groovy script just after usual handling of timesheet submission/revoke and perform additional processing such as auto approval of timesheet or sending emails. Note that, at that time timesheet submission/revoke is already done, you can’t do anything to stop it. This script should not return any result and should not cause any exception.

Within this script you can access Jira services, WorklogPRO services and services from other plugins. You can use TimesheetApprovalAction parameter to access values submitted by the user. Following script automatically approves the user’s timesheet if user is member of “jira-administrators” user group.

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.*;
import com.deniz.jira.worklog.approval.*;
import com.atlassian.jira.security.groups.*;
 
//We need to load WorklogPRO classes differently using getOSGiComponentInstanceOfType
def worklogHelper = ComponentAccessor.getOSGiComponentInstanceOfType(WorklogHelper.class);
def timesheetApprovalService = ComponentAccessor.getOSGiComponentInstanceOfType(TimesheetApprovalService.class);
def groupManager = ComponentAccessor.getComponent(GroupManager.class);

def userKey = timesheetApprovalRequest.userKey;
def explanation = timesheetApprovalRequest.explanation;
def period = timesheetApprovalRequest.period;
def action = timesheetApprovalRequest.action;

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

def sendApprovalEmail = true;

if (timesheetApprovalRequest.action == "SEND_TO_APPROVAL") {
  if (groupManager.isUserInGroup(userKey, "jira-administrators")) {
    def workedProjectIds = timesheetApprovalService.getWorkedProjectsForUser(period.id, userKey);
    workedProjectIds.each { projectId ->
      log.debug("Approving project {} for user {}", projectId, userKey);
      timesheetApprovalService.approvePeriod(period.id, projectId, userKey, explanation, sendApprovalEmail)
    }
  }
}
  • No labels