feat: jira同步缺陷自定义字段

This commit is contained in:
chenjianxing 2021-09-24 18:22:07 +08:00 committed by jianxing
parent ab3c42e2f0
commit eee28615e6
3 changed files with 52 additions and 18 deletions

View File

@ -6,7 +6,7 @@ import lombok.Data;
public class CustomFieldItemDTO { public class CustomFieldItemDTO {
private String id; private String id;
private String name; private String name;
private String value; private Object value;
private String type; private String type;
private String customData; private String customData;
} }

View File

@ -354,13 +354,8 @@ public abstract class AbstractIssuePlatform implements IssuesPlatform {
List<CustomFieldItemDTO> customFields = getCustomFields(issuesRequest.getCustomFields()); List<CustomFieldItemDTO> customFields = getCustomFields(issuesRequest.getCustomFields());
customFields.forEach(item -> { customFields.forEach(item -> {
if (StringUtils.isNotBlank(item.getCustomData())) { if (StringUtils.isNotBlank(item.getCustomData())) {
if (StringUtils.isNotBlank(item.getType()) &&
StringUtils.equalsAny(item.getType(), "multipleInput") && StringUtils.isNotBlank(item.getValue())) {
paramMap.put(item.getCustomData(), JSONArray.parseArray(item.getValue()));
} else {
paramMap.add(item.getCustomData(), item.getValue()); paramMap.add(item.getCustomData(), item.getValue());
} }
}
}); });
} }
} }

View File

