Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
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

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.

2 comments

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

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.

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.

Like Pramodh M likes this
Pramodh M Community Leader Jan 03, 2022

Great!! @Anh Trinh 

Comment

Log in or Sign up to comment
TAGS
Community showcase
Published in Jira

Do you use Jira Cloud with Microsoft Teams?

Hi, Jira users! Do you use Jira alongside Microsoft Teams? We want to hear how you’ve used the power of Jira Cloud and Microsoft Teams (via the Jira Cloud for Microsoft Teams app) to achieve a team...

470 views 3 7
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