From 755eca4225b097307c19483b8ae48cf6c9a2fee8 Mon Sep 17 00:00:00 2001 From: song-cc-rock Date: Wed, 13 Mar 2024 15:43:03 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E7=BC=BA=E9=99=B7=E7=AE=A1=E7=90=86):=20?= =?UTF-8?q?=E7=BC=BA=E9=99=B7=E9=99=84=E4=BB=B6=E5=90=8C=E6=AD=A5=E8=87=B3?= =?UTF-8?q?=E7=AC=AC=E4=B8=89=E6=96=B9=E4=B8=B4=E6=97=B6=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E4=B8=BA=E7=A9=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bug/service/BugAttachmentService.java | 33 +++-------- .../bug/service/BugPlatformService.java | 39 +++++++++++++ .../metersphere/bug/service/BugService.java | 58 ++++++++++++------- .../BugAttachmentControllerTests.java | 10 ++-- .../bug/controller/BugControllerTests.java | 2 +- .../business/ms-case-associate/index.vue | 2 +- .../bug-management/components/bugCaseTab.vue | 2 +- frontend/src/views/bug-management/index.vue | 4 +- 8 files changed, 95 insertions(+), 55 deletions(-) create mode 100644 backend/services/bug-management/src/main/java/io/metersphere/bug/service/BugPlatformService.java diff --git a/backend/services/bug-management/src/main/java/io/metersphere/bug/service/BugAttachmentService.java b/backend/services/bug-management/src/main/java/io/metersphere/bug/service/BugAttachmentService.java index cc17c8cbb1..0f75bb51c7 100644 --- a/backend/services/bug-management/src/main/java/io/metersphere/bug/service/BugAttachmentService.java +++ b/backend/services/bug-management/src/main/java/io/metersphere/bug/service/BugAttachmentService.java @@ -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 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 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 syncUnlinkFiles = unLinkFile(bug.getPlatformBugId(), request.getProjectId(), tempFileDir, request.getRefId(), currentUser, bug.getPlatform(), true); @@ -248,7 +244,7 @@ public class BugAttachmentService { List syncLinkFiles = uploadLinkFile(bug.getId(), bug.getPlatformBugId(), request.getProjectId(), tempFileDir, List.of(upgradeFileId), currentUser, bug.getPlatform(), true); List 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 platformAttachments, String projectId, File tmpFilePath) { - // 平台缺陷需同步附件 - Platform platform = projectApplicationService.getPlatform(projectId, true); - platformAttachments.forEach(platform::syncAttachmentToPlatform); - tmpFilePath.deleteOnExit(); - } /** * 同步平台附件到MS diff --git a/backend/services/bug-management/src/main/java/io/metersphere/bug/service/BugPlatformService.java b/backend/services/bug-management/src/main/java/io/metersphere/bug/service/BugPlatformService.java new file mode 100644 index 0000000000..8122957bb0 --- /dev/null +++ b/backend/services/bug-management/src/main/java/io/metersphere/bug/service/BugPlatformService.java @@ -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 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()); + } + }); + } +} diff --git a/backend/services/bug-management/src/main/java/io/metersphere/bug/service/BugService.java b/backend/services/bug-management/src/main/java/io/metersphere/bug/service/BugService.java index 0b77ec5e9f..25f86e1a64 100644 --- a/backend/services/bug-management/src/main/java/io/metersphere/bug/service/BugService.java +++ b/backend/services/bug-management/src/main/java/io/metersphere/bug/service/BugService.java @@ -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 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 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 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 platformCustomFields = platform.getDefaultTemplateCustomField(projectConfig); + List platformCustomFields = new ArrayList<>(); + try { + platformCustomFields = platform.getDefaultTemplateCustomField(projectConfig); + } catch (Exception e) { + LogUtils.error("获取平台默认模板字段失败: " + e.getMessage()); + } if (CollectionUtils.isNotEmpty(platformCustomFields)) { List customFields = platformCustomFields.stream().map(platformCustomField -> { TemplateCustomFieldDTO customField = new TemplateCustomFieldDTO(); diff --git a/backend/services/bug-management/src/test/java/io/metersphere/bug/controller/BugAttachmentControllerTests.java b/backend/services/bug-management/src/test/java/io/metersphere/bug/controller/BugAttachmentControllerTests.java index c22b228380..10a02a0fd5 100644 --- a/backend/services/bug-management/src/test/java/io/metersphere/bug/controller/BugAttachmentControllerTests.java +++ b/backend/services/bug-management/src/test/java/io/metersphere/bug/controller/BugAttachmentControllerTests.java @@ -108,12 +108,12 @@ public class BugAttachmentControllerTests extends BaseTest { request.setBugId("default-bug-id-tapd"); request.setSelectIds(List.of("not-exist-file-id")); MultiValueMap 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 paramMap5 = getDefaultMultiPartParam(request, null); - this.requestMultipart(BUG_ATTACHMENT_UPLOAD, paramMap5).andExpect(status().is5xxServerError()); + this.requestMultipart(BUG_ATTACHMENT_UPLOAD, paramMap5); MultiValueMap 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); } diff --git a/backend/services/bug-management/src/test/java/io/metersphere/bug/controller/BugControllerTests.java b/backend/services/bug-management/src/test/java/io/metersphere/bug/controller/BugControllerTests.java index ca2a319b3e..098d5686a4 100644 --- a/backend/services/bug-management/src/test/java/io/metersphere/bug/controller/BugControllerTests.java +++ b/backend/services/bug-management/src/test/java/io/metersphere/bug/controller/BugControllerTests.java @@ -987,4 +987,4 @@ public class BugControllerTests extends BaseTest { paramMap.add("request", JSON.toJSONString(param)); return paramMap; } -} +} \ No newline at end of file diff --git a/frontend/src/components/business/ms-case-associate/index.vue b/frontend/src/components/business/ms-case-associate/index.vue index b8f8202e04..8810d432b4 100644 --- a/frontend/src/components/business/ms-case-associate/index.vue +++ b/frontend/src/components/business/ms-case-associate/index.vue @@ -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()" diff --git a/frontend/src/views/bug-management/components/bugCaseTab.vue b/frontend/src/views/bug-management/components/bugCaseTab.vue index bf04e905b7..58ca2c9d69 100644 --- a/frontend/src/views/bug-management/components/bugCaseTab.vue +++ b/frontend/src/views/bug-management/components/bugCaseTab.vue @@ -4,7 +4,7 @@ {{ t('caseManagement.featureCase.linkCase') }}