Hi
I have been asked to update the resolution date of almost 400 tickets with the date the ticket last transitioned to a certain status.
I am new to Scriptrunner, but think I have got close to a solution with the help of the Atlassian Community. However, I see that I shouldn't use issue.store(), so I'm now struggling to find the correct way to save the date in the resolution date field.
Is anyone able to suggest the correct way to do this please?
Many Thanks
Vikki
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.search.SearchQuery
// Issues returned from that JQL will get altered
final searchQuery = "key = ABC-123" //There will be more!
// Get some components
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def issueManager = ComponentAccessor.issueManager
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
// Perform the JQL search
def query = jqlQueryParser.parseQuery(searchQuery)
def searchResults = searchProvider.search(SearchQuery.create(query, user), PagerFilter.unlimitedFilter)
// Iterate all results
searchResults.results.each { documentIssue ->
def key = documentIssue.document.fields.find { it.name() == "key" }.stringValue()
def issue = issueManager.getIssueObject(key)
//Find the date of last transition to desired status
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def created = changeHistoryManager.getChangeItemsForField(issue, "status").reverse().find {
it.toString == "In Progress"
}?.getCreated()
// The new value to set
final newValue = created
issue.setResolutionDate(newValue)
issue.store()
}
Hi @Vikki Short
please try something like this:
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.query.Query
import java.sql.Timestamp
String query = "key = ABC-123"
String statusName = "In Progress"
IssueManager issueManager = ComponentAccessor.getIssueManager()
JqlQueryParser jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
ChangeHistoryManager changeHistoryManager = changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
Query searchQuery = jqlQueryParser.parseQuery(query)
SearchResults<Issue> results = searchService.search(user, searchQuery, PagerFilter.getUnlimitedFilter())
List<Issue> foundIssues = results.getResults()
foundIssues.each { Issue foundIssue ->
MutableIssue issue = issueManager.getIssueObject(foundIssue.getId())
Timestamp created = changeHistoryManager.getChangeItemsForField(issue, "status").reverse().find {
it.toString == statusName
}?.getCreated()
issue.setResolutionDate(created)
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}
But please, be aware, that I didn't tested it. You will probably need to perform reindex after the bulk change.
Thanks for your suggestion Hana, but it sounds like issueManager.updateIssue doesn't work in this case. This is the reply I got from Adaptavist:
I tried with the issueManager.updateIssuemethod, with additional code below for my testing:
import com.atlassian.jira.event.type.EventDispatchOption def issueUpdated = issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false) log.warn "output: " + issueUpdatedHowever, issueManager.updateIssue does not work for setResolutionDate.setResolutionDate only works using the issue.store() method. It does work well for other fields like setSummary()
On advice from Adaptavist, I have therefore proceeded to use the script in my original post.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
I have a similar kind of requirement where I need to update the set of tickets on the basis of last transition date. I tried the above the script after modifying few bits but it's not working.
Is anyone able to suggest the correct way to do this please?
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.search.SearchQuery
//import com.atlassian.jira.issue.MutableIssue
// Issues returned from that JQL will get altered
final searchQuery = "key in ()"
// Get some components
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def issueManager = ComponentAccessor.issueManager
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
// Perform the JQL search
def query = jqlQueryParser.parseQuery(searchQuery)
def searchResults = searchProvider.search(SearchQuery.create(query, user), PagerFilter.unlimitedFilter)
// Iterate all results
searchResults.results.each { documentIssue ->
def key = documentIssue.document.fields.find { it.name() == "key" }.stringValue()
def issue = issueManager.getIssueObject(key)
//Find the date of last transition to desired status
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def created = changeHistoryManager.getChangeItemsForField(issue, "status").reverse().find {
}?.getCreated()
// The new value to set
final newValue = created
issue.setResolutionDate(newValue)
issue.store()
//ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
}
Regards,
Rituresh Verma
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Vikki Short ,
It is not a coding help but maybe the free app Status Time Free can help. It displays status transition dates in issue detail page.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for the suggestion Bloompeak Support, but having now fully tested the script in the original post, it looks like we can use that.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Vikki Short ,
could you be so kind as to share your tested script? I´m currently facing the same Issue.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I ended up using the script I added in my original post (i.e. using "issue.store()"). It worked and I didn't have any problems subsequently.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.