It's not the same without you
Join the community to find out what other Atlassian users are discussing, debating and creating.
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 { }
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.
This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.
Read moreHey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...
Connect with like-minded Atlassian users at free events near you!
Find a groupConnect with like-minded Atlassian users at free events near you!
Unfortunately there are no AUG chapters near you at the moment.
Start an AUGYou're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.