Sometimes we need to transition issues from one status to other, but these issues can be from different projects and can be from different Workflows.

In this case, probably the Transition IDs will be different.

For this reason we need to develop a “discover transition” function.

See below an example of how we can start with the code…

String jqlSearch = "Some JQL Query"
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
SearchService.ParseResult parseResult = searchService.parseQuery(currentUserObj, jqlSearch)
if (parseResult.isValid()) {

def searchResult = searchService.search(currentUserObj, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())

def issues = searchResult.issues.collect {issueMgr.getIssueObject(it.id)}
for ( issue in issues ){
   //Transition issue by transition name and not by transition id, very useful for queries with issues with different workflows
   def action = findTransition(issue,"Transition Name")
   if ( action != null ) executeTransition(issue,action,currentUserObj)
   //Do more changes to the issue...
   MutableIssue issue_aux = issue
   issue_aux.setAssigneeId(issue.getReporterId())
   issue_aux.store()
}
}

def ActionDescriptor findTransition(Issue issue, String actionNamePattern) {
    JiraWorkflow workflow = ComponentAccessor.getWorkflowManager().getWorkflow(issue)
    Status status = issue.getStatus()
    StepDescriptor sd = workflow.getLinkedStep(status)
    Iterator<ActionDescriptor> it = sd.getActions().iterator()
    ActionDescriptor action = null
	while ( it.hasNext() ) {
        ActionDescriptor ad = (ActionDescriptor)it.next()
        String actionName = ad.getName()
        if ( actionName==actionNamePattern ) {
            action = ad
        }
    }
    return action
}

def executeTransition(Issue issue, ActionDescriptor action, ApplicationUser user) {
    try {
        WorkflowTransitionUtil workflowTransitionUtil = ComponentAccessor.getComponent(WorkflowTransitionUtilFactory.class).create()
        ErrorCollection errors = null
        workflowTransitionUtil.setIssue((MutableIssue) issue)
        workflowTransitionUtil.setUserkey(user?.getKey())
        workflowTransitionUtil.setAction(action?.getId())
        errors = workflowTransitionUtil.validate()
    } catch (Exception e) {
    }
}

Advertisements
Posted by:.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s