Versions Compared

Key

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

You can execute a Groovy script just after usual handling of worklog create and update and perform additional logic.

...

When this script is running, work is already logged, so there is no way to stop logging work within this script. If you need to validate and stop logging work, you can use “Worklog Verification Script”.

Removing “Work logged” on a Child Issue from “Remaining Estimate” of Parent Epic

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. This script exactly does that. You may need additional logic for your own needs.

Code Block
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);

def issueManager = ComponentAccessor.getIssueManager();
def issueService = ComponentAccessor.getIssueService();
def authenticationContext = ComponentAccessor.getJiraAuthenticationContext();
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def previousTimeSpent = 0;

if (update) {
  previousTimeSpent = previousWorklog.getTimeSpent();
}

def currentUser = authenticationContext.getLoggedInUser();
def issue=worklog.getIssue();
def epicLinkCf = customFieldManager.getCustomFieldObjectByName("Epic Link");
def epicIssue = issue.getCustomFieldValue(epicLinkCf);
def issueSpentTime=worklog.getTimeSpent();
def diff = issueSpentTime - previousTimeSpent;

if (epicIssue) {
    log.debug("Subtracting {} seconds from remaining estimate of epic {}", diff, epicIssue.getKey());
    def epicEstimate=epicIssue.getEstimate();
    Long epicEstimateUpdated=epicEstimate-diff;
    if (epicEstimateUpdated < 0) {
      epicEstimateUpdated = 0;
    }
    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())
    }
}