From 02c812fe86310dec4742d1bf2e9d615bfb39afd5 Mon Sep 17 00:00:00 2001 From: song-cc-rock Date: Fri, 22 Jul 2022 10:50:51 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=B5=8B=E8=AF=95=E8=B7=9F=E8=B8=AA):?= =?UTF-8?q?=20=E7=BC=BA=E9=99=B7=E7=AE=A1=E7=90=86=E5=90=8C=E6=AD=A5Jira?= =?UTF-8?q?=E5=B9=B3=E5=8F=B0=E9=99=84=E4=BB=B6=E7=9A=84=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --story=1008034 --user=宋昌昌 【Bug转需求】[缺陷管理]-github#9580-jira集成,缺陷模版使用 jira 缺陷模版,字段没有同步全 https://www.tapd.cn/55049933/s/1206944 --- .../metersphere/track/issue/JiraPlatform.java | 57 ++++++++++++------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/backend/src/main/java/io/metersphere/track/issue/JiraPlatform.java b/backend/src/main/java/io/metersphere/track/issue/JiraPlatform.java index 703a77b0bb..8e7bd1d329 100644 --- a/backend/src/main/java/io/metersphere/track/issue/JiraPlatform.java +++ b/backend/src/main/java/io/metersphere/track/issue/JiraPlatform.java @@ -862,32 +862,51 @@ public class JiraPlatform extends AbstractIssuePlatform { } public void syncJiraIssueAttachments(IssuesWithBLOBs issue, JiraIssue jiraIssue) { + List jiraAttachmentsName = new ArrayList(); AttachmentRequest request = new AttachmentRequest(); request.setBelongType(AttachmentType.ISSUE.type()); request.setBelongId(issue.getId()); - attachmentService.deleteAttachment(request); + List allMsAttachments = attachmentService.listMetadata(request); + List msAttachmentsName = allMsAttachments.stream().map(FileAttachmentMetadata::getName).collect(Collectors.toList()); JSONArray attachments = jiraIssue.getFields().getJSONArray("attachment"); - if (CollectionUtils.isEmpty(attachments)) { - return; - } - for (int i = 0; i < attachments.size(); i++) { - JSONObject attachment = attachments.getJSONObject(i); - String filename = attachment.getString("filename"); - if ((issue.getDescription() == null || !issue.getDescription().contains(filename)) - && (issue.getCustomFields() == null || !issue.getCustomFields().contains(filename))) { - try { - byte[] content = jiraClientV2.getAttachmentContent(attachment.getString("content")); - FileAttachmentMetadata fileAttachmentMetadata = fileService.saveAttachmentByBytes(content, AttachmentType.ISSUE.type(), issue.getId(), filename); - AttachmentModuleRelation attachmentModuleRelation = new AttachmentModuleRelation(); - attachmentModuleRelation.setAttachmentId(fileAttachmentMetadata.getId()); - attachmentModuleRelation.setRelationId(issue.getId()); - attachmentModuleRelation.setRelationType(AttachmentType.ISSUE.type()); - attachmentModuleRelationMapper.insert(attachmentModuleRelation); - } catch (Exception e) { - LogUtil.error(e); + // 同步Jira中新的附件 + if (CollectionUtils.isNotEmpty(attachments)) { + for (int i = 0; i < attachments.size(); i++) { + JSONObject attachment = attachments.getJSONObject(i); + String filename = attachment.getString("filename"); + jiraAttachmentsName.add(filename); + if ((issue.getDescription() == null || !issue.getDescription().contains(filename)) + && (issue.getCustomFields() == null || !issue.getCustomFields().contains(filename)) + && !msAttachmentsName.contains(filename)) { + try { + byte[] content = jiraClientV2.getAttachmentContent(attachment.getString("content")); + FileAttachmentMetadata fileAttachmentMetadata = fileService.saveAttachmentByBytes(content, AttachmentType.ISSUE.type(), issue.getId(), filename); + AttachmentModuleRelation attachmentModuleRelation = new AttachmentModuleRelation(); + attachmentModuleRelation.setAttachmentId(fileAttachmentMetadata.getId()); + attachmentModuleRelation.setRelationId(issue.getId()); + attachmentModuleRelation.setRelationType(AttachmentType.ISSUE.type()); + attachmentModuleRelationMapper.insert(attachmentModuleRelation); + } catch (Exception e) { + LogUtil.error(e); + } } } } + + // 删除Jira中不存在的附件 + if (CollectionUtils.isNotEmpty(allMsAttachments)) { + List deleteMsAttachments = allMsAttachments.stream() + .filter(msAttachment -> !jiraAttachmentsName.contains(msAttachment.getName())).collect(Collectors.toList()); + deleteMsAttachments.forEach(fileAttachmentMetadata -> { + List ids = List.of(fileAttachmentMetadata.getId()); + AttachmentModuleRelationExample example = new AttachmentModuleRelationExample(); + example.createCriteria().andAttachmentIdIn(ids).andRelationTypeEqualTo(AttachmentType.ISSUE.type()); + // 删除MS附件及关联数据 + fileService.deleteAttachment(ids); + fileService.deleteFileAttachmentByIds(ids); + attachmentModuleRelationMapper.deleteByExample(example); + }); + } } public void syncJiraRichTextAttachment(IssuesUpdateRequest request) {