New example of Groovy Service for JIRA to Flag and comment issues not updated in X days in status X… very easy!

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.user.util.UserUtil
import com.atlassian.crowd.embedded.api.User
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.issue.customfields.CustomFieldType
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.context.IssueContext
import com.atlassian.jira.issue.context.IssueContextImpl
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder


String jqlSearch = 'project = XXXX AND status = "XXXX" AND updated <= -XXh ORDER BY updated DESC'
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
CommentManager commentManager = ComponentAccessor.getCommentManager()
IssueManager issueManager = ComponentAccessor.getIssueManager();
def userManager = ComponentAccessor.getUserManager()
def user = userManager.getUserByName("useradmin")
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()

Category log = Category.getInstance("com.onresolve.jira.groovy")
log.setLevel(org.apache.log4j.Level.DEBUG)
log.debug "UPDATING ISSUES: FLAGGING TICKETS IN X STATUS BY INACTIVITY"

def String adminUserName = "useradmin"
def String nagComment = ""

List < Issue > issues = null

SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch)
if (parseResult.isValid()) {
	def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
	issues = searchResult.issues.collect {
		(Issue) issueManager.getIssueObject(it.id)
	}
	for (issue in issues) {
		def cf = customFieldManager.getCustomFieldObjects(issue).find {
			it.name == 'Flagged'
		}
		def fieldConfig = cf.getRelevantConfig(issue)
		def value = optionsManager.getOptions(fieldConfig) ? .find {
			it.toString() == 'Impediment'
		}

		nagComment = "Hello [~" + issue.assignee.name + "],\n*You have a Jira ticket awaiting approval!*\n"
		log.debug "issue: ${issue.getId()}, ${issue.getKey()} commented."
		commentManager.create(issue, adminUserName, nagComment, true);


		IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
		cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), [value]), changeHolder)


		//break
	}
	log.debug " FINISH UPDATING: FLAGGING TICKETS IN X STATUS BY INACTIVITY"

} 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