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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,463,681
Community Members
 
Community Events
176
Community Groups

Safely Deleting Change History Records

Edited

I have a number of derived fields which create noise in the issue change log. I'd like to remove these from the log.

I've had a read of https://community.atlassian.com/t5/Jira-questions/Is-it-possible-to-delete-History-records-of-an-issue-like-the/qaq-p/254441 and take note of the warnings. I've also had a look at the code for com.atlassian.jira.issue.changehistory.removeAllChangeItems(Issue issue) in order to understand what it is doing and adapt it to my needs.

I'm wondering if the following code is the correct/safe way to delete change items, without needing to shutdown and restart the service.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager
import com.atlassian.jira.issue.changehistory.ChangeHistory
import com.atlassian.jira.entity.property.JsonEntityPropertyManager
import com.atlassian.jira.entity.Entity
import com.atlassian.jira.entity.property.EntityPropertyType
import com.atlassian.jira.ofbiz.OfBizDelegator

List<String> fieldsToRemove = ["Field Name 1", "Field Name 2", "Field Name 3"]

ChangeHistoryManager changeHistoryManager =
ComponentAccessor.getChangeHistoryManager()
JsonEntityPropertyManager jsonEntityPropertyManager =
ComponentAccessor.getComponent(JsonEntityPropertyManager)
OfBizDelegator ofBizDelegator = ComponentAccessor.getOfBizDelegator()

List<ChangeHistory> history = changeHistoryManager.getChangeHistories(issue)
history.each{ChangeHistory ch ->
Boolean allDeleted = null
ch.changeItems.each {
Long changeItemId = it.get("id")
String field = it.get("field")
if (fieldsToRemove.contains(field)) {
if (allDeleted==null) {
allDeleted = true
}
ofBizDelegator.removeByAnd(Entity.Name.CHANGE_ITEM,
[id: changeItemId].asImmutable())
} else {
allDeleted = false
}
}

if (allDeleted) {
ofBizDelegator.removeByAnd(Entity.Name.CHANGE_GROUP,
[id: ch.id].asImmutable())
jsonEntityPropertyManager.deleteByEntity(
EntityPropertyType.CHANGE_HISTORY_PROPERTY.getDbEntityName(),
ch.id)
}
}

 

1 answer

Did this end up working out for you? We're considering something similar and testing of your approach seems fine. I'd love to know if you ran into any issues, though.

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events