Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Within this script you can access Jira services, WorklogPRO services and services from other plugins. You can use TimesheetApprovalAction parameter to access approval related parameters.

Assigning the text attribute value as APPROVE/REJECT after the timesheet is approved or rejected

After the timesheet is approved, the text attribute value of all worklogs in the relevant timesheet changes to 'APPROVE' if it has a value before. Similary, after the timesheet is rejected, the value changes to 'REJECT'.

Code Block
import com.atlassian.jira.project.Project;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.security.roles.ProjectRole;
import com.atlassian.jira.security.roles.ProjectRoleManager;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.deniz.jira.worklog.*;
import com.deniz.jira.worklog.approval.*;
import com.deniz.jira.worklog.scripting.TimesheetApprovalRequest;
import com.deniz.jira.worklog.services.*;
import com.deniz.jira.worklog.services.WorklogProService;
import com.deniz.jira.worklog.services.TimesheetService;
import com.atlassian.jira.bc.issue.worklog.DefaultWorklogService;
import com.atlassian.jira.bc.issue.worklog.*;
import com.atlassian.jira.issue.worklog.Worklog;
import com.deniz.jira.worklog.data.attr.AttrWorkLogImp;
import com.deniz.jira.worklog.data.attr.AttrWorkLog;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.component.*;
import com.atlassian.jira.issue.*;
import com.atlassian.jira.issue.worklog.DefaultWorklogManager;
import com.atlassian.jira.issue.worklog.WorklogManager;
import com.atlassian.jira.security.groups.*;
import com.atlassian.jira.project.*;
import com.atlassian.jira.*;
import com.atlassian.jira.user.util.*;
import com.deniz.jira.worklog.services.attr.*;
import java.time.*;
import java.util.*;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import com.atlassian.jira.bc.issue.worklog.WorklogService;
import com.deniz.jira.worklog.services.attr.AttrTypeService;
import org.slf4j.*;
 
//please enable logging for package "com.deniz.jira.worklog.scripting" from Administration/System/Logging and Profiling
Logger log = LoggerFactory.getLogger(com.deniz.jira.worklog.scripting.ScriptingService.class);

def attrIdToChecStatusTS = 1;

TimesheetApprovalService approvalService = ComponentAccessor.getOSGiComponentInstanceOfType(TimesheetApprovalService.class);
AttrTypeService attrTypeService = ComponentAccessor.getOSGiComponentInstanceOfType(AttrTypeService.class);
TimesheetService timesheetService = ComponentAccessor.getOSGiComponentInstanceOfType(TimesheetService.class);
IssueManager issueManager = ComponentAccessor.getIssueManager();
DefaultWorklogManager defaultWorklogManager = ComponentAccessor.getComponent(DefaultWorklogManager.class);
WorklogManager worklogManager = ComponentAccessor.getComponent(WorklogManager.class);
UserManager userManager = ComponentAccessor.getUserManager();
ProjectManager projectManager = ComponentAccessor.getProjectManager();

// Define a DateTimeFormatter
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

// Parse the given date string to ZonedDateTime
ZonedDateTime zonedDateTimeStart = LocalDate.parse(timesheetApprovalAction.period.getStartDate(), dtf).atStartOfDay(ZoneOffset.UTC);
ZonedDateTime zonedDateTimeEnd = LocalDate.parse(timesheetApprovalAction.period.getEndDate(), dtf).atStartOfDay(ZoneOffset.UTC).plusDays(1).minusSeconds(1);

timesheetService.getUsersWorklogs(zonedDateTimeStart, zonedDateTimeEnd, true, null,
null, null, null, timesheetApprovalAction.userKey).projects.each { projectLog -> 
  def project = projectManager.getProjectObj(projectLog.id);
  def projectFromKey = projectManager.getProjectByCurrentKey(timesheetApprovalAction.projectKey);
  if (project.equals(projectFromKey)) {
    
    projectLog.issues.each { issue -> 
      issue.workLogs.each { worklog -> 
        Worklog worklogFromJira = worklogManager.getById(worklog.id);
        def worklogId=worklogFromJira.getId();
        if(attrTypeService.getWorklogAttribute(attrIdToChecStatusTS, worklogFromJira.getId()).isPresent()) {
            AttrWorkLog statusWorklogValue = attrTypeService.getWorklogAttribute(attrIdToChecStatusTS, worklogFromJira.getId()).get();
            List<AttrWorkLogImp> attrWorkLogImplList = attrTypeService.getWorkLogAttrList(worklogFromJira.getId());
            AttrWorkLogImp attrWorkLogImpl = attrWorkLogImplList.stream().filter{a -> a.getAttrTypeId().equals(attrIdToChecStatusTS)}.findFirst().get();
            attrWorkLogImpl.copyTo(statusWorklogValue);
            statusWorklogValue.setAttrValue(timesheetApprovalAction.action);
            attrWorkLogImpl.copyFrom(statusWorklogValue);
            attrTypeService.updateWorkLogAttribute(attrWorkLogImpl);
          };
      };
    };
  };
}