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,466,614
Community Members
 
Community Events
176
Community Groups

Update issue history after copy attachment via API

Edited

I used Jira API to write a function to copy attachments to another issue like below:

public void copyAttachmentsToAnotherIssueWithKey(
final String targetIssueKey,
final List<Long> attachmentIds
) {
ApplicationUser currentUser = jiraAuthenticationContext.getLoggedInUser();
List<Attachment> attachments = attachmentIds.stream().map(attachmentManager::getAttachment).collect(Collectors.toList());
attachments.forEach(attachment -> attachmentManager.copyAttachment(attachment, currentUser, targetIssueKey));
}

 It copied successfully, but in the 'History' of the target issue, there is no update.

attachmenthistopynotfound.png

Furthermore, when I used JQL search with 'attachments is not EMPTY', the target issue was not in the results although the attachments already exist in the target issue.

Could you provide me with a solution? Thanks.

4 answers

I found the solution to update the changelog with ChangeItemBean and IssueUpdater.

Issue targetIssue = issueManager.getIssueObject(targetIssueKey);
attachments.forEach(attachment -> {
attachmentManager.copyAttachment(attachment, currentUser, targetIssueKey);
Issue issue = attachment.getIssueObject();
List<ChangeItemBean> changeItemBeans = changeHistoryManager.getChangeItemsForField(issue, "Attachment");
if (!changeItemBeans.isEmpty()) {
ChangeItemBean latestChangeItemBean = changeItemBeans.get(changeItemBeans.size() - 1);
ChangeItemBean newChangeItemBean = new ChangeItemBean();
newChangeItemBean.setFieldType(ChangeItemBean.STATIC_FIELD);
newChangeItemBean.setField("Attachment");
newChangeItemBean.setFrom(latestChangeItemBean.getFrom());
newChangeItemBean.setFromString(latestChangeItemBean.getFromString());
newChangeItemBean.setTo(attachment.getId().toString());
newChangeItemBean.setToString(attachment.getFilename());
newChangeItemBean.setCreated(new Timestamp(System.currentTimeMillis()));
List<ChangeItemBean> newChangeItemBeans = new ArrayList<>();
newChangeItemBeans.add(newChangeItemBean);

IssueUpdateBean issueUpdateBean = new IssueUpdateBean(targetIssue, targetIssue, EventType.ISSUE_UPDATED_ID, currentUser);
issueUpdateBean.setChangeItems(newChangeItemBeans);
issueUpdateBean.setDispatchEvent(true);
issueUpdateBean.setParams(MapBuilder.build("eventsource", IssueEventSource.ACTION));
issueUpdater.doUpdate(issueUpdateBean, true);
}
});

Thanks, everybody for taking a look at my problem.

Hi @Anh Trinh , can you explain how to get IssueUpdater? I am writing a listener and need to create an entry in the history like you do.

0 votes
Pramodh M Community Leader Jan 03, 2022

Great!! @Anh Trinh 

Hi @Pramodh M

Thanks for your reply. The context is instead of manually adding attachments, I use the above code with:

attachmentManager.copyAttachment

This method is used to copy attachments to another issue, but this method did not update the history of issues. I tried to find other methods to update history but there is no result. Do we have any method that can copy and update history in Jira API?

Furthermore, does the history affect the result of the JQL search? Because when I search with 'attachments is not EMPTY', although the attachments already exist after copying by code, I still did not see that issue in the search result.

Thanks,

Anh.

0 votes
Pramodh M Community Leader Dec 31, 2021

Hi @Anh Trinh 

I do not have the context, it will surely update when you include this in the transition.

Include this in the transition, so that issue history is updated as well.

Thanks,
Pramodh

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events