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

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

 

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Marketplace Apps & Integrations

Do Not Lose your Customer’s Trust

Missing deadlines is one of the biggest problems every team lead wants to avoid when dealing with managed services. When the customer contracts your company to help with IT services it is expected th...

289 views 0 1
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