See this code:


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.customfields.CustomFieldType
import com.atlassian.jira.issue.CustomFieldManager
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 com.atlassian.jira.user.util.UserUtil
import org.apache.log4j.Category
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 = "TEST" and status = "Resolved" AND NOT updated >= -5d '
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
ComponentManager componentManager = ComponentManager.getInstance()
//CommentManager commentManager = componentManager.getCommentManager()
CommentManager commentManager = ComponentAccessor.getCommentManager()
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
IssueManager issueManager = ComponentAccessor.getIssueManager();
UserManager userManager = ComponentAccessor.getUserManager();
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
UserUtil userUtil = ComponentAccessor.getUserUtil()
def user2 = userUtil.getUserByKey("useradmin_or_other_with_permissions")
Category log = Category.getInstance("com.onresolve.jira.groovy")
log.setLevel(org.apache.log4j.Level.DEBUG)
log.debug "CLOSING ISSUES IN RESOLVED IN TEST (5 DAYS IN RESOLVED)"

def String adminUserName = "user_who_send_the_comment_mail_with_the_survey"
def String nagComment = ""
List<Issue> issues = null

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)}
for ( issue in issues ){
//Find Custom Field Location.. it's only an example. This field we need to set as input parameter for the survey
def cf_location = customFieldManager.getCustomFieldObject("customfield_XXXXXX")
def location = issue.getCustomFieldValue(cf_location)?.value

nagComment = "Hello [~"+issue.reporter.name+"],\nThis ticket has been closed automatically by the system ( 5 days in Resolved state)\n [Please fill this survey|https://docs.google.com/a/XXXXX/viewform?entry.XXX&entry.XXXX="+(issue.getKey())+"&entry.XXXXX="+location+"&entry.XXXX="+issue.reporter.name+"]"
log.debug "issue: ${issue.getId()}, ${issue.getKey()} updated"
commentManager.create(issueManager.getIssueObject(issue.getKey()), adminUserName, nagComment, true)
//SET STATUS X, Ressolution X
MutableIssue myIssue = issue
myIssue.setStatusId("X")
myIssue.setResolutionId("X")
myIssue.store()

issueobtained = issueManager.getIssueObject(issue.getKey());
ManagerFactory.getIndexManager().reIndex(issueobtained.getGenericValue());

workflowTransitionUtil.setIssue(issue);
workflowTransitionUtil.setUsername(adminUserName);
//To follow the right transition to the status ( else, the status will be corruptd)
workflowTransitionUtil.setAction (XXX);

workflowTransitionUtil.validate();
workflowTransitionUtil.progress();
}
log.debug "CLOSING ISSUES IN RESOLVED IN TEST (5 DAYS IN RESOLVED) FINISH"

} else {
log.debug("Invalid JQL: " + jqlSearch);
}

Posted by:.

One thought on “New Example of Groovy JIRA Service to CLOSE issues in 5 days and send mail with Survey of Google Forms

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 )

Facebook photo

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

Connecting to %s