Don't Allow Logging Work Under Specific Conditions

You can control project’s permission schema to control who can log work on an issue or use workflow status properties to control on which statuses work can be logged on an issue but sometimes you may need more complex controls. Following scripts are just examples of such conditions all of which are required by some of our customers.

Don't Allow Logging Work If Remaining Estimate is Zero or Not Specified

Following scripts do not allow any worklog if remaining estimate of an issue is zero or not specified. This script works on 'Before Worklog Create/Update' script type.

1 2 3 4 5 def issue = worklog.getIssue(); def remainingEstimate=issue.getEstimate(); if (remainingEstimate== null || remainingEstimate == 0 ) { //no remaining estimate on issue, don't allow logging work return "Can't log work because the remaining estimate is '0' or 'Not Specified'"; }

Don't Allow Logging Work Higher Than Remaining Estimate

Following scripts do not allow any worklog to be higher than remaining estimate of an issue. If there is no remaining estimate, users can log any duration.

1 2 3 4 5 6 7 def issue = worklog.getIssue(); if (issue.getEstimate() == null) { //no estimate on issue, allow logging work return; } if (issue.getEstimate() < worklog.getTimeSpent()) { return "Maximum duration you can log is " + issue.getEstimate() / 60 + " minute(s)" }

Don't allow logging work on issues which has subtasks

This script does not allow logging work on issues which has subtasks. This is applied in use cases where a parent task is divided to subtasks and each subtask is separately estimated.

1 2 3 if (worklog.getIssue().getSubTaskObjects().size() > 0) { return "If an issue has subtasks, you can only log work on subtasks of the issue" }

Don't allow logging work on subtasks

Simply the reverse of above script.

1 2 3 if (worklog.getIssue().isSubTask()) { return "You can't log work on subtasks" }

Don't allow logging work on specific issue types

Following script does not allow logging work if issue type is “Task”.

1 2 3 if (worklog.getIssue().getIssueType().getName() == "Task") { return "You can't log work for issue type 'Task'"; }

Don't allow logging work on resolved issues

1 2 3 if (worklog.getIssue().getResolution() != null) { return "You can't log work for resolved issues"; }

Don't allow logging work when an issue is in a specific status

1 2 3 if (worklog.getIssue().getStatus().getName() == 'Done') { return "You can't log work for issues in 'Done' issues status"; }

Don't allow logging work less than 30m

1 2 3 if (worklog.getTimeSpent() < 1800) { // 60 (seconds per minute) * 30 (minutes) = 1800 seconds return "Time spent can't be less than 30m"; }

Logged Time should be in block of 15 minutes

1 2 3 if ((worklog.getTimeSpent() % 900) != 0) { // 60 (seconds per minute) * 15 (minutes) = 900 seconds return "Please log 15 minutes or more, and if more, ensure it is in blocks of 15 minutes, e.g. 30m, 1.5h, etc"; }

Billable Time can't Be Larger than Time Spent

1 2 3 4 def billableTime = worklogAttributes.get(4); //update id with actual id of "Billable Time Worklog Attribute" for your system if (Integer.valueOf(billableTime) > worklog.getTimeSpent()) { return "Billable time can't be larger than time Spent"; }

If Users are in specific user group only allows logging work on subset of projects

1 2 3 4 5 6 7 8 9 10 11 12 13 14 import com.atlassian.jira.security.groups.GroupManager; import com.atlassian.jira.component.ComponentAccessor; def projects = ["ERP", "ACN"]; //projects users are allowed to work def issue = worklogResult.getWorklog().getIssue(); def project = issue.getProjectObject(); def GroupManager groupManager = ComponentAccessor.getGroupManager(); if (!projects.contains(project.getKey())) { String author = worklog.getAuthorObject(); Collection<String> groupNamesForUser = groupManager.getGroupNamesForUser(author); if(groupNamesForUser.contains("restricted-users")) { //users in this group can only work on selected projects return "You can't work on this project. You can only work on:" + projects; } }

 

If Users are in specific user group only allows logging work on subset of projects

1 2 3 4 5 6 7 8 9 10 11 12 13 14 import com.atlassian.jira.security.groups.GroupManager; import com.atlassian.jira.component.ComponentAccessor; def projects = ["ERP", "ACN"]; //projects users are allowed to work def issue = worklogResult.getWorklog().getIssue(); def project = issue.getProjectObject(); def GroupManager groupManager = ComponentAccessor.getGroupManager(); if (!projects.contains(project.getKey())) { String author = worklog.getAuthorObject(); Collection<String> groupNamesForUser = groupManager.getGroupNamesForUser(author); if(groupNamesForUser.contains("restricted-users")) { //users in this group can only work on selected projects return "You can't work on this project. You can only work on:" + projects; } }

Don't Allow Specifying a Work Start Date After Due Date of the Issue

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import com.atlassian.jira.component.ComponentAccessor; import com.atlassian.jira.security.JiraAuthenticationContext; import java.text.SimpleDateFormat; import java.time.Duration; def authenticationContext = ComponentAccessor.getJiraAuthenticationContext(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", authenticationContext.getLocale()); def issue = worklog.getIssue(); def dueDate = issue.getDueDate(); //2020-04-22 00:00:00, due dates default to 00:00:00 if (dueDate != null) { def dueDateNight = Date.from(dueDate.toInstant().plus(Duration.ofHours(23)).plus(Duration.ofMinutes(59))); //20-04-22 23:59:00 //the date on which work is performed, not the day worklog is created def workStartDate = worklog.getStartDate(); //If you need to consider timezone differences of users you may want to use LocalDateTime if (workStartDate.after(dueDateNight)) { return "You can't specify a work start date after " + simpleDateFormat.format(dueDate); } }

Don't Allow Worklog After a Specific Date Specified on Issue

1 2 3 4 5 6 7 8 9 10 11 import com.atlassian.jira.security.groups.GroupManager import com.atlassian.jira.component.ComponentAccessor def cfm = ComponentAccessor.getCustomFieldManager() def issue = worklog.getIssue() def endDateField = cfm.getCustomFieldObject('customfield_11300') //custom field id of date field we are interested def endDate = endDateField.getValue(issue) as Date if (worklog.getStartDate() > endDate + 1){ return "You can only log time before or during ${endDate.format('dd.MM.yyyy')}" }

Don't Allow a User to Log Overlapping Worklogs

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 import com.atlassian.jira.issue.worklog.*; import java.time.*; import java.time.temporal.*; import com.deniz.jira.worklog.services.*; import com.atlassian.jira.component.ComponentAccessor; import com.atlassian.jira.security.JiraAuthenticationContext; def timesheetService = ComponentAccessor.getOSGiComponentInstanceOfType(TimesheetService.class); def issue = worklog.getIssue(); def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(); def newWorklogStart = ZonedDateTime.ofInstant(worklog.getStartDate().toInstant(), ZoneId.systemDefault()); ZonedDateTime startTime = newWorklogStart.truncatedTo(ChronoUnit.DAYS); ZonedDateTime endTime = newWorklogStart.plusSeconds(worklog.timeSpent); def newWorklogStartInMilis = newWorklogStart.toInstant().toEpochMilli(); def newWorklogEndInMilis = newWorklogStart.toInstant().plusSeconds(worklog.timeSpent).toEpochMilli(); def worklogWrapper = timesheetService.getUsersWorklogs(startTime, endTime, true, null, null, null, null, currentUser.getUsername()); def errorMessage = null; worklogWrapper.projects.each { project -> project.issues.each { wIssue -> wIssue.workLogs.each { pWorklog -> if (pWorklog.id != worklog.id) { def pWorkStartInMilis = pWorklog.workStart.toInstant().toEpochMilli(); def pWorkEndInMilis = pWorklog.workStart.toInstant().plusSeconds(pWorklog.getTimeSpent()).toEpochMilli(); if ((newWorklogStartInMilis >= pWorkStartInMilis && newWorklogStartInMilis <= pWorkEndInMilis) || (newWorklogStartInMilis <= pWorkEndInMilis && newWorklogEndInMilis >= pWorkStartInMilis)) { errorMessage = "Collides with worklog which started on " + pWorklog.workStart.toString() + ". Please check your timesheet."; } } } } } return errorMessage;

Don’t Allow Non Service Desk Users to Create Worklogs

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 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.attr.AttrTypeService; def authenticationContext = ComponentAccessor.getJiraAuthenticationContext(); def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager.class); //We need to load WorklogPRO classes differently using getOSGiComponentInstanceOfType def attrTypeService = ComponentAccessor.getOSGiComponentInstanceOfType(AttrTypeService.class); def billableAttrType = attrTypeService.getAttrTypeWithName("Billable").get(); if (billableAttrType == null) { return; //there is no billable attribute } def isBillable = worklogAttributes.get(billableAttrType.getID()); def loggedInUser = authenticationContext.getLoggedInUser(); def serviceDeskTeamRole = projectRoleManager.getProjectRole("Service Desk Team"); def project = worklog.getIssue().getProjectObject(); println(worklogAttributes); if (isBillable == "yes") { //boolean attributes are has either "no" or "yes" value. if (!projectRoleManager.isUserInProjectRole(loggedInUser, serviceDeskTeamRole, project)) { return "Only Service Desk Team can register billable worklogs!" } }