fix(缺陷管理): 缺陷中自定义字段的处理

This commit is contained in:
shiziyuan9527 2022-07-05 18:35:34 +08:00 committed by jianxing
parent 6f431de67b
commit b0979e27fd
11 changed files with 81 additions and 82 deletions

View File

@ -22,7 +22,7 @@
<select id="lisSimple" resultType="io.metersphere.dto.CustomFieldDao"> <select id="lisSimple" resultType="io.metersphere.dto.CustomFieldDao">
select select
template_id, required, default_value, custom_data, template_id, required, default_value, custom_data,
cft.id as id, cf.id as id,
cf.name as name, cf.type as type cf.name as name, cf.type as type
from custom_field_template cft from custom_field_template cft
inner join custom_field cf inner join custom_field cf

View File

@ -2,6 +2,7 @@ package io.metersphere.base.mapper.ext;
import io.metersphere.base.domain.Issues; import io.metersphere.base.domain.Issues;
import io.metersphere.base.domain.IssuesDao; import io.metersphere.base.domain.IssuesDao;
import io.metersphere.dto.CustomFieldItemDTO;
import io.metersphere.track.dto.CustomFieldResourceCompatibleDTO; import io.metersphere.track.dto.CustomFieldResourceCompatibleDTO;
import io.metersphere.track.dto.PlanReportIssueDTO; import io.metersphere.track.dto.PlanReportIssueDTO;
import io.metersphere.track.request.testcase.IssuesRequest; import io.metersphere.track.request.testcase.IssuesRequest;
@ -36,4 +37,6 @@ public interface ExtIssuesMapper {
List<CustomFieldResourceCompatibleDTO> getForCompatibleCustomField(String projectId, int offset, int pageSize); List<CustomFieldResourceCompatibleDTO> getForCompatibleCustomField(String projectId, int offset, int pageSize);
IssuesDao selectByPrimaryKey(String id); IssuesDao selectByPrimaryKey(String id);
List<CustomFieldItemDTO> getIssueCustomField(String id);
} }

View File

@ -127,6 +127,20 @@
from issues from issues
where id = #{id,jdbcType=VARCHAR} where id = #{id,jdbcType=VARCHAR}
</select> </select>
<select id="getIssueCustomField" resultType="io.metersphere.dto.CustomFieldItemDTO">
select cfi.field_id as id,
cf.type,
cft.custom_data,
cf.name,
case cf.type
when 'richText' then cfi.text_value
when 'textarea' then cfi.text_value
else cfi.value end as value
from custom_field_issues cfi
join custom_field_template cft on cfi.field_id = cft.field_id
join custom_field cf on cf.id = cft.field_id
where cfi.resource_id = #{id}
</select>
<sql id="queryWhereCondition"> <sql id="queryWhereCondition">
<where> <where>

View File

@ -12,5 +12,6 @@ import java.io.Serializable;
@ToString(callSuper = true) @ToString(callSuper = true)
public class CustomFieldResourceDTO extends CustomFieldResource implements Serializable { public class CustomFieldResourceDTO extends CustomFieldResource implements Serializable {
private String name; private String name;
private String type;
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
} }

View File

