Before Worklog Delete
You can execute a Groovy script just before usual handling of worklog delete and perform additional logic.
Adding “Work logged” of a Child Issue to “Remaining Estimate” of Parent Epic When "Work logged" is deleted
Some organizations prefer to define “Remaining Estimate” an “Original Estimate” required for a development in the parent epic. They log work to contained issues within the epic and these issues don’t have their own “Remaining Estimate” or “Original Estimate”. They want “time spent” for these contained issues to be deducted from “Remaining Estimate” of the parent epic.
The script that provides this is on the pageAfter Worklog Create/Update .Furthermore, when the worklog entered in a child issue is deleted, they want this worklog to be added to the 'remaining estimate' part of the epic. This script provides that.
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.security.groups.GroupManager;
import com.atlassian.jira.issue.IssueInputParameters;
import com.atlassian.jira.bc.issue.IssueService;
import com.atlassian.jira.bc.issue.DefaultIssueService;
import com.atlassian.jira.bc.issue.worklog.WorklogInputParametersImpl.Builder;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.IssueInputParameters;
import com.atlassian.jira.security.*;
import org.slf4j.*;
Logger log = LoggerFactory.getLogger(com.deniz.jira.worklog.scripting.ScriptingService.class);
log.debug('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
def issueManager = ComponentAccessor.getIssueManager();
def issueService = ComponentAccessor.getIssueService();
def authenticationContext = ComponentAccessor.getJiraAuthenticationContext();
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def currentUser = authenticationContext.getLoggedInUser();
def issue=worklogResult.getWorklog().getIssue();
def epicLinkCf = customFieldManager.getCustomFieldObjectByName("Epic Link");
def epicIssue = issue.getCustomFieldValue(epicLinkCf);
def parentObject=issue.getParentObject();
if(parentObject){
def epicIssueOfObject=parentObject.getCustomFieldValue(epicLinkCf);
if(epicIssueOfObject){
//In this case,epic has a child issue and child issue has a subtask.Worklog is done for this subtask.
epicIssue = epicIssueOfObject;
}
if(parentObject.issueType.name == 'Epic'){
//In this case,epic has a subtask.Worklog is done for this subtask.
epicIssue=parentObject;
log.debug("EPIC ISSUE IS PARENT OBJECT:{}", epicIssue);
}
}
if (epicIssue!=null){
log.debug("EPIC ISSUE:{}", epicIssue);
def issueSpentTime=worklog.getTimeSpent();
log.debug("TIME SPENT:{}", issueSpentTime);
def epicEstimate=epicIssue.getEstimate();
log.debug("EPIC ESTIMATE:{}", epicEstimate);
if(epicEstimate!=null){
//Since this script runs before worklog deletion process,issueSpentTime is added twice to remaining estimate,so the number issueSpentTime is divided by 2 in calculating 'epicEstimateUpdated'
Long epicEstimateUpdated=epicEstimate+(issueSpentTime/2);
log.debug("EPIC ESTIMATE UPDATED:{}", epicEstimateUpdated);
IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();
issueInputParameters.setRemainingEstimate((epicEstimateUpdated/60) + "m");
IssueService.UpdateValidationResult updateValidationResult = issueService.validateUpdate(currentUser, epicIssue.getId(), issueInputParameters);
if(updateValidationResult.isValid()){
issueService.update(currentUser, updateValidationResult);
}else{
log.error("!!!Error in Worklog Update Script:{}", getErrorCollection.getErrorCollection())
}
}
}
return null;