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.
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.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Great!! @Anh Trinh
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.