After Timesheet Submit/Revoke
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 or TimesheetApprovalRequest parameters 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)
}
}
}
Following scripts send a notification email to users who are preferred approvers (Requires WorklogPRO Version 4.5.0+) for the user submitting timesheet. Users can always send notification emails using @mention syntax in comment field when submitting their timesheet.
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.util.*;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.security.roles.ProjectRoleManager;
import com.atlassian.jira.security.groups.*;
import com.deniz.jira.worklog.services.*;
import com.deniz.jira.worklog.utils.*;
import com.deniz.jira.worklog.approval.*;
def submittingUserKey = timesheetApprovalRequest.userKey;
def explanation = timesheetApprovalRequest.explanation;
def period = timesheetApprovalRequest.period;
def action = timesheetApprovalRequest.action;
if (action == "SEND_TO_APPROVAL") {
def preferredApprovers = getPreferredApprovers(submittingUserKey);
sendEmailToUsers(preferredApprovers, submittingUserKey, period, explanation);
}
def getPreferredApprovers(def submittingUserKey) {
def timesheetApprovalService = ComponentAccessor.getOSGiComponentInstanceOfType(TimesheetApprovalService.class);
def preferredApprovers = [] as Set;
def preferredApproversPerProject = timesheetApprovalService.getPreferredApprovers(submittingUserKey);
preferredApproversPerProject.each { projectId, preferredApproversForProject ->
preferredApprovers.addAll(preferredApproversForProject);
}
log.debug("Preferred approvers {}", preferredApprovers);
return preferredApprovers;
}
def getTimesheetURL(def submittingUser, period) {
def properties = ComponentAccessor.getOSGiComponentInstanceOfType(com.atlassian.sal.api.ApplicationProperties.class);
return properties.getBaseUrl() + "/secure/WPShowTimesheetAction!customTimesheet.jspa?periodMode=MONTH&targetType=USER&calendarType=CUSTOM&groupingType=ISSUE#targetType=USER&targetKey=" + submittingUser.getUsername() + "&groupingType=Issue&periodMode=PERIOD&startDate=" + period.startDate + "&endDate=" + period.endDate + "&periodLocked=false&calendarType=CUSTOM&saveToUserHistory=false&extraIssueFilter=&viewType=TIMESHEET";
}
def sendEmailToUsers(def preferredApprovers, def submittingUserKey, period, explanation) {
def emailService = ComponentAccessor.getOSGiComponentInstanceOfType(EmailService.class);
def userManager = ComponentAccessor.getComponentOfType(UserManager.class);
def submittingUser = userManager.getUserByKey(submittingUserKey);
def emailSubject = "Period " + period.startDate + " - " + period.endDate + " is send to approval by " + submittingUser.getDisplayName();
def emailBody = "<blockquote>" + explanation + "</blockquote>" + "<div>Please approve/reject the <a href='" + getTimesheetURL(submittingUser, period) +"'>timesheet</a></div>";
preferredApprovers.each {userKey ->
def approverUser = userManager.getUserByKey(userKey);
emailService.sendEmailToUser(submittingUser, approverUser, emailSubject, emailBody);
}
}