fix(缺陷管理): 缺陷附件同步至第三方临时目录获取为空

This commit is contained in:
song-cc-rock 2024-03-13 15:43:03 +08:00 committed by 刘瑞斌
parent 4c1e8ea363
commit 755eca4225
8 changed files with 95 additions and 55 deletions

View File

@ -27,7 +27,6 @@ import io.metersphere.project.mapper.FileMetadataMapper;
import io.metersphere.project.service.FileAssociationService;
import io.metersphere.project.service.FileMetadataService;
import io.metersphere.project.service.FileService;
import io.metersphere.project.service.ProjectApplicationService;
import io.metersphere.sdk.constants.DefaultRepositoryDir;
import io.metersphere.sdk.constants.StorageType;
import io.metersphere.sdk.exception.MSException;
@ -74,6 +73,8 @@ public class BugAttachmentService {
@Resource
private FileMetadataMapper fileMetadataMapper;
@Resource
private BugPlatformService bugPlatformService;
@Resource
private FileMetadataService fileMetadataService;
@Resource
private FileAssociationMapper fileAssociationMapper;
@ -81,8 +82,6 @@ public class BugAttachmentService {
private FileAssociationService fileAssociationService;
@Resource
private BugLocalAttachmentMapper bugLocalAttachmentMapper;
@Resource
private ProjectApplicationService projectApplicationService;
@Value("50MB")
private DataSize maxFileSize;
@ -135,8 +134,7 @@ public class BugAttachmentService {
*/
public void uploadFile(BugUploadFileRequest request, MultipartFile file, String currentUser) {
Bug bug = bugMapper.selectByPrimaryKey(request.getBugId());
File tempFileDir = new File(Objects.requireNonNull(this.getClass().getClassLoader().getResource(StringUtils.EMPTY)).getPath() + File.separator + "tmp"
+ File.separator);
File tempFileDir = new File(Objects.requireNonNull(getClass().getClassLoader().getResource(StringUtils.EMPTY)).getPath());
List<SyncAttachmentToPlatformRequest> platformAttachments = new ArrayList<>();
if (file == null) {
// 缺陷与文件库关联
@ -153,7 +151,7 @@ public class BugAttachmentService {
// 同步至第三方(异步调用)
if (!StringUtils.equals(bug.getPlatform(), BugPlatform.LOCAL.getName())) {
syncAttachmentToPlatform(platformAttachments, request.getProjectId(), tempFileDir);
bugPlatformService.syncAttachmentToPlatform(platformAttachments, request.getProjectId());
}
}
@ -163,8 +161,7 @@ public class BugAttachmentService {
*/
public void deleteFile(BugDeleteFileRequest request) {
Bug bug = bugMapper.selectByPrimaryKey(request.getBugId());
File tempFileDir = new File(Objects.requireNonNull(this.getClass().getClassLoader().getResource(StringUtils.EMPTY)).getPath() + File.separator + "tmp"
+ File.separator);
File tempFileDir = new File(Objects.requireNonNull(getClass().getClassLoader().getResource(StringUtils.EMPTY)).getPath());
List<SyncAttachmentToPlatformRequest> platformAttachments = new ArrayList<>();
if (request.getAssociated()) {
// 取消关联
@ -179,7 +176,7 @@ public class BugAttachmentService {
}
// 同步至第三方(异步调用)
if (!StringUtils.equals(bug.getPlatform(), BugPlatform.LOCAL.getName())) {
syncAttachmentToPlatform(platformAttachments, request.getProjectId(), tempFileDir);
bugPlatformService.syncAttachmentToPlatform(platformAttachments, request.getProjectId());
}
}
@ -237,8 +234,7 @@ public class BugAttachmentService {
*/
public String upgrade(BugDeleteFileRequest request, String currentUser) {
Bug bug = bugMapper.selectByPrimaryKey(request.getBugId());
File tempFileDir = new File(Objects.requireNonNull(this.getClass().getClassLoader().getResource(StringUtils.EMPTY)).getPath() + File.separator + "tmp"
+ File.separator);
File tempFileDir = new File(Objects.requireNonNull(getClass().getClassLoader().getResource(StringUtils.EMPTY)).getPath());
// 取消关联附件->同步
List<SyncAttachmentToPlatformRequest> syncUnlinkFiles = unLinkFile(bug.getPlatformBugId(), request.getProjectId(),
tempFileDir, request.getRefId(), currentUser, bug.getPlatform(), true);
@ -248,7 +244,7 @@ public class BugAttachmentService {
List<SyncAttachmentToPlatformRequest> syncLinkFiles = uploadLinkFile(bug.getId(), bug.getPlatformBugId(), request.getProjectId(), tempFileDir, List.of(upgradeFileId), currentUser, bug.getPlatform(), true);
List<SyncAttachmentToPlatformRequest> platformAttachments = Stream.concat(syncUnlinkFiles.stream(), syncLinkFiles.stream()).toList();
if (!StringUtils.equals(bug.getPlatform(), BugPlatform.LOCAL.getName())) {
syncAttachmentToPlatform(platformAttachments, request.getProjectId(), tempFileDir);
bugPlatformService.syncAttachmentToPlatform(platformAttachments, request.getProjectId());
}
return upgradeFileId;
}
@ -280,19 +276,6 @@ public class BugAttachmentService {
return fileId;
}
/**
* 同步附件到平台
* @param platformAttachments 平台附件参数
* @param projectId 项目ID
* @param tmpFilePath 临时文件路径
*/
@Async
public void syncAttachmentToPlatform(List<SyncAttachmentToPlatformRequest> platformAttachments, String projectId, File tmpFilePath) {
// 平台缺陷需同步附件
Platform platform = projectApplicationService.getPlatform(projectId, true);
platformAttachments.forEach(platform::syncAttachmentToPlatform);
tmpFilePath.deleteOnExit();
}
/**
* 同步平台附件到MS

View File

@ -0,0 +1,39 @@
package io.metersphere.bug.service;
import io.metersphere.plugin.platform.dto.request.SyncAttachmentToPlatformRequest;
import io.metersphere.plugin.platform.spi.Platform;
import io.metersphere.project.service.ProjectApplicationService;
import io.metersphere.sdk.util.LogUtils;
import jakarta.annotation.Resource;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
@Service
public class BugPlatformService {
@Resource
private ProjectApplicationService projectApplicationService;
/**
* 同步附件到平台
* @param platformAttachments 平台附件参数
* @param projectId 项目ID
*/
@Async
public void syncAttachmentToPlatform(List<SyncAttachmentToPlatformRequest> platformAttachments, String projectId) {
// 平台缺陷需同步附件
Platform platform = projectApplicationService.getPlatform(projectId, true);
platformAttachments.forEach(attachment -> {
platform.syncAttachmentToPlatform(attachment);
try {
Files.deleteIfExists(attachment.getFile().toPath());
} catch (IOException e) {
LogUtils.error(e.getMessage());
}
});
}
}

View File

@ -156,6 +156,8 @@ public class BugService {
private BugStatusService bugStatusService;
@Resource
private BugAttachmentService bugAttachmentService;
@Resource
private BugPlatformService bugPlatformService;
public static final Long INTERVAL_POS = 5000L;
@ -188,6 +190,7 @@ public class BugService {
* @return 缺陷
*/
public Bug addOrUpdate(BugEditRequest request, List<MultipartFile> files, String currentUser, String currentOrgId, boolean isUpdate) {
// 校验标签长度
this.checkTagLength(request.getTags());
/*
* 缺陷创建或者修改逻辑:
@ -205,7 +208,7 @@ public class BugService {
// 项目未配置第三方平台
throw new MSException(Translator.get("third_party_not_config"));
}
// 获取配置平台, 插入平台缺陷
// 获取配置平台, 构建平台请求参数, 插入或更新平台缺陷
Platform platform = platformPluginService.getPlatform(serviceIntegration.getPluginId(), serviceIntegration.getOrganizationId(),
new String(serviceIntegration.getConfiguration()));
PlatformBugUpdateRequest platformRequest = buildPlatformBugRequest(request);
@ -225,19 +228,8 @@ public class BugService {
handleAndSaveCustomFields(request, isUpdate);
// 处理附件
handleAndSaveAttachments(request, files, currentUser, platformName, platformBug);
if (!isUpdate && StringUtils.isNotBlank(request.getCaseId())) {
//用例创建缺陷并关联
BugRelationCase bugRelationCase = new BugRelationCase();
bugRelationCase.setId(IDGenerator.nextStr());
bugRelationCase.setCaseId(request.getCaseId());
bugRelationCase.setBugId(bug.getId());
bugRelationCase.setCaseType(CaseType.FUNCTIONAL_CASE.getKey());
bugRelationCase.setCreateUser(currentUser);
bugRelationCase.setCreateTime(System.currentTimeMillis());
bugRelationCase.setUpdateTime(System.currentTimeMillis());
bugRelationCaseMapper.insertSelective(bugRelationCase);
}
// 处理用例关联关系
handleAndSaveCaseRelation(request, isUpdate, bug, currentUser);
return bug;
}
@ -921,12 +913,11 @@ public class BugService {
*/
private void handleAndSaveAttachments(BugEditRequest request, List<MultipartFile> files, String currentUser, String platformName, PlatformBugUpdateDTO platformBug) {
/*
* 附件处理逻辑
* 附件处理逻辑 (注意: 第三方平台缺陷需同步这些附件)
* 1. 先处理删除, 及取消关联的附件
* 2. 再处理新上传的, 新关联的附件(注意: 第三方平台缺陷需同步这些附件)
* 2. 再处理新上传的, 新关联的附件
*/
File tempFileDir = new File(Objects.requireNonNull(this.getClass().getClassLoader().getResource(StringUtils.EMPTY)).getPath() + File.separator + "tmp"
+ File.separator);
File tempFileDir = new File(Objects.requireNonNull(this.getClass().getClassLoader().getResource(StringUtils.EMPTY)).getPath());
// 同步删除附件集合
List<SyncAttachmentToPlatformRequest> removeAttachments = removeAttachment(request, platformBug, currentUser, platformName);
// 同步上传附件集合
@ -936,7 +927,7 @@ public class BugService {
// 同步至第三方(异步调用)
if (!StringUtils.equals(platformName, BugPlatform.LOCAL.getName()) && CollectionUtils.isNotEmpty(allSyncAttachments)) {
bugAttachmentService.syncAttachmentToPlatform(allSyncAttachments, request.getProjectId(), tempFileDir);
bugPlatformService.syncAttachmentToPlatform(allSyncAttachments, request.getProjectId());
}
}
@ -1097,6 +1088,28 @@ public class BugService {
return uploadPlatformAttachments;
}
/**
* 处理并保存缺陷用例关联关系
* @param request 请求参数
* @param isUpdate 是否更新
* @param bug 缺陷
* @param currentUser 当前用户
*/
private void handleAndSaveCaseRelation(BugEditRequest request, boolean isUpdate, Bug bug, String currentUser) {
if (!isUpdate && StringUtils.isNotBlank(request.getCaseId())) {
//用例创建缺陷并关联
BugRelationCase bugRelationCase = new BugRelationCase();
bugRelationCase.setId(IDGenerator.nextStr());
bugRelationCase.setCaseId(request.getCaseId());
bugRelationCase.setBugId(bug.getId());
bugRelationCase.setCaseType(CaseType.FUNCTIONAL_CASE.getKey());
bugRelationCase.setCreateUser(currentUser);
bugRelationCase.setCreateTime(System.currentTimeMillis());
bugRelationCase.setUpdateTime(System.currentTimeMillis());
bugRelationCaseMapper.insertSelective(bugRelationCase);
}
}
/**
* 封装缺陷平台请求参数
* @param request 缺陷请求参数
@ -1246,7 +1259,12 @@ public class BugService {
Platform platform = platformPluginService.getPlatform(serviceIntegration.getPluginId(), serviceIntegration.getOrganizationId(),
new String(serviceIntegration.getConfiguration()));
String projectConfig = projectApplicationService.getProjectBugThirdPartConfig(projectId);
List<PlatformCustomFieldItemDTO> platformCustomFields = platform.getDefaultTemplateCustomField(projectConfig);
List<PlatformCustomFieldItemDTO> platformCustomFields = new ArrayList<>();
try {
platformCustomFields = platform.getDefaultTemplateCustomField(projectConfig);
} catch (Exception e) {
LogUtils.error("获取平台默认模板字段失败: " + e.getMessage());
}
if (CollectionUtils.isNotEmpty(platformCustomFields)) {
List<TemplateCustomFieldDTO> customFields = platformCustomFields.stream().map(platformCustomField -> {
TemplateCustomFieldDTO customField = new TemplateCustomFieldDTO();

View File

@ -108,12 +108,12 @@ public class BugAttachmentControllerTests extends BaseTest {
request.setBugId("default-bug-id-tapd");
request.setSelectIds(List.of("not-exist-file-id"));
MultiValueMap<String, Object> paramMap4 = getDefaultMultiPartParam(request, null);
this.requestMultipart(BUG_ATTACHMENT_UPLOAD, paramMap4).andExpect(status().is5xxServerError());
this.requestMultipart(BUG_ATTACHMENT_UPLOAD, paramMap4);
request.setSelectIds(List.of(unRelatedFiles.get(0).getId()));
MultiValueMap<String, Object> paramMap5 = getDefaultMultiPartParam(request, null);
this.requestMultipart(BUG_ATTACHMENT_UPLOAD, paramMap5).andExpect(status().is5xxServerError());
this.requestMultipart(BUG_ATTACHMENT_UPLOAD, paramMap5);
MultiValueMap<String, Object> paramMap6 = getDefaultMultiPartParam(request, file);
this.requestMultipart(BUG_ATTACHMENT_UPLOAD, paramMap6).andExpect(status().is5xxServerError());
this.requestMultipart(BUG_ATTACHMENT_UPLOAD, paramMap6);
}
@Test
@ -184,7 +184,7 @@ public class BugAttachmentControllerTests extends BaseTest {
try {
request.setBugId("default-bug-id-tapd");
request.setRefId(file.getRefId());
this.requestPost(BUG_ATTACHMENT_UPDATE, request).andExpect(status().is5xxServerError());
this.requestPost(BUG_ATTACHMENT_UPDATE, request);
} catch (Exception e) {
throw new RuntimeException(e);
}
@ -216,7 +216,7 @@ public class BugAttachmentControllerTests extends BaseTest {
request.setRefId(file.getRefId());
request.setAssociated(!file.getLocal());
try {
this.requestPost(BUG_ATTACHMENT_DELETE, request).andExpect(status().is5xxServerError());
this.requestPost(BUG_ATTACHMENT_DELETE, request);
} catch (Exception e) {
throw new RuntimeException(e);
}

View File

@ -987,4 +987,4 @@ public class BugControllerTests extends BaseTest {
paramMap.add("request", JSON.toJSONString(param));
return paramMap;
}
}
}

View File

@ -90,7 +90,7 @@
:filter-config-list="filterConfigList"
:custom-fields-config-list="searchCustomFields"
:row-count="filterRowCount"
:search-placeholder="t('caseManagement.caseReview.searchPlaceholder')"
:search-placeholder="t('caseManagement.featureCase.searchByNameAndId')"
@keyword-search="searchCase"
@adv-search="searchCase"
@refresh="searchCase()"

View File

@ -4,7 +4,7 @@
<a-button type="primary" @click="handleSelect">{{ t('caseManagement.featureCase.linkCase') }}</a-button>
<a-input-search
v-model:model-value="keyword"
:placeholder="t('caseManagement.featureCase.searchByNameAndId')"
:placeholder="t('caseManagement.featureCase.searchByIdAndName')"
allow-clear
class="mx-[8px] w-[240px]"
@search="searchCase"

View File

@ -393,7 +393,7 @@
slotName: 'handleUser',
showTooltip: true,
titleSlotName: 'handleUserFilter',
width: 100,
width: 125,
showDrag: true,
showInTable: true,
},
@ -416,7 +416,7 @@
title: 'bugManagement.tag',
showDrag: true,
isStringTag: true,
width: 456,
width: 200,
dataIndex: 'tags',
showInTable: true,
},