How many of you knows, when an issue grows and become a big issue ( for diverse reasons, more comments, more worklogged hous in Tempo, …), then the access to this ticket cost a lot of CPU. In this post I try to make a little example of a Custom Groovy Scriptrunner Service to archive all the big tasks ( closing the task and creating a new version).

import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.bc.JiraServiceContextImpl
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.issue.search.*
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.index.IndexException
import com.atlassian.jira.ManagerFactory
import com.atlassian.jira.user.ApplicationUser
import org.apache.log4j.Category
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.util.ImportUtils
import com.opensymphony.workflow.WorkflowContext
import com.atlassian.jira.user.ApplicationUsers
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.ManagerFactory
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.ComponentManager

import com.opensymphony.workflow.WorkflowContext
import com.atlassian.jira.workflow.WorkflowTransitionUtil;
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl;
import com.atlassian.jira.util.JiraUtils;

WorkflowTransitionUtil workflowTransitionUtil = ( WorkflowTransitionUtil ) JiraUtils.loadComponent( WorkflowTransitionUtilImpl.class );
 


 
String jqlSearch = " project in (..) and status != xxx  and timespent >= xxxx and ...";
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser.class)
ComponentManager componentManager = ComponentManager.getInstance()
CommentManager commentManager = ComponentAccessor.getCommentManager()
IssueManager issueManager = ComponentAccessor.getIssueManager();
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
UserUtil userUtil = ComponentAccessor.getUserUtil()
def searchProvider = ComponentAccessor.getComponent(SearchProvider.class)
def user2 = userUtil.getUserByKey("admin_user")

 
Category log = Category.getInstance("com.onresolve.jira.groovy")
log.setLevel(org.apache.log4j.Level.DEBUG)
log.debug "START: SERVICE CLONE AND ARCHIVE BIG ISSUES"

def String adminUserName = "admin_user"
def String nagComment = ""

SearchService.ParseResult parseResult =  searchService.parseQuery(user2.getDirectoryUser(), jqlSearch)

if (parseResult.isValid()) {
    def searchResult = searchService.search(user2.getDirectoryUser(), parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
    issues = searchResult.issues.collect {issueManager.getIssueObject(it.id)}
	log.debug "Size:" + issues.size();
	

	//------  Another way to do the same --------
        //def ctx = new JiraServiceContextImpl(user2);
	//def query = jqlQueryParser.parseQuery(jqlSearch);
	//def results = searchProvider.search(query, user2, PagerFilter.getUnlimitedFilter());
	//def sr = searchRequestService.getFilter(ctx, results);
	//def issues = results.getIssues();
	//log.debug "Size:" + issues.size();
	
	for ( issue in issues ){
		
		log.debug "Issue: " +issue.getKey();
		nagComment = "Hello [~"+issue.reporter.name+"],\nThis ticket is very big. Archiving ticket."
		log.debug "issue: ${issue.getId()}, ${issue.getKey()} commented to ARCHIVE."
		commentManager.create(issueManager.getIssueObject(issue.getKey()), adminUserName, nagComment, true)
		
		issueFactory = ComponentManager.getInstance().getIssueFactory()
		newissue = issueFactory.getIssue()
		newissue.setSummary (issue.summary)
		newissue.setProject (issue.project) 
		newissue.setIssueTypeId(issue.issueTypeObject.id) 
		newissue.description = issue.description
		newissue.reporter = issue.getReporter()
		newissue.assignee = issue.getAssignee()
 
		params = ["issue":newissue]
		subTask = issueManager.createIssue(adminUserName, params)
		
		nagComment = "The new ticket is " + newissue.getKey();
		log.debug "issue: ${issue.getId()}, ${issue.getKey()} commented to ARCHIVE."
		commentManager.create(issueManager.getIssueObject(issue.getKey()), adminUserName, nagComment, true)
		
		//Clone link
		linkMgr = ComponentManager.getInstance().getIssueLinkManager()
		linkMgr.createIssueLink (newissue.id, issue.id, Long.parseLong("XXXXX"),Long.valueOf(1), user2.getDirectoryUser())
		
		//SET STATUS CLOSED
		MutableIssue myIssue = issue
		myIssue.setStatusId("X") //CLOSED
		myIssue.setResolutionId("X") //DONE
		myIssue.setSummary ("Archived:" + issue.summary)
		myIssue.store()
		
		issueobtained = issueManager.getIssueObject(issue.getKey());
		ManagerFactory.getIndexManager().reIndex(issueobtained.getGenericValue());
		
		workflowTransitionUtil.setIssue(issue);
        workflowTransitionUtil.setUsername(adminUserName);
        workflowTransitionUtil.setAction (X); //Transition to Close  
		
		workflowTransitionUtil.validate();
        workflowTransitionUtil.progress();
		
		
	}
	log.debug "FINISH: SERVICE "
	
} else {
    log.debug("Invalid JQL: " + jqlSearch);
} 
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