Update issue history after copy attachment via API

Anh Trinh December 31, 2021

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

1 vote
Anh Trinh January 3, 2022

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.

Adolfo Casari
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.
November 14, 2022

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
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 3, 2022

Great!! @Anh Trinh 

0 votes
Anh Trinh January 2, 2022

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
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 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
AUG Leaders

Atlassian Community Events