@ -79,7 +79,7 @@ public class JiraPlatform extends AbstractIssuePlatform {
return issues; return issues;
} }
public void parseIssue(IssuesWithBLOBs item, JiraIssue jiraIssue) { public void parseIssue(IssuesWithBLOBs item, JiraIssue jiraIssue, String customFieldsStr) {
String lastmodify = ""; String lastmodify = "";
String status = ""; String status = "";
JSONObject fields = jiraIssue.getFields(); JSONObject fields = jiraIssue.getFields();
@ -89,9 +89,11 @@ public class JiraPlatform extends AbstractIssuePlatform {
String description = fields.getString("description"); String description = fields.getString("description");
Parser parser = Parser.builder().build(); Parser parser = Parser.builder().build();
if (StringUtils.isNotBlank(description)) {
Node document = parser.parse(description); Node document = parser.parse(description);
HtmlRenderer renderer = HtmlRenderer.builder().build(); HtmlRenderer renderer = HtmlRenderer.builder().build();
description = renderer.render(document); description = renderer.render(document);
}
if (assignee != null) { if (assignee != null) {
lastmodify = assignee.getString("displayName"); lastmodify = assignee.getString("displayName");
@ -103,6 +105,43 @@ public class JiraPlatform extends AbstractIssuePlatform {
item.setDescription(description); item.setDescription(description);
item.setPlatformStatus(status); item.setPlatformStatus(status);
item.setPlatform(IssuesManagePlatform.Jira.toString()); item.setPlatform(IssuesManagePlatform.Jira.toString());
item.setCustomFields(parseIssueCustomField(customFieldsStr, jiraIssue));
}
public String parseIssueCustomField(String customFieldsStr, JiraIssue jiraIssue) {
List<CustomFieldItemDTO> customFields = getCustomFields(customFieldsStr);
JSONObject fields = jiraIssue.getFields();
customFields.forEach(item -> {
String fieldName = item.getCustomData();
Object value = fields.get(fieldName);
if (value != null) {
if (value instanceof JSONObject) {
if (!fieldName.equals("assignee") && !fieldName.equals("reporter")) { // 获取不到账号名
item.setValue(((JSONObject)value).getString("id"));
}
} else {
if (StringUtils.isNotBlank(item.getType()) &&
StringUtils.equalsAny(item.getType(), "multipleSelect", "checkbox", "multipleMember")) {
List<String> values = new ArrayList<>();
if (item.getValue() != null) {
JSONArray attrs = (JSONArray) item.getValue();
attrs.forEach(attr -> {
if (attr instanceof JSONObject) {
values.add(((JSONObject)attr).getString("id"));
} else {
values.add((String) attr);
}
});
}
item.setValue(values);
} else {
item.setValue(value);
}
}
}
});
return JSONObject.toJSONString(customFields);
} }
private String getStatus(JSONObject fields) { private String getStatus(JSONObject fields) {
@ -253,7 +292,7 @@ public class JiraPlatform extends AbstractIssuePlatform {
customFields.forEach(item -> { customFields.forEach(item -> {
String fieldName = item.getCustomData(); String fieldName = item.getCustomData();
if (StringUtils.isNotBlank(fieldName)) { if (StringUtils.isNotBlank(fieldName)) {
if (StringUtils.isNotBlank(item.getValue())) { if (item.getValue() != null) {
if (StringUtils.isNotBlank(item.getType()) && if (StringUtils.isNotBlank(item.getType()) &&
StringUtils.equalsAny(item.getType(), "select", "radio", "member")) { StringUtils.equalsAny(item.getType(), "select", "radio", "member")) {
JSONObject param = new JSONObject(); JSONObject param = new JSONObject();
@ -266,8 +305,8 @@ public class JiraPlatform extends AbstractIssuePlatform {
} else if (StringUtils.isNotBlank(item.getType()) && } else if (StringUtils.isNotBlank(item.getType()) &&
StringUtils.equalsAny(item.getType(), "multipleSelect", "checkbox", "multipleMember")) { StringUtils.equalsAny(item.getType(), "multipleSelect", "checkbox", "multipleMember")) {
JSONArray attrs = new JSONArray(); JSONArray attrs = new JSONArray();
if (StringUtils.isNotBlank(item.getValue())) { if (item.getValue() != null) {
JSONArray values = JSONObject.parseArray(item.getValue()); JSONArray values = (JSONArray)item.getValue();
values.forEach(v -> { values.forEach(v -> {
JSONObject param = new JSONObject(); JSONObject param = new JSONObject();
param.put("id", v); param.put("id", v);
@ -275,9 +314,6 @@ public class JiraPlatform extends AbstractIssuePlatform {
}); });
} }
fields.put(fieldName, attrs); fields.put(fieldName, attrs);
} else if (StringUtils.isNotBlank(item.getType()) &&
StringUtils.equalsAny(item.getType(), "multipleInput") && StringUtils.isNotBlank(item.getValue())) {
fields.put(fieldName, JSONArray.parseArray(item.getValue()));
} else { } else {
fields.put(fieldName, item.getValue()); fields.put(fieldName, item.getValue());
} }
@ -330,11 +366,12 @@ public class JiraPlatform extends AbstractIssuePlatform {
setConfig(); setConfig();
try { try {
IssuesWithBLOBs issuesWithBLOBs = issuesMapper.selectByPrimaryKey(item.getId()); IssuesWithBLOBs issuesWithBLOBs = issuesMapper.selectByPrimaryKey(item.getId());
parseIssue(item, jiraClientV2.getIssues(item.getId())); parseIssue(item, jiraClientV2.getIssues(item.getId()), issuesWithBLOBs.getCustomFields());
String desc = htmlDesc2MsDesc(item.getDescription()); String desc = htmlDesc2MsDesc(item.getDescription());
// 保留之前上传的图片 // 保留之前上传的图片
String images = getImages(issuesWithBLOBs.getDescription()); String images = getImages(issuesWithBLOBs.getDescription());
item.setDescription(desc + "\n" + images); item.setDescription(desc + "\n" + images);
issuesMapper.updateByPrimaryKeySelective(item); issuesMapper.updateByPrimaryKeySelective(item);
} catch (HttpClientErrorException e) { } catch (HttpClientErrorException e) {
if (e.getRawStatusCode() == 404) { if (e.getRawStatusCode() == 404) {
@ -342,6 +379,8 @@ public class JiraPlatform extends AbstractIssuePlatform {
item.setPlatformStatus(IssuesStatus.DELETE.toString()); item.setPlatformStatus(IssuesStatus.DELETE.toString());
issuesMapper.deleteByPrimaryKey(item.getId()); issuesMapper.deleteByPrimaryKey(item.getId());
} }
} catch (Exception e) {
LogUtil.error(e);
} }
}); });
} }