Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save azzlack/728992d7d7e74a66aac66154bf507141 to your computer and use it in GitHub Desktop.
Save azzlack/728992d7d7e74a66aac66154bf507141 to your computer and use it in GitHub Desktop.
JIRA: Transition issue when adding to or removing from sprint
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.greenhopper.service.rapid.view.RapidViewService
import com.atlassian.greenhopper.service.sprint.SprintIssueService
import com.atlassian.greenhopper.service.sprint.SprintManager
import com.atlassian.greenhopper.service.sprint.SprintService
import org.apache.log4j.Logger
import org.apache.log4j.Level
def log = Logger.getLogger("com.knowit.UpdateIssueStatusWithSprint");
log.setLevel(Level.DEBUG);
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def workflowManager = ComponentAccessor.getWorkflowManager();
def issueService = ComponentAccessor.getIssueService()
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def sprintField = customFieldManager.getCustomFieldObjectByName("Sprint");
def sprints = event.issue.getCustomFieldValue(sprintField);
def workflow = workflowManager.getWorkflow(event.issue);
def status = event.issue.getStatus().getName();
def availableActions = workflow.getAllActions();
log.info("Issue has status '" + status + "'");
if (sprints != null) {
log.info("Issue added to sprint: " + event.issue);
if (status == "Backlog") {
log.info("Trying to find 'Select for Development' action to progress task");
def actionId = -1;
// Find the "Select for development" action
for (def actionDescriptor : availableActions) {
if (actionDescriptor.getName().equals("Select for Development")) {
actionId = actionDescriptor.getId();
}
}
if (actionId > -1) {
def validationResult = issueService.validateTransition(user, event.issue.id, actionId, issueService.newIssueInputParameters());
if (validationResult.isValid()) {
log.info("Issue can be changed with action " + actionId);
def transitionResult = issueService.transition(user, validationResult);
if (transitionResult.isValid()) {
log.info("Successfully transitioned issue");
}
else {
log.error("Error when transitioning issue");
}
}
else {
log.error("Issue cannot be changed with action " + actionId);
log.error(validationResult.errorCollection);
}
}
else {
log.info("Could not find 'Select for Development' action")
}
}
}
else {
log.info("Issue removed from sprint: " + event.issue);
if (status == "Selected for Development" || status == "In Progress" ) {
log.info("Trying to find 'Move to Backlog' action to progress task");
def actionId = -1;
// Find the "Move to Backlog" action
for (def actionDescriptor : availableActions) {
if (actionDescriptor.getName().equals("Move to Backlog")) {
actionId = actionDescriptor.getId();
}
}
if (actionId > -1) {
def validationResult = issueService.validateTransition(user, event.issue.id, actionId, issueService.newIssueInputParameters());
if (validationResult.isValid()) {
log.info("Issue can be changed with action " + actionId);
def transitionResult = issueService.transition(user, validationResult);
if (transitionResult.isValid()) {
log.info("Successfully transitioned issue");
}
else {
log.error("Error when transitioning issue");
}
}
else {
log.error("Issue cannot be changed with action " + actionId);
log.error(validationResult.errorCollection);
}
}
else {
log.info("Could not find 'Move to Backlog' action")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment