From 473c7a97b482f349f2b5e61c9625b0497a25dbaa Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Thu, 7 Jul 2022 17:10:42 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=B5=8B=E8=AF=95=E8=B7=9F=E8=B8=AA):=20?= =?UTF-8?q?=E5=90=8C=E6=AD=A5jira=E7=BC=BA=E9=99=B7=E9=9D=9E=E6=8F=8F?= =?UTF-8?q?=E8=BF=B0=E5=AD=97=E6=AE=B5=E7=9A=84=E5=AF=8C=E6=96=87=E6=9C=AC?= =?UTF-8?q?=E6=A1=86=E5=9B=BE=E7=89=87=E6=9C=AA=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1014666 --user=陈建星 【测试跟踪】github#15514 jira自定义富文本环境字段添加图片后,同步到ms缺陷图片未显示 https://www.tapd.cn/55049933/s/1197257 --- .../track/issue/AbstractIssuePlatform.java | 11 ++- .../metersphere/track/issue/JiraPlatform.java | 73 +++++++++++++------ 2 files changed, 59 insertions(+), 25 deletions(-) diff --git a/backend/src/main/java/io/metersphere/track/issue/AbstractIssuePlatform.java b/backend/src/main/java/io/metersphere/track/issue/AbstractIssuePlatform.java index 91adedc534..8213c1812b 100644 --- a/backend/src/main/java/io/metersphere/track/issue/AbstractIssuePlatform.java +++ b/backend/src/main/java/io/metersphere/track/issue/AbstractIssuePlatform.java @@ -407,14 +407,19 @@ public abstract class AbstractIssuePlatform implements IssuesPlatform { } protected String syncIssueCustomField(String customFieldsStr, JSONObject issue) { + List customFieldItemDTOList = syncIssueCustomFieldList(customFieldsStr, issue); + return JSONObject.toJSONString(customFieldItemDTOList); + } + + protected List syncIssueCustomFieldList(String customFieldsStr, JSONObject issue) { List customFields = CustomFieldService.getCustomFields(customFieldsStr); Set names = issue.keySet(); customFields.forEach(item -> { String fieldName = item.getCustomData(); Object value = issue.get(fieldName); if (value != null) { - if (value instanceof JSONObject) { - item.setValue(getSyncJsonParamValue(value)); + if (value instanceof JSONObject) { + item.setValue(getSyncJsonParamValue(value)); } else if (value instanceof JSONArray) { List values = new ArrayList<>(); ((JSONArray)value).forEach(attr -> { @@ -436,7 +441,7 @@ public abstract class AbstractIssuePlatform implements IssuesPlatform { } } }); - return JSONObject.toJSONString(customFields); + return customFields; } @Override 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 295c6bb944..0d3eec3f90 100644 --- a/backend/src/main/java/io/metersphere/track/issue/JiraPlatform.java +++ b/backend/src/main/java/io/metersphere/track/issue/JiraPlatform.java @@ -77,8 +77,31 @@ public class JiraPlatform extends AbstractIssuePlatform { JSONObject fields = jiraIssue.getFields(); String status = getStatus(fields); - String description = dealWithDescription(fields.getString("description"), fields.getJSONArray("attachment")); + Map fileContentMap = getContextMap(fields.getJSONArray("attachment")); + + // 先转换下desc的图片 + String description = dealWithDescription(fields.getString("description"), fileContentMap); fields.put("description", description); + CustomFieldItemDTO descItem = null; + List customFieldItems = syncIssueCustomFieldList(issue.getCustomFields(), jiraIssue.getFields()); + + // 其他自定义里有富文本框的也转换下图片 + for (CustomFieldItemDTO item : customFieldItems) { + if (StringUtils.equals("description", item.getId())) { + // desc转过了,跳过 + descItem = item; + } else { + if (StringUtils.equals(CustomFieldType.RICH_TEXT.getValue(), item.getType())) { + item.setValue(dealWithDescription((String) item.getValue(), fileContentMap)); + } + } + } + + // 剩下的附件就是非富文本框的附件 + description = appendMoreImage(description, fileContentMap); + if (descItem != null) { + descItem.setValue(description); + } JSONObject assignee = (JSONObject) fields.get("assignee"); issue.setTitle(fields.getString("summary")); @@ -88,34 +111,15 @@ public class JiraPlatform extends AbstractIssuePlatform { issue.setDescription(description); issue.setPlatformStatus(status); issue.setPlatform(key); - issue.setCustomFields(syncIssueCustomField(issue.getCustomFields(), jiraIssue.getFields())); + issue.setCustomFields(JSONObject.toJSONString(customFieldItems)); return issue; } - private String dealWithDescription(String description, JSONArray attachments) { + private String dealWithDescription(String description, Map fileContentMap) { if (StringUtils.isBlank(description)) { return description; } - // 附件处理 - Map fileContentMap = new HashMap<>(); - if (CollectionUtils.isNotEmpty(attachments)) { - for (int i = 0; i < attachments.size(); i++) { - JSONObject attachment = attachments.getJSONObject(i); - String filename = attachment.getString("filename"); - String content = attachment.getString("content"); - content = "/resource/md/get/url?platform=Jira&url=" + URLEncoder.encode(content, StandardCharsets.UTF_8); - - if (StringUtils.contains(attachment.getString("mimeType"), "image")) { - String contentUrl = "![" + filename + "](" + content + ")"; - fileContentMap.put(filename, contentUrl); - } else { - String contentUrl = "附件[" + filename + "]下载地址:" + content; - fileContentMap.put(filename, contentUrl); - } - } - } - String[] splitStrs = description.split("\\n\\n"); for (int j = 0; j < splitStrs.length; j++) { String splitStr = splitStrs[j]; @@ -138,7 +142,10 @@ public class JiraPlatform extends AbstractIssuePlatform { } } } + return description; + } + private String appendMoreImage(String description, Map fileContentMap) { for (String key: fileContentMap.keySet()) { // 同步jira上传的附件 description += "\n" + fileContentMap.get(key); @@ -146,6 +153,28 @@ public class JiraPlatform extends AbstractIssuePlatform { return description; } + private Map getContextMap(JSONArray attachments) { + // 附件处理 + Map fileContentMap = new HashMap<>(); + if (CollectionUtils.isNotEmpty(attachments)) { + for (int i = 0; i < attachments.size(); i++) { + JSONObject attachment = attachments.getJSONObject(i); + String filename = attachment.getString("filename"); + String content = attachment.getString("content"); + content = "/resource/md/get/url?platform=Jira&url=" + URLEncoder.encode(content, StandardCharsets.UTF_8); + + if (StringUtils.contains(attachment.getString("mimeType"), "image")) { + String contentUrl = "![" + filename + "](" + content + ")"; + fileContentMap.put(filename, contentUrl); + } else { + String contentUrl = "附件[" + filename + "]下载地址:" + content; + fileContentMap.put(filename, contentUrl); + } + } + } + return fileContentMap; + } + private String getStatus(JSONObject fields) { JSONObject statusObj = (JSONObject) fields.get("status"); if (statusObj != null) {