Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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
Hana Kučerová Community Leader Nov 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.

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.

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.

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

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
Community showcase
Published in Jira

Announcing the waitlist for Jira Work Management

Hey there Cloud Community members! We’re excited to give you the first glimpse of the new home for business teams on Jira — Jira Work Management. Jira Work Management is the next generation of J...

866 views 14 20
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you