diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtCustomFieldTemplateMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtCustomFieldTemplateMapper.xml index 92b4aeb05e..742abe654d 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtCustomFieldTemplateMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtCustomFieldTemplateMapper.xml @@ -22,7 +22,7 @@ + diff --git a/backend/src/main/java/io/metersphere/dto/CustomFieldResourceDTO.java b/backend/src/main/java/io/metersphere/dto/CustomFieldResourceDTO.java index 4c7da93d93..99a85c343e 100644 --- a/backend/src/main/java/io/metersphere/dto/CustomFieldResourceDTO.java +++ b/backend/src/main/java/io/metersphere/dto/CustomFieldResourceDTO.java @@ -12,5 +12,6 @@ import java.io.Serializable; @ToString(callSuper = true) public class CustomFieldResourceDTO extends CustomFieldResource implements Serializable { private String name; + private String type; private static final long serialVersionUID = 1L; } diff --git a/backend/src/main/java/io/metersphere/service/CustomFieldService.java b/backend/src/main/java/io/metersphere/service/CustomFieldService.java index b72966c8c8..2f57890656 100644 --- a/backend/src/main/java/io/metersphere/service/CustomFieldService.java +++ b/backend/src/main/java/io/metersphere/service/CustomFieldService.java @@ -237,23 +237,9 @@ public class CustomFieldService { List list = new ArrayList<>(); if (StringUtils.isNotBlank(customFieldsStr)) { if (JSONObject.parse(customFieldsStr) instanceof JSONArray) { - List fieldItems = JSONArray.parseArray(customFieldsStr, CustomFieldItemDTO.class); - for (CustomFieldItemDTO dto : fieldItems) { - // customFieldItemDTO里的id是模版ID - CustomFieldResource resource = new CustomFieldResource(); - CustomFieldTemplate customFieldTemplate = customFieldTemplateMapper.selectByPrimaryKey(dto.getId()); - if (customFieldTemplate == null) { - continue; - } else { - resource.setFieldId(customFieldTemplate.getFieldId()); - } - if (StringUtils.isNotBlank(dto.getType()) - && StringUtils.equalsAny(CustomFieldType.RICH_TEXT.getValue(), CustomFieldType.TEXTAREA.getValue())) { - resource.setTextValue(dto.getValue().toString()); - } else { - resource.setValue(JSONObject.toJSONString(dto.getValue())); - } - list.add(resource); + List items = JSONArray.parseArray(customFieldsStr, CustomFieldItemDTO.class); + for (CustomFieldItemDTO item : items) { + list.add(constructorCustomFieldResource(item)); } return list; } @@ -261,50 +247,15 @@ public class CustomFieldService { return new ArrayList<>(); } - public List getJiraCustomFieldResource(String customFieldsStr, String projectId) { - List list = new ArrayList<>(); - if (StringUtils.isNotBlank(customFieldsStr)) { - if (JSONObject.parse(customFieldsStr) instanceof JSONArray) { - List fieldItems = JSONArray.parseArray(customFieldsStr, CustomFieldItemDTO.class); - for (CustomFieldItemDTO dto : fieldItems) { - CustomFieldResource resource = new CustomFieldResource(); - // customFieldItemDTO里的id是模版ID - CustomFieldTemplate customFieldTemplate = customFieldTemplateMapper.selectByPrimaryKey(dto.getId()); - if (customFieldTemplate == null) { - CustomField customField = customFieldMapper.selectByPrimaryKey(dto.getId()); - if (customField == null) { - CustomField field = new CustomField(); - field.setId(dto.getId()); - field.setUpdateTime(System.currentTimeMillis()); - field.setCreateTime(System.currentTimeMillis()); - field.setGlobal(false); - field.setSystem(false); - field.setName(dto.getName()); - field.setScene(TemplateConstants.FieldTemplateScene.ISSUE.name()); - field.setThirdPart(true); - field.setType(dto.getType()); - field.setProjectId(projectId); - if (StringUtils.isNotBlank(SessionUtils.getUserId())) { - field.setCreateUser(SessionUtils.getUserId()); - } - customFieldMapper.insert(field); - } - resource.setFieldId(dto.getId()); - } else { - resource.setFieldId(customFieldTemplate.getFieldId()); - } - if (StringUtils.isNotBlank(dto.getType()) - && StringUtils.equalsAny(CustomFieldType.RICH_TEXT.getValue(), CustomFieldType.TEXTAREA.getValue())) { - resource.setTextValue(dto.getValue().toString()); - } else { - resource.setValue(JSONObject.toJSONString(dto.getValue())); - } - list.add(resource); - } - return list; - } + private CustomFieldResource constructorCustomFieldResource(CustomFieldItemDTO dto) { + CustomFieldResource resource = new CustomFieldResource(); + resource.setFieldId(dto.getId()); + if (StringUtils.isNotBlank(dto.getType()) && StringUtils.equalsAny(CustomFieldType.RICH_TEXT.getValue(), CustomFieldType.TEXTAREA.getValue())) { + resource.setTextValue(dto.getValue().toString()); + } else { + resource.setValue(JSONObject.toJSONString(dto.getValue())); } - return new ArrayList<>(); + return resource; } public List getByProjectId(String projectId) { 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 f91ad94f1d..ff3f091e83 100644 --- a/backend/src/main/java/io/metersphere/track/issue/AbstractIssuePlatform.java +++ b/backend/src/main/java/io/metersphere/track/issue/AbstractIssuePlatform.java @@ -1,5 +1,6 @@ package io.metersphere.track.issue; +import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import io.metersphere.base.domain.*; @@ -372,7 +373,7 @@ public abstract class AbstractIssuePlatform implements IssuesPlatform { } protected void addCustomFields(IssuesUpdateRequest issuesRequest, MultiValueMap paramMap) { - List customFields = CustomFieldService.getCustomFields(issuesRequest.getCustomFields()); + List customFields = issuesRequest.getRequestFields(); customFields.forEach(item -> { if (StringUtils.isNotBlank(item.getCustomData())) { if (item.getValue() instanceof String) { @@ -417,18 +418,28 @@ public abstract class AbstractIssuePlatform implements IssuesPlatform { 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 -> { - if (attr instanceof JSONObject) { - values.add(getSyncJsonParamValue(attr)); - } else { - values.add(attr); + // Sprint 是单选 同步回来是 JSONArray + if (StringUtils.equals(item.getType(), "select")) { + if (((JSONArray) value).size() > 0) { + Object o = ((JSONArray) value).get(0); + if (o instanceof JSONObject) { + item.setValue(getSyncJsonParamValue(o)); + } } - }); - item.setValue(values); + } else { + List values = new ArrayList<>(); + ((JSONArray) value).forEach(attr -> { + if (attr instanceof JSONObject) { + values.add(getSyncJsonParamValue(attr)); + } else { + values.add(attr); + } + }); + item.setValue(values); + } } else { item.setValue(value); } @@ -487,9 +498,8 @@ public abstract class AbstractIssuePlatform implements IssuesPlatform { protected void mergeCustomField(IssuesWithBLOBs issues, String defaultCustomField) { if (StringUtils.isNotBlank(defaultCustomField)) { - String issuesCustomFields = issues.getCustomFields(); - if (StringUtils.isBlank(issuesCustomFields) || issuesCustomFields.startsWith("{")) issuesCustomFields = "[]"; - JSONArray issueFields = JSONArray.parseArray(issuesCustomFields); + List customFields = extIssuesMapper.getIssueCustomField(issues.getId()); + JSONArray issueFields = JSONArray.parseArray(JSON.toJSONString(customFields)); Set ids = issueFields.stream().map(i -> ((JSONObject) i).getString("id")).collect(Collectors.toSet()); JSONArray defaultFields = JSONArray.parseArray(defaultCustomField); defaultFields.forEach(item -> { // 如果自定义字段里没有模板新加的字段,就把新字段加上 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 1b1b4d6b7b..a2c7574c49 100644 --- a/backend/src/main/java/io/metersphere/track/issue/JiraPlatform.java +++ b/backend/src/main/java/io/metersphere/track/issue/JiraPlatform.java @@ -368,7 +368,7 @@ public class JiraPlatform extends AbstractIssuePlatform { } private void parseCustomFiled(IssuesUpdateRequest issuesRequest, JSONObject fields) { - List customFields = CustomFieldService.getCustomFields(issuesRequest.getCustomFields()); + List customFields = issuesRequest.getRequestFields(); customFields.forEach(item -> { String fieldName = item.getCustomData(); @@ -390,7 +390,7 @@ public class JiraPlatform extends AbstractIssuePlatform { } else if (StringUtils.equalsAny(item.getType(), "multipleSelect", "checkbox", "multipleMember")) { JSONArray attrs = new JSONArray(); if (item.getValue() != null) { - JSONArray values = (JSONArray)item.getValue(); + JSONArray values = JSONArray.parseArray((String) item.getValue()); values.forEach(v -> { JSONObject param = new JSONObject(); param.put("id", v); @@ -402,7 +402,7 @@ public class JiraPlatform extends AbstractIssuePlatform { if (item.getValue() != null) { JSONObject attr = new JSONObject(); if (item.getValue() instanceof JSONArray) { - JSONArray values = (JSONArray) item.getValue(); + JSONArray values = JSONArray.parseArray((String) item.getValue()); if (CollectionUtils.isNotEmpty(values)) { if (values.size() > 0) { attr.put("id", values.get(0)); @@ -418,7 +418,7 @@ public class JiraPlatform extends AbstractIssuePlatform { } fields.put(fieldName, attr); } - } else if (StringUtils.equalsAny(item.getType(), "richText")) { + } else if (StringUtils.equalsAny(item.getType(), "richText")) { fields.put(fieldName, removeImage(item.getValue().toString())); if (fieldName.equals("description")) { issuesRequest.setDescription(item.getValue().toString()); @@ -521,7 +521,7 @@ public class JiraPlatform extends AbstractIssuePlatform { getUpdateIssue(item, jiraClientV2.getIssues(item.getPlatformId())); String customFields = item.getCustomFields(); // 把自定义字段存入新表 - List customFieldResource = customFieldService.getJiraCustomFieldResource(customFields, project.getId()); + List customFieldResource = customFieldService.getCustomFieldResource(customFields); customFieldIssuesService.addFields(item.getId(), customFieldResource); issuesMapper.updateByPrimaryKeySelective(item); } catch (HttpClientErrorException e) { diff --git a/backend/src/main/java/io/metersphere/track/issue/LocalPlatform.java b/backend/src/main/java/io/metersphere/track/issue/LocalPlatform.java index c7ebc4cb6a..fe16dd1b1e 100644 --- a/backend/src/main/java/io/metersphere/track/issue/LocalPlatform.java +++ b/backend/src/main/java/io/metersphere/track/issue/LocalPlatform.java @@ -7,6 +7,7 @@ import io.metersphere.commons.constants.IssuesManagePlatform; import io.metersphere.commons.user.SessionUser; import io.metersphere.commons.utils.BeanUtils; import io.metersphere.commons.utils.SessionUtils; +import io.metersphere.dto.CustomFieldItemDTO; import io.metersphere.track.dto.DemandDTO; import io.metersphere.track.request.testcase.IssuesRequest; import io.metersphere.track.request.testcase.IssuesUpdateRequest; @@ -43,7 +44,9 @@ public class LocalPlatform extends LocalAbstractPlatform { public IssuesWithBLOBs addIssue(IssuesUpdateRequest issuesRequest) { String issueStatus = "new"; if (StringUtils.isNotBlank(issuesRequest.getCustomFields())) { - List fields = JSONObject.parseArray(issuesRequest.getCustomFields(), TestCaseBatchRequest.CustomFiledRequest.class); + List customFields = issuesRequest.getRequestFields(); + String customFieldStr = JSONObject.toJSONString(customFields); + List fields = JSONObject.parseArray(customFieldStr, TestCaseBatchRequest.CustomFiledRequest.class); for (TestCaseBatchRequest.CustomFiledRequest field : fields) { if (StringUtils.equals("状态", field.getName())) { issueStatus = (String) field.getValue(); diff --git a/backend/src/main/java/io/metersphere/track/request/testcase/IssuesUpdateRequest.java b/backend/src/main/java/io/metersphere/track/request/testcase/IssuesUpdateRequest.java index d1df2acfb9..b6eaeed611 100644 --- a/backend/src/main/java/io/metersphere/track/request/testcase/IssuesUpdateRequest.java +++ b/backend/src/main/java/io/metersphere/track/request/testcase/IssuesUpdateRequest.java @@ -3,6 +3,7 @@ package io.metersphere.track.request.testcase; import io.metersphere.base.domain.FileMetadata; import io.metersphere.base.domain.IssuesWithBLOBs; import io.metersphere.base.domain.ext.CustomFieldResource; +import io.metersphere.dto.CustomFieldItemDTO; import io.metersphere.track.dto.PlatformStatusDTO; import lombok.Getter; import lombok.Setter; @@ -18,6 +19,7 @@ public class IssuesUpdateRequest extends IssuesWithBLOBs { private List tapdUsers; private List addFields; private List editFields; + private List requestFields; /** * zentao bug 处理人 */ diff --git a/backend/src/main/java/io/metersphere/track/service/IssuesService.java b/backend/src/main/java/io/metersphere/track/service/IssuesService.java index cf953fe77a..456768a004 100644 --- a/backend/src/main/java/io/metersphere/track/service/IssuesService.java +++ b/backend/src/main/java/io/metersphere/track/service/IssuesService.java @@ -6,6 +6,7 @@ import com.alibaba.fastjson.JSONObject; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import io.metersphere.base.domain.*; +import io.metersphere.base.domain.ext.CustomFieldResource; import io.metersphere.base.mapper.*; import io.metersphere.base.mapper.ext.ExtIssuesMapper; import io.metersphere.commons.constants.AttachmentType; @@ -116,7 +117,9 @@ public class IssuesService { }); } saveFollows(issuesRequest.getId(), issuesRequest.getFollows()); - customFieldIssuesService.addFields(issuesRequest.getId(), issuesRequest.getAddFields()); + List addFields = issuesRequest.getAddFields(); + addFields.addAll(issuesRequest.getEditFields()); + customFieldIssuesService.addFields(issuesRequest.getId(), addFields); return issues; } diff --git a/frontend/src/common/js/custom_field.js b/frontend/src/common/js/custom_field.js index c5325fc7a6..74dac4c4bb 100644 --- a/frontend/src/common/js/custom_field.js +++ b/frontend/src/common/js/custom_field.js @@ -105,6 +105,7 @@ export function buildCustomFields(data, param, template) { let addFields = []; let editFields = []; + let requestFields = []; template.customFields.forEach(item => { let customField = {fieldId: item.id}; @@ -118,9 +119,20 @@ export function buildCustomFields(data, param, template) { } else { addFields.push(customField); } + let fieldValue = (item.defaultValue instanceof Array && item.type !== 'multipleInput') ? + JSON.stringify(item.defaultValue) : (item.defaultValue || ""); + let requestField = { + id: item.id, + name: item.name, + customData: item.customData, + type: item.type, + value: fieldValue + } + requestFields.push(requestField); }); param.addFields = addFields; param.editFields = editFields; + param.requestFields = requestFields; } }