Script works in console, but not as a service

Kyle Moseley _blueridge_cx_ November 10, 2014

I've written this script and it works great in the console, but it's not working when I schedule it as a service. I've tested some other scripts in services and they've all worked. I'm almost certain my problem lies with my method of JQL query, but I don't understand why it works in the console if it's a problem.

I'd love any feedback. Thanks.

import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
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.user.util.UserUtil
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.workflow.WorkflowTransitionUtil
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl
import com.atlassian.jira.util.JiraUtils
import com.atlassian.jira.util.ImportUtils
// --------------------------------
// 		adjustable variables
// --------------------------------
// project or category
def project = "(project = SAND)"
// issue status to transition from
def status = "(status = Resolved)"
// date field to measure against
def dateField = "resolved"
// the length of time (in days) that should pass
// before the issue auto-closes. only enter the
// number of weekdays, i.e. 5 days for a work-week
// 0 days will mean it will look for older than now
def dayLength = 0
// workflow's transition ID number
def transitionId = 701
// --------------------------------
// 		date and query formation
// --------------------------------
def today = new Date()
def i = 0
while (i<dayLength) {
	today = today.minus(1)
	strDay = today.format("EEEE")
	if (strDay!="Saturday"&&strDay!="Sunday"){
		i++
	}
}
def finalDate = "\"" + today.format("yyyy/MM/dd HH:mm") + "\""
jqlSearch = project + " AND " + status + " AND " + "(" + dateField + " < " + finalDate + ")"
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
def User user = ComponentAccessor.getUserUtil().getUserByName("n9948469").getDirectoryUser()
// --------------------------------
// 		creation of issue list
// --------------------------------
def 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.getIssues()
	issues.each{issue ->
		strissue = issue.getKey()
		def ComponentManager componentManager = ComponentManager.getInstance()
		IssueManager issueManager = componentManager.getIssueManager()
		def MutableIssue missue = (MutableIssue) issueManager.getIssueObject(strissue)
		WorkflowTransitionUtil workflowTransitionUtil = ( WorkflowTransitionUtil ) JiraUtils.loadComponent( WorkflowTransitionUtilImpl.class )
		workflowTransitionUtil.setIssue(missue)
		if (missue.getAssigneeId()!=null) {
			workflowUser = missue.getAssigneeId()
		} else {
			workflowUser = missue.getReporterId()
		}
		workflowTransitionUtil.setUsername(workflowUser)
		workflowTransitionUtil.setAction(transitionId)
		workflowTransitionUtil.progress()
		//re-index issue
		def indexManager = ComponentManager.getInstance().getIndexManager()
		boolean wasIndexing = ImportUtils.isIndexIssues()
		indexManager.reIndex(missue)
		ImportUtils.setIndexIssues(wasIndexing)
		return issue
	}
}
else {
}

1 answer

1 accepted

1 vote
Answer accepted
JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 10, 2014

What happens when run as a service, just no issues retrieved? Or transition fails?

Some methods check the current logged in user, just for kicks put this in and see if that works:

ComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(user)

 

 

 

Kyle Moseley _blueridge_cx_ November 10, 2014

That did it. Jamie, you're great!

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 10, 2014

No worries but... curious to know why it doesn't work without that as you're passing a user object to the relevant methods.

Kyle Moseley _blueridge_cx_ November 10, 2014

Yeah, it didn't make any sense to me. I even went out of my way to set it to a certain user rather than getting the logged in user, figuring that would work in console but how could it in the service. Every aspect of the script, sans the jql query/iterating through results, worked fine as a service without any sort of logged in user context...so that's why I pointed the finger there.

Suggest an answer

Log in or Sign up to answer