import com.atlassian.crowd.embedded.api.User
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.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 org.apache.log4j.Category
//import com.googlecode.jsu.util.WorkflowUtils

jqlSearch = 'status = "Statux X" AND NOT updated >= -Nd AND issueFunction in commented("by user_special") ORDER BY updated DESC'
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
UserUtil userUtil = ComponentAccessor.getUserUtil()
User user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
ComponentManager componentManager = ComponentManager.getInstance()
CommentManager commentManager = componentManager.getCommentManager()
IssueManager issueManager = componentManager.getIssueManager();

Category log = Category.getInstance("com.onresolve.jira.groovy")
log.setLevel(org.apache.log4j.Level.DEBUG)
log.debug "CLOSING ISSUES IN STATUS X (N DAYS WITHOUT UPDATES)"
def String adminUserName = "user_special"
def String nagComment = "This ticket has been closed automatically by the system as no updates have been provided in the past N days."
//def targetFieldName = "customfield_10260"

if (!user) {
user = userUtil.getUserObject(adminUserName)
}

//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 ){
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 STATUS X 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