Script to update resolution code with the date of last transition to a status

Vikki Short November 17, 2020

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()

}

 

2 answers

0 votes
Bloompeak Support
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
November 17, 2020

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.

Vikki Short November 25, 2020

Thanks for the suggestion Bloompeak Support, but having now fully tested the script in the original post, it looks like we can use that.

Nyenty_ Tabe April 18, 2024

Hi @Vikki Short ,

could you be so kind as to share your tested script? I´m currently facing the same Issue.

 

Like Bloompeak Support likes this
Vikki Short April 18, 2024

Hi @Nyenty_ Tabe 

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.

0 votes
Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 17, 2020

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.

Vikki Short November 25, 2020

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: " + issueUpdated

However, 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.

Like Heiko Mueller likes this
Rituresh Verma December 8, 2021

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

Suggest an answer

Log in or Sign up to answer