@ -237,23 +237,9 @@ public class CustomFieldService {
List<CustomFieldResource> list = new ArrayList<>(); List<CustomFieldResource> list = new ArrayList<>();
if (StringUtils.isNotBlank(customFieldsStr)) { if (StringUtils.isNotBlank(customFieldsStr)) {
if (JSONObject.parse(customFieldsStr) instanceof JSONArray) { if (JSONObject.parse(customFieldsStr) instanceof JSONArray) {
List<CustomFieldItemDTO> fieldItems = JSONArray.parseArray(customFieldsStr, CustomFieldItemDTO.class); List<CustomFieldItemDTO> items = JSONArray.parseArray(customFieldsStr, CustomFieldItemDTO.class);
for (CustomFieldItemDTO dto : fieldItems) { for (CustomFieldItemDTO item : items) {
// customFieldItemDTO里的id是模版ID list.add(constructorCustomFieldResource(item));
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);
} }
return list; return list;
} }
@ -261,50 +247,15 @@ public class CustomFieldService {
return new ArrayList<>(); return new ArrayList<>();
} }
public List<CustomFieldResource> getJiraCustomFieldResource(String customFieldsStr, String projectId) { private CustomFieldResource constructorCustomFieldResource(CustomFieldItemDTO dto) {
List<CustomFieldResource> list = new ArrayList<>();
if (StringUtils.isNotBlank(customFieldsStr)) {
if (JSONObject.parse(customFieldsStr) instanceof JSONArray) {
List<CustomFieldItemDTO> fieldItems = JSONArray.parseArray(customFieldsStr, CustomFieldItemDTO.class);
for (CustomFieldItemDTO dto : fieldItems) {
CustomFieldResource resource = new CustomFieldResource(); 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()); resource.setFieldId(dto.getId());
} else { if (StringUtils.isNotBlank(dto.getType()) && StringUtils.equalsAny(CustomFieldType.RICH_TEXT.getValue(), CustomFieldType.TEXTAREA.getValue())) {
resource.setFieldId(customFieldTemplate.getFieldId());
}
if (StringUtils.isNotBlank(dto.getType())
&& StringUtils.equalsAny(CustomFieldType.RICH_TEXT.getValue(), CustomFieldType.TEXTAREA.getValue())) {
resource.setTextValue(dto.getValue().toString()); resource.setTextValue(dto.getValue().toString());
} else { } else {
resource.setValue(JSONObject.toJSONString(dto.getValue())); resource.setValue(JSONObject.toJSONString(dto.getValue()));
} }
list.add(resource); return resource;
}
return list;
}
}
return new ArrayList<>();
} }
public List<CustomField> getByProjectId(String projectId) { public List<CustomField> getByProjectId(String projectId) {

View File

@ -1,5 +1,6 @@
package io.metersphere.track.issue; package io.metersphere.track.issue;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import io.metersphere.base.domain.*; import io.metersphere.base.domain.*;
@ -372,7 +373,7 @@ public abstract class AbstractIssuePlatform implements IssuesPlatform {
} }
protected void addCustomFields(IssuesUpdateRequest issuesRequest, MultiValueMap<String, Object> paramMap) { protected void addCustomFields(IssuesUpdateRequest issuesRequest, MultiValueMap<String, Object> paramMap) {
List<CustomFieldItemDTO> customFields = CustomFieldService.getCustomFields(issuesRequest.getCustomFields()); List<CustomFieldItemDTO> customFields = issuesRequest.getRequestFields();
customFields.forEach(item -> { customFields.forEach(item -> {
if (StringUtils.isNotBlank(item.getCustomData())) { if (StringUtils.isNotBlank(item.getCustomData())) {
if (item.getValue() instanceof String) { if (item.getValue() instanceof String) {
@ -420,8 +421,17 @@ public abstract class AbstractIssuePlatform implements IssuesPlatform {
if (value instanceof JSONObject) { if (value instanceof JSONObject) {
item.setValue(getSyncJsonParamValue(value)); item.setValue(getSyncJsonParamValue(value));
} else if (value instanceof JSONArray) { } else if (value instanceof JSONArray) {
// 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));
}
}
} else {
List<Object> values = new ArrayList<>(); List<Object> values = new ArrayList<>();
((JSONArray)value).forEach(attr -> { ((JSONArray) value).forEach(attr -> {
if (attr instanceof JSONObject) { if (attr instanceof JSONObject) {
values.add(getSyncJsonParamValue(attr)); values.add(getSyncJsonParamValue(attr));
} else { } else {
@ -429,6 +439,7 @@ public abstract class AbstractIssuePlatform implements IssuesPlatform {
} }
}); });
item.setValue(values); item.setValue(values);
}
} else { } else {
item.setValue(value); item.setValue(value);
} }
@ -487,9 +498,8 @@ public abstract class AbstractIssuePlatform implements IssuesPlatform {
protected void mergeCustomField(IssuesWithBLOBs issues, String defaultCustomField) { protected void mergeCustomField(IssuesWithBLOBs issues, String defaultCustomField) {
if (StringUtils.isNotBlank(defaultCustomField)) { if (StringUtils.isNotBlank(defaultCustomField)) {
String issuesCustomFields = issues.getCustomFields(); List<CustomFieldItemDTO> customFields = extIssuesMapper.getIssueCustomField(issues.getId());
if (StringUtils.isBlank(issuesCustomFields) || issuesCustomFields.startsWith("{")) issuesCustomFields = "[]"; JSONArray issueFields = JSONArray.parseArray(JSON.toJSONString(customFields));
JSONArray issueFields = JSONArray.parseArray(issuesCustomFields);
Set<String> ids = issueFields.stream().map(i -> ((JSONObject) i).getString("id")).collect(Collectors.toSet()); Set<String> ids = issueFields.stream().map(i -> ((JSONObject) i).getString("id")).collect(Collectors.toSet());
JSONArray defaultFields = JSONArray.parseArray(defaultCustomField); JSONArray defaultFields = JSONArray.parseArray(defaultCustomField);
defaultFields.forEach(item -> { // 如果自定义字段里没有模板新加的字段就把新字段加上 defaultFields.forEach(item -> { // 如果自定义字段里没有模板新加的字段就把新字段加上

View File

@ -368,7 +368,7 @@ public class JiraPlatform extends AbstractIssuePlatform {
} }
private void parseCustomFiled(IssuesUpdateRequest issuesRequest, JSONObject fields) { private void parseCustomFiled(IssuesUpdateRequest issuesRequest, JSONObject fields) {
List<CustomFieldItemDTO> customFields = CustomFieldService.getCustomFields(issuesRequest.getCustomFields()); List<CustomFieldItemDTO> customFields = issuesRequest.getRequestFields();
customFields.forEach(item -> { customFields.forEach(item -> {
String fieldName = item.getCustomData(); String fieldName = item.getCustomData();
@ -390,7 +390,7 @@ public class JiraPlatform extends AbstractIssuePlatform {
} else if (StringUtils.equalsAny(item.getType(), "multipleSelect", "checkbox", "multipleMember")) { } else if (StringUtils.equalsAny(item.getType(), "multipleSelect", "checkbox", "multipleMember")) {
JSONArray attrs = new JSONArray(); JSONArray attrs = new JSONArray();
if (item.getValue() != null) { if (item.getValue() != null) {
JSONArray values = (JSONArray)item.getValue(); JSONArray values = JSONArray.parseArray((String) item.getValue());
values.forEach(v -> { values.forEach(v -> {
JSONObject param = new JSONObject(); JSONObject param = new JSONObject();
param.put("id", v); param.put("id", v);
@ -402,7 +402,7 @@ public class JiraPlatform extends AbstractIssuePlatform {
if (item.getValue() != null) { if (item.getValue() != null) {
JSONObject attr = new JSONObject(); JSONObject attr = new JSONObject();
if (item.getValue() instanceof JSONArray) { if (item.getValue() instanceof JSONArray) {
JSONArray values = (JSONArray) item.getValue(); JSONArray values = JSONArray.parseArray((String) item.getValue());
if (CollectionUtils.isNotEmpty(values)) { if (CollectionUtils.isNotEmpty(values)) {
if (values.size() > 0) { if (values.size() > 0) {
attr.put("id", values.get(0)); attr.put("id", values.get(0));
@ -521,7 +521,7 @@ public class JiraPlatform extends AbstractIssuePlatform {
getUpdateIssue(item, jiraClientV2.getIssues(item.getPlatformId())); getUpdateIssue(item, jiraClientV2.getIssues(item.getPlatformId()));
String customFields = item.getCustomFields(); String customFields = item.getCustomFields();
// 把自定义字段存入新表 // 把自定义字段存入新表
List<CustomFieldResource> customFieldResource = customFieldService.getJiraCustomFieldResource(customFields, project.getId()); List<CustomFieldResource> customFieldResource = customFieldService.getCustomFieldResource(customFields);
customFieldIssuesService.addFields(item.getId(), customFieldResource); customFieldIssuesService.addFields(item.getId(), customFieldResource);
issuesMapper.updateByPrimaryKeySelective(item); issuesMapper.updateByPrimaryKeySelective(item);
} catch (HttpClientErrorException e) { } catch (HttpClientErrorException e) {

View File

@ -7,6 +7,7 @@ import io.metersphere.commons.constants.IssuesManagePlatform;
import io.metersphere.commons.user.SessionUser; import io.metersphere.commons.user.SessionUser;
import io.metersphere.commons.utils.BeanUtils; import io.metersphere.commons.utils.BeanUtils;
import io.metersphere.commons.utils.SessionUtils; import io.metersphere.commons.utils.SessionUtils;
import io.metersphere.dto.CustomFieldItemDTO;
import io.metersphere.track.dto.DemandDTO; import io.metersphere.track.dto.DemandDTO;
import io.metersphere.track.request.testcase.IssuesRequest; import io.metersphere.track.request.testcase.IssuesRequest;
import io.metersphere.track.request.testcase.IssuesUpdateRequest; import io.metersphere.track.request.testcase.IssuesUpdateRequest;
@ -43,7 +44,9 @@ public class LocalPlatform extends LocalAbstractPlatform {
public IssuesWithBLOBs addIssue(IssuesUpdateRequest issuesRequest) { public IssuesWithBLOBs addIssue(IssuesUpdateRequest issuesRequest) {
String issueStatus = "new"; String issueStatus = "new";
if (StringUtils.isNotBlank(issuesRequest.getCustomFields())) { if (StringUtils.isNotBlank(issuesRequest.getCustomFields())) {
List<TestCaseBatchRequest.CustomFiledRequest> fields = JSONObject.parseArray(issuesRequest.getCustomFields(), TestCaseBatchRequest.CustomFiledRequest.class); List<CustomFieldItemDTO> customFields = issuesRequest.getRequestFields();
String customFieldStr = JSONObject.toJSONString(customFields);
List<TestCaseBatchRequest.CustomFiledRequest> fields = JSONObject.parseArray(customFieldStr, TestCaseBatchRequest.CustomFiledRequest.class);
for (TestCaseBatchRequest.CustomFiledRequest field : fields) { for (TestCaseBatchRequest.CustomFiledRequest field : fields) {
if (StringUtils.equals("状态", field.getName())) { if (StringUtils.equals("状态", field.getName())) {
issueStatus = (String) field.getValue(); issueStatus = (String) field.getValue();

View File

@ -3,6 +3,7 @@ package io.metersphere.track.request.testcase;
import io.metersphere.base.domain.FileMetadata; import io.metersphere.base.domain.FileMetadata;
import io.metersphere.base.domain.IssuesWithBLOBs; import io.metersphere.base.domain.IssuesWithBLOBs;
import io.metersphere.base.domain.ext.CustomFieldResource; import io.metersphere.base.domain.ext.CustomFieldResource;
import io.metersphere.dto.CustomFieldItemDTO;
import io.metersphere.track.dto.PlatformStatusDTO; import io.metersphere.track.dto.PlatformStatusDTO;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
@ -18,6 +19,7 @@ public class IssuesUpdateRequest extends IssuesWithBLOBs {
private List<String> tapdUsers; private List<String> tapdUsers;
private List<CustomFieldResource> addFields; private List<CustomFieldResource> addFields;
private List<CustomFieldResource> editFields; private List<CustomFieldResource> editFields;
private List<CustomFieldItemDTO> requestFields;
/** /**
* zentao bug 处理人 * zentao bug 处理人
*/ */

View File

@ -6,6 +6,7 @@ import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page; import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageHelper;
import io.metersphere.base.domain.*; import io.metersphere.base.domain.*;
import io.metersphere.base.domain.ext.CustomFieldResource;
import io.metersphere.base.mapper.*; import io.metersphere.base.mapper.*;
import io.metersphere.base.mapper.ext.ExtIssuesMapper; import io.metersphere.base.mapper.ext.ExtIssuesMapper;
import io.metersphere.commons.constants.AttachmentType; import io.metersphere.commons.constants.AttachmentType;
@ -116,7 +117,9 @@ public class IssuesService {
}); });
} }
saveFollows(issuesRequest.getId(), issuesRequest.getFollows()); saveFollows(issuesRequest.getId(), issuesRequest.getFollows());
customFieldIssuesService.addFields(issuesRequest.getId(), issuesRequest.getAddFields()); List<CustomFieldResource> addFields = issuesRequest.getAddFields();
addFields.addAll(issuesRequest.getEditFields());
customFieldIssuesService.addFields(issuesRequest.getId(), addFields);
return issues; return issues;
} }

View File

@ -105,6 +105,7 @@ export function buildCustomFields(data, param, template) {
let addFields = []; let addFields = [];
let editFields = []; let editFields = [];
let requestFields = [];
template.customFields.forEach(item => { template.customFields.forEach(item => {
let customField = {fieldId: item.id}; let customField = {fieldId: item.id};
@ -118,9 +119,20 @@ export function buildCustomFields(data, param, template) {
} else { } else {
addFields.push(customField); 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.addFields = addFields;
param.editFields = editFields; param.editFields = editFields;
param.requestFields = requestFields;
} }
} }