From 89e3ce9266c8f33783dc480cdfdd426971009793 Mon Sep 17 00:00:00 2001 From: WangXu10 Date: Thu, 1 Feb 2024 20:53:26 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E9=A1=B9=E7=9B=AE=E7=AE=A1=E7=90=86):?= =?UTF-8?q?=20=E9=85=8D=E7=BD=AE=E4=B8=89=E6=96=B9=E5=B9=B3=E5=8F=B0key?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProjectApplicationController.java | 2 +- .../service/ProjectApplicationService.java | 27 ++++++++++++------- .../ProjectApplicationControllerTests.java | 7 ++++- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectApplicationController.java b/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectApplicationController.java index 60298248f9..9052400e90 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectApplicationController.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectApplicationController.java @@ -309,7 +309,7 @@ public class ProjectApplicationController { @PostMapping("/validate/{pluginId}") @Operation(summary = "插件key校验") public void validateProjectConfig(@PathVariable("pluginId") String pluginId, @RequestBody Map configs) { - projectApplicationService.validateProjectConfig(pluginId, configs); + projectApplicationService.validateProjectConfig(pluginId, configs, SessionUtils.getCurrentOrganizationId()); } } diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectApplicationService.java b/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectApplicationService.java index 4701e31cb4..1d7587eeb0 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectApplicationService.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectApplicationService.java @@ -19,7 +19,6 @@ import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.Translator; import io.metersphere.system.domain.Plugin; import io.metersphere.system.domain.ServiceIntegration; -import io.metersphere.system.domain.ServiceIntegrationExample; import io.metersphere.system.domain.User; import io.metersphere.system.dto.sdk.OptionDTO; import io.metersphere.system.log.constants.OperationLogModule; @@ -442,19 +441,29 @@ public class ProjectApplicationService { * @param pluginId * @param configs */ - public void validateProjectConfig(String pluginId, Map configs) { - Platform platform = this.getPlatform(pluginId); + public void validateProjectConfig(String pluginId, Map configs, String organizationId) { + Platform platform = this.getPlatform(pluginId, organizationId); platform.validateProjectConfig(JSON.toJSONString(configs)); } - private Platform getPlatform(String pluginId) { - ServiceIntegrationExample example = new ServiceIntegrationExample(); - example.createCriteria().andPluginIdEqualTo(pluginId); - List serviceIntegrations = serviceIntegrationMapper.selectByExampleWithBLOBs(example); - if (CollectionUtils.isEmpty(serviceIntegrations)) { + private Platform getPlatform(String pluginId, String organizationId) { + Set orgPluginIds = platformPluginService.getOrgEnabledPlatformPlugins(organizationId) + .stream() + .map(Plugin::getId) + .collect(Collectors.toSet()); + // 查询服务集成中启用并且支持第三方模板的插件 + ServiceIntegration integration = serviceIntegrationService.getServiceIntegrationByOrgId(organizationId) + .stream() + .filter(serviceIntegration -> { + return serviceIntegration.getEnable() // 服务集成开启 + && orgPluginIds.contains(pluginId); + }) + .findFirst() + .orElse(null); + if (integration == null) { throw new MSException(NOT_FOUND); } - return platformPluginService.getPlatform(pluginId, serviceIntegrations.get(0).getOrganizationId(), new String(serviceIntegrations.get(0).getConfiguration())); + return platformPluginService.getPlatform(pluginId, integration.getOrganizationId(), new String(integration.getConfiguration())); } public int getFakeErrorList(String projectId) { diff --git a/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectApplicationControllerTests.java b/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectApplicationControllerTests.java index 6f8931d8ae..03612f897a 100644 --- a/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectApplicationControllerTests.java +++ b/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectApplicationControllerTests.java @@ -15,6 +15,7 @@ import io.metersphere.system.base.BaseTest; import io.metersphere.system.controller.handler.ResultHolder; import io.metersphere.system.domain.Plugin; import io.metersphere.system.domain.ServiceIntegration; +import io.metersphere.system.domain.ServiceIntegrationExample; import io.metersphere.system.dto.request.ServiceIntegrationUpdateRequest; import io.metersphere.system.mapper.ServiceIntegrationMapper; import jakarta.annotation.Resource; @@ -639,7 +640,7 @@ public class ProjectApplicationControllerTests extends BaseTest { request.setEnable(true); request.setPluginId(plugin.getId()); request.setConfiguration(integrationConfigMap); - request.setOrganizationId("100001100001"); + request.setOrganizationId("100001"); this.requestPostWithOkAndReturn("/service/integration/add", request); mockServerClient .when( @@ -660,6 +661,10 @@ public class ProjectApplicationControllerTests extends BaseTest { ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class); // 返回请求正常 Assertions.assertNotNull(resultHolder); + //测试完清掉 + ServiceIntegrationExample example = new ServiceIntegrationExample(); + example.createCriteria().andPluginIdEqualTo(plugin.getId()).andOrganizationIdEqualTo("100001").andEnableEqualTo(true); + serviceIntegrationMapper.deleteByExample(example); } @Getter