fix(测试跟踪): 更新自定义字段选项后,模板里的选项没有同步更新

--bug=1013993 --user=陈建星 【测试跟踪】github #14654项目设置-模板管理-缺陷模板管理:系统自带的状态字段-添加了自定义字段应用到default缺陷模板中,新增字段未同步 https://www.tapd.cn/55049933/s/1186786
This commit is contained in:
chenjianxing 2022-06-20 15:16:01 +08:00 committed by jianxing
parent f488d8d977
commit 26502d8791
5 changed files with 129 additions and 90 deletions

View File

@ -17,5 +17,7 @@ public class CustomFieldDao extends CustomField {
private String customData;
private String originGlobalId;
private String key;
}

View File

@ -103,12 +103,15 @@ public class CustomFieldService {
public void update(CustomField customField) {
if (customField.getGlobal() != null && customField.getGlobal()) {
// 如果是全局字段则创建对应工作空间字段
add(customField);
CustomFieldDao customFieldDao = new CustomFieldDao();
BeanUtils.copyBean(customFieldDao, customField);
customFieldDao.setOriginGlobalId(customField.getId());
// 如果是全局字段则创建对应项目字段
add(customFieldDao);
if (StringUtils.equals(customField.getScene(), TemplateConstants.FieldTemplateScene.TEST_CASE.name())) {
testCaseTemplateService.handleSystemFieldCreate(customField);
testCaseTemplateService.handleSystemFieldCreate(customFieldDao);
} else {
issueTemplateService.handleSystemFieldCreate(customField);
issueTemplateService.handleSystemFieldCreate(customFieldDao);
}
} else {
checkExist(customField);

View File

@ -21,7 +21,6 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
@Service
@Transactional(rollbackFor = Exception.class)
@ -72,17 +71,19 @@ public class CustomFieldTemplateService {
}
}
public List<CustomFieldTemplate> getSystemFieldCreateTemplate(CustomField customField, String scene) {
List<CustomField> globalField = customFieldService.getGlobalField(scene);
for (int i = 0; i < globalField.size(); i++) {
// 替换全局的字段
if (StringUtils.equals(globalField.get(i).getName(), customField.getName())) {
globalField.set(i, customField);
public List<CustomFieldTemplate> getSystemFieldCreateTemplate(CustomFieldDao customField, String templateId) {
CustomFieldTemplateExample example = new CustomFieldTemplateExample();
example.createCriteria().andTemplateIdEqualTo(templateId);
// 获取全局模板的关联关系
List<CustomFieldTemplate> fieldTemplates = customFieldTemplateMapper.selectByExample(example);
for (int i = 0; i < fieldTemplates.size(); i++) {
// 将全局字段替换成项目下的字段
if (StringUtils.equals(fieldTemplates.get(i).getFieldId(), customField.getOriginGlobalId())) {
fieldTemplates.get(i).setFieldId(customField.getId());
break;
}
}
List<String> fieldIds = globalField.stream().map(CustomField::getId).collect(Collectors.toList());
return getFieldTemplateByFieldIds(fieldIds);
return fieldTemplates;
}
public void updateFieldIdByTemplate(String templateId, String originId , String fieldId) {
@ -135,4 +136,24 @@ public class CustomFieldTemplateService {
String fieldId = customFieldTemplate.getFieldId();
return customFieldMapper.selectByPrimaryKey(fieldId);
}
/**
* 将原来全局字段与模板的关联
* 改为项目下字段与模板的关联
* @param customField
* @param templateIds
* @return
*/
public int updateProjectTemplateGlobalField(CustomFieldDao customField, List<String> templateIds) {
if (CollectionUtils.isEmpty(templateIds)) {
return 0;
}
CustomFieldTemplateExample example = new CustomFieldTemplateExample();
example.createCriteria()
.andFieldIdEqualTo(customField.getOriginGlobalId())
.andTemplateIdIn(templateIds);
CustomFieldTemplate customFieldTemplate = new CustomFieldTemplate();
customFieldTemplate.setFieldId(customField.getId());
return customFieldTemplateMapper.updateByExampleSelective(customFieldTemplate, example);
}
}

View File

@ -26,10 +26,8 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import java.util.*;
import java.util.stream.Collectors;
@Service
@Transactional(rollbackFor = Exception.class)
@ -89,7 +87,7 @@ public class IssueTemplateService extends TemplateBaseService {
public void update(UpdateIssueTemplateRequest request) {
if (request.getGlobal() != null && request.getGlobal()) {
String originId = request.getId();
// 如果是全局字段则创建对应工作空间字段
// 如果是全局字段则创建对应项目字段
String id = add(request);
projectService.updateIssueTemplate(originId, id, request.getProjectId());
} else {
@ -105,51 +103,58 @@ public class IssueTemplateService extends TemplateBaseService {
}
/**
* 获取该工作空间的系统模板
* - 如果没有则创建该工作空间模板并关联默认的字段
* 获取该项目下的系统模板
* - 如果没有则创建该工项目模板并关联默认的字段
* - 如果有则更新原来关联的 fieldId
*
* @param customField
*/
public void handleSystemFieldCreate(CustomField customField) {
IssueTemplate workspaceSystemTemplate = getWorkspaceSystemTemplate(customField.getProjectId());
if (workspaceSystemTemplate == null) {
createTemplateWithUpdateField(customField.getProjectId(), customField);
} else {
updateRelateWithUpdateField(workspaceSystemTemplate, customField);
}
}
private IssueTemplate getWorkspaceSystemTemplate(String workspaceId) {
public void handleSystemFieldCreate(CustomFieldDao customField) {
IssueTemplateExample example = new IssueTemplateExample();
example.createCriteria()
.andProjectIdEqualTo(workspaceId)
.andSystemEqualTo(true);
.andGlobalEqualTo(true);
example.or(example.createCriteria()
.andProjectIdEqualTo(customField.getProjectId()));
List<IssueTemplate> issueTemplates = issueTemplateMapper.selectByExampleWithBLOBs(example);
if (CollectionUtils.isNotEmpty(issueTemplates)) {
return issueTemplates.get(0);
}
return null;
}
private void createTemplateWithUpdateField(String projectId, CustomField customField) {
UpdateIssueTemplateRequest request = new UpdateIssueTemplateRequest();
IssueTemplate issueTemplate = new IssueTemplate();
issueTemplate.setName("default");
issueTemplate.setPlatform(TemplateConstants.IssueTemplatePlatform.metersphere.name());
issueTemplate.setGlobal(false);
issueTemplate.setSystem(true);
issueTemplate.setProjectId(projectId);
BeanUtils.copyBean(request, issueTemplate);
List<CustomFieldTemplate> systemFieldCreateTemplate =
customFieldTemplateService.getSystemFieldCreateTemplate(customField, TemplateConstants.FieldTemplateScene.ISSUE.name());
request.setCustomFields(systemFieldCreateTemplate);
add(request);
}
Map<Boolean, List<IssueTemplate>> templatesMap = issueTemplates.stream()
.collect(Collectors.groupingBy(IssueTemplate::getGlobal));
private void updateRelateWithUpdateField(IssueTemplate template, CustomField customField) {
CustomField globalField = customFieldService.getGlobalFieldByName(customField.getName());
customFieldTemplateService.updateFieldIdByTemplate(template.getId(), globalField.getId(), customField.getId());
// 获取全局模板
List<IssueTemplate> globalTemplates = templatesMap.get(true);
// 获取当前工作空间下模板
List<IssueTemplate> projectTemplates = templatesMap.get(false);
globalTemplates.forEach(global -> {
List<IssueTemplate> projectTemplate = null;
if (CollectionUtils.isNotEmpty(projectTemplates)) {
projectTemplate = projectTemplates.stream()
.filter(i -> i.getName().equals(global.getName()))
.collect(Collectors.toList());
}
// 如果没有项目级别的模板就创建
if (CollectionUtils.isEmpty(projectTemplate)) {
IssueTemplate template = new IssueTemplate();
BeanUtils.copyBean(template, global);
template.setId(UUID.randomUUID().toString());
template.setCreateTime(System.currentTimeMillis());
template.setUpdateTime(System.currentTimeMillis());
template.setCreateUser(SessionUtils.getUserId());
template.setGlobal(false);
template.setProjectId(customField.getProjectId());
issueTemplateMapper.insert(template);
List<CustomFieldTemplate> customFieldTemplate =
customFieldTemplateService.getSystemFieldCreateTemplate(customField, global.getId());
customFieldTemplateService.create(customFieldTemplate, template.getId(),
TemplateConstants.FieldTemplateScene.ISSUE.name());
}
});
if (CollectionUtils.isNotEmpty(projectTemplates)) {
customFieldTemplateService.updateProjectTemplateGlobalField(customField,
projectTemplates.stream().map(IssueTemplate::getId).collect(Collectors.toList()));
}
}
private void checkExist(IssueTemplate issueTemplate) {

View File

@ -27,6 +27,7 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
@Service
@Transactional(rollbackFor = Exception.class)
@ -101,45 +102,52 @@ public class TestCaseTemplateService extends TemplateBaseService {
* - 如果有则更新原来关联的 fieldId
* @param customField
*/
public void handleSystemFieldCreate(CustomField customField) {
TestCaseTemplateWithBLOBs workspaceSystemTemplate = getWorkspaceSystemTemplate(customField.getProjectId());
if (workspaceSystemTemplate == null) {
createTemplateWithUpdateField(customField.getProjectId(), customField);
} else {
updateRelateWithUpdateField(workspaceSystemTemplate, customField);
}
}
private void createTemplateWithUpdateField(String projectId, CustomField customField) {
UpdateCaseFieldTemplateRequest request = new UpdateCaseFieldTemplateRequest();
TestCaseTemplate testCaseTemplate = new TestCaseTemplate();
testCaseTemplate.setName("default");
testCaseTemplate.setType(TemplateConstants.TestCaseTemplateScene.functional.name());
testCaseTemplate.setGlobal(false);
testCaseTemplate.setSystem(true);
testCaseTemplate.setProjectId(projectId);
BeanUtils.copyBean(request, testCaseTemplate);
List<CustomFieldTemplate> systemFieldCreateTemplate =
customFieldTemplateService.getSystemFieldCreateTemplate(customField, TemplateConstants.FieldTemplateScene.TEST_CASE.name());
request.setCustomFields(systemFieldCreateTemplate);
add(request);
}
private void updateRelateWithUpdateField(TestCaseTemplateWithBLOBs template, CustomField customField) {
CustomField globalField = customFieldService.getGlobalFieldByName(customField.getName());
customFieldTemplateService.updateFieldIdByTemplate(template.getId(), globalField.getId(), customField.getId());
}
private TestCaseTemplateWithBLOBs getWorkspaceSystemTemplate(String workspaceId) {
public void handleSystemFieldCreate(CustomFieldDao customField) {
TestCaseTemplateExample example = new TestCaseTemplateExample();
example.createCriteria()
.andProjectIdEqualTo(workspaceId)
.andSystemEqualTo(true);
.andGlobalEqualTo(true);
example.or(example.createCriteria()
.andProjectIdEqualTo(customField.getProjectId()));
List<TestCaseTemplateWithBLOBs> testCaseTemplates = testCaseTemplateMapper.selectByExampleWithBLOBs(example);
if (CollectionUtils.isNotEmpty(testCaseTemplates)) {
return testCaseTemplates.get(0);
Map<Boolean, List<TestCaseTemplateWithBLOBs>> templatesMap = testCaseTemplates.stream()
.collect(Collectors.groupingBy(TestCaseTemplateWithBLOBs::getGlobal));
// 获取全局模板
List<TestCaseTemplateWithBLOBs> globalTemplates = templatesMap.get(true);
// 获取当前工作空间下模板
List<TestCaseTemplateWithBLOBs> projectTemplates = templatesMap.get(false);
globalTemplates.forEach(global -> {
List<TestCaseTemplateWithBLOBs> projectTemplate = null;
if (CollectionUtils.isNotEmpty(projectTemplates)) {
projectTemplate = projectTemplates.stream()
.filter(i -> i.getName().equals(global.getName()))
.collect(Collectors.toList());
}
// 如果没有项目级别的模板就创建
if (CollectionUtils.isEmpty(projectTemplate)) {
TestCaseTemplateWithBLOBs template = new TestCaseTemplateWithBLOBs();
BeanUtils.copyBean(template, global);
template.setId(UUID.randomUUID().toString());
template.setCreateTime(System.currentTimeMillis());
template.setUpdateTime(System.currentTimeMillis());
template.setCreateUser(SessionUtils.getUserId());
template.setGlobal(false);
template.setProjectId(customField.getProjectId());
testCaseTemplateMapper.insert(template);
List<CustomFieldTemplate> customFieldTemplate =
customFieldTemplateService.getSystemFieldCreateTemplate(customField, global.getId());
customFieldTemplateService.create(customFieldTemplate, template.getId(),
TemplateConstants.FieldTemplateScene.TEST_CASE.name());
}
});
if (CollectionUtils.isNotEmpty(projectTemplates)) {
customFieldTemplateService.updateProjectTemplateGlobalField(customField,
projectTemplates.stream().map(TestCaseTemplateWithBLOBs::getId).collect(Collectors.toList()));
}
return null;
}
private void checkExist(TestCaseTemplateWithBLOBs testCaseTemplate) {