How to update issue history using java api?

Ahmed Trabelsi May 26, 2020

Hi,

I have to update issue's change history using java api. I succeeded to retrieve the ChangeItemBean via changeHistoryManager.getChangeItemsForField

However when setting the new value using setToString(), the update is not populated in the history.

Any idea?

Thanks

1 answer

0 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 26, 2020

I don't know if it's possible to change the history.

You can definitely read the history.
There is a method to clear the history.
And you can add to the history using the ChangeLogUtils.

But there is no updateChangeItem method to store/persist the ChangeItemBean that you've attempted to modify.

At best, you can retrieve a changeItem Bean, modify it, then store it as a new ChangeGroup/ChangeItem. But the old one will still be there.

Ahmed Trabelsi May 28, 2020

Hi Peter,

Thanks for you reply

The goal behind history update is migrating duplicated storypoint to the default one. 

Any idea how to achieve that kind of migration?

Regards,

Ahmed

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 28, 2020

Sorry, I don't know what you mean. Can you give more details?

Ahmed Trabelsi June 2, 2020

Hi Peter,

The goal is to update the following history item by replacing story points obsolete 1,  by story point (default one)

 

story point.jpg

Thanks

Ahmed

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 2, 2020

The only way to change this I think would be through making changes directly in the database and that's never recommended.

But presumably, this means that you had a customfield called story points that you renamed "story points_obsolete" but you now also have the default "story points" field created by jira software. After the new field was created, all your issues would have had to be converted from the old to the new field. The entry (New story points going from null to 7 should be listed). I am not sure what you think changing the field label in the history will do, but wouldn't it be best to show the entire actual history? 

Ahmed Trabelsi June 8, 2020

Hi Peter,

From your point of view, what will be the best approach to copy all items history from from old to new field?

Thanks

Ahmed

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 8, 2020

History item are associated with an issue object, not a custom field. The "fieldname" is just an attribute of a change item and has no direct link to that field.

My recommendation is to no copy the history. The only thing I would be comfortable recommended with regards to the issue history is to insert a single history change item that would document the change in the field name.

Something like

Field                  |   Old Value                    |    New Value
Field Replaced  |    story points_obsolete |    Story Points

This would allow a user to to follow the chronology of events

I_L_I_A March 19, 2021

Hello Peter.
So how would one append to issue's history? I haven't found any method in java doc yet.

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 19, 2021

You'll want to use the ChangeLogUtils for that

Here is a couple of sample functions that uses that:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.history.ChangeLogUtils
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.history.ChangeItemBean
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.event.issue.IssueEventBundleFactory
import com.atlassian.jira.user.ApplicationUser
import org.ofbiz.core.entity.GenericValue

def createChangeLog(Issue issue, String fieldName,String oldValue,String newValue,ApplicationUser currentUser,String eventName,String commentBody="",Map params = [:]) {
def changeHolder = new DefaultIssueChangeHolder();
def changeItemBean = new ChangeItemBean(ChangeItemBean.CUSTOM_FIELD, fieldName, "", oldValue, "", newValue)
changeHolder.addChangeItem(changeItemBean);
def changeGroup = ChangeLogUtils.createChangeGroup(currentUser, issue, issue, changeHolder.getChangeItems(), false);
if(eventName){
dispatchEvent(issue, currentUser, eventName, changeGroup, commentBody, params)
}
}
def dispatchEvent(Issue issue,ApplicationUser currentUser,String eventName, GenericValue changegroup,String commentBody = "",Map params = [:]) {
def eventFactory = ComponentAccessor.getComponent(IssueEventBundleFactory.class)
def eventId = ComponentAccessor.eventTypeManager.eventTypes.find { it.name == eventName }?.id
if(eventId){
log.debug "Dispatching event $eventName ($eventId)"
def comment
if(commentBody) {
comment = ComponentAccessor.commentManager.create(issue, currentUser, commentBody, false)
}
def eventBundle = eventFactory.wrapInBundle(new IssueEvent(issue, currentUser, comment, null, changegroup, params, eventId, true))
//issueEventManager.dispatchEvent(eventBundle)
ComponentAccessor.issueEventManager.dispatchEvent(eventId,issue, currentUser, comment,null, changegroup,params, true)
} else {
log.warn "No event found matching name: $eventName"
}
}

def issue = ComponentAccessor.issueManager.getIssueObject('JSP-1922')
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
createChangeLog(issue, "dummy field", "old dummy value", "new dummy value", currentUser, 'Issue Updated')

This will produce a history item that looks like this:

history.png

Like I_L_I_A likes this
Sai Sanjay Bandarupalli_a August 5, 2022

Hello Peter,

         I am beginner in JIRA. So I wanted to know if there is any stable platform to connect from Java to JIRA without using REST API? Can I use native Java libraries or any other additional libraries to connect to JIRA?

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 5, 2022

The normal way to connect to Jira from Java is to create a plugin using the atlassian SDK.

If you want to connect to a Jira instance from outside of Jira then REST API is the only way.

There is a Java wrapper for the REST API, but I've never used it and I'm not a fan.

Kim Tso November 18, 2022

Is it possible to create changegroup using changeLogUtils with a specified timestamp? I can create the changeItemBean with timestamp. Unfortunately, when I create a changeGroup using the list of changeItemBean created, it make me a changegroup to now, but not the timestamp in changeItemBean 

Peter-Dave Sheehan
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 18, 2022

I don't know. I never tried.

Suggest an answer

Log in or Sign up to answer