import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.user.ApplicationUsers
import com.atlassian.jira.bc.issue.search.SearchService
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.issue.MutableIssue
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.user.ApplicationUser
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 org.apache.log4j.Category
 
String jqlSearch = 'status = "STATUSX" AND NOT updated >= -5d  AND issueFunction in lastComment("by raul.pelaez") ORDER BY updated DESC'
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
ComponentManager componentManager = ComponentManager.getInstance()
//CommentManager commentManager = componentManager.getCommentManager()
CommentManager commentManager = ComponentAccessor.getCommentManager()
IssueManager issueManager = ComponentAccessor.getIssueManager();
UserManager userManager = ComponentAccessor.getUserManager();
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
 
Category log = Category.getInstance("com.onresolve.jira.groovy")
log.setLevel(org.apache.log4j.Level.DEBUG)
log.debug "CLOSING ISSUES IN STATUSX (35 DAYS WITHOUT UPDATES)"

def String adminUserName = "raul.pelaez"
def String nagComment = ""
//def targetFieldName = "customfield_10260"
 
 
//def targetField = WorkflowUtils.getFieldFromKey(targetFieldName) 
List<Issue> issues = null
 
SearchService.ParseResult parseResult =  searchService.parseQuery(user, jqlSearch)
if (parseResult.isValid()) {
    def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
    // Transform issues from DocumentIssueImpl to the "pure" form IssueImpl (some methods don't work with DocumentIssueImps)
    issues = searchResult.issues.collect {issueManager.getIssueObject(it.id)}
	for ( issue in issues ){
		nagComment = "Hello [~"+issue.reporter.name+"],\nThis ticket has been closed automatically by the system as no updates or comments have been provided in the past 35 days"
		log.debug "issue: ${issue.getId()}, ${issue.getKey()} updated"
		commentManager.create(issueManager.getIssueObject(issue.getKey()), adminUserName, nagComment, true)
		//SET STATUS CLOSED
		MutableIssue myIssue = issue
		myIssue.setStatusId("6") //CLOSED
		myIssue.setResolutionId("8") //DONE
		myIssue.store()
		//ManagerFactory.getIndexManager().reIndex(myIssue.getGenericValue());
	}
	log.debug "CLOSING ISSUES IN STATUSX FINISH"
	
} 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