Script runner - Store issue change history in a custom field

Sanu Soman
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.
August 14, 2015

HI All,

Could you please let me know how to store the issue change history like transition time, tansition from, tansition to and the comment to a free text field?

I guess its possible by script runner in post function. Could you please share some sample script to get this info.

Thanks,

Sanu P Soman

6 answers

1 vote
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 31, 2015

Hi Sanu,

Below I have attached a sample script wrote for JIRA 6.4 which will get the change history information for the previous transition. This script should be placed inside a Scripted Field.

Please note if you are using this on a different version of JIRA then you may need to update this slightly.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.core.util.DateUtils

// Get access to the required issue managers
def componentManager = ComponentManager.getInstance()
def issueManager = componentManager.getIssueManager()
def changeHistoryManager = ComponentManager.getInstance().getChangeHistoryManager()

// Get the current User
def User = ComponentAccessor.getJiraAuthenticationContext().getUser()

// Get the current Issue
SubjectIssue = issue

// Get Change History information
def currentStatus = issue.getStatusObject().getName();
def previousStatus = "In Progress"
def updated = issue.getUpdated().format("HH:mm (dd MMM yyyy)")
String previousTransitison = changeHistoryManager.getChangeItemsForField(issue, "status").getAt(changeHistoryManager.getChangeItemsForField(issue, "status").size() - 1).toString()
// Extract the from and to statuses below
String fromStatus = previousTransitison.substring(previousTransitison.indexOf("fromString="), previousTransitison.indexOf(",to="));
String fromStatusVal = fromStatus.substring(11);
String toStatus = previousTransitison.substring(previousTransitison.indexOf("toString="), previousTransitison.indexOf(",created"));
String toStatusVal = toStatus.substring(9);
def previousTransitisonVal = fromStatusVal + " --> " + toStatusVal

// Construct the format to be output
def TransitionInfo = "Current Logged In User = " + User.getDisplayName() + "<br/>" + "Last Updated Time = " + updated  + "<br/>" + "Curent Status = " + currentStatus + "<br/>" + "Previous Transition Info = " + previousTransitisonVal

return TransitionInfo

 

Also the Issue History Plugin may be of use to you.

 

I hope this helps

 

Kristian

0 votes
Sanu Soman
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.
August 18, 2015

Basically I want to store transition information like current user, from status, to status and time to a unlimited free text field.

0 votes
Sanu Soman
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.
August 18, 2015

IssueService? can you please elaborate little bit?

0 votes
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.
August 18, 2015

If you want to store transition information use IssueService to make the issue undergo a transition.

0 votes
Sanu Soman
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.
August 18, 2015

i'm using below script for this but it;s not working. import com.atlassian.jira.bc.JiraServiceContextImpl 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.changehistory.ChangeHistoryManager import com.atlassian.jira.security.JiraAuthenticationContext import com.atlassian.jira.web.bean.PagerFilter import com.atlassian.jira.issue.history.ChangeItemBean ChangeHistoryManager changeHistoryManager = ComponentAccessor.getChangeHistoryManager() SearchService searchService = ComponentAccessor.getComponent(SearchService.class); JiraAuthenticationContext jiraAuthenticationContext = ComponentAccessor.getJiraAuthenticationContext() user = jiraAuthenticationContext.getLoggedInUser() ctx = new JiraServiceContextImpl(user) List<Issue> issues = null List<ChangeItemBean> changeItems changeItems = changeHistoryManager.getAllChangeItems(issue) log.warn (changeItems) logs = changeItems.findAll{it.getField()=='status'}{result += "${it.created}; ${it.getFrom()}; ${it.getTo()}"} log.warn (logs) IssueManager issueManager = ComponentAccessor.getIssueManager(); CustomFieldManager cfm=ComponentAccessor.getCustomFieldManager(); def MeD =cfm.getCustomFieldObjectByName("StatusLog"); issue.setCustomFieldValue(MeD, String.valueOf(logs)); Any suggestions?

0 votes
Sanu Soman
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.
August 16, 2015

Any suggestions?

Suggest an answer

Log in or Sign up to answer