From e56f978d7d3bf98d1326b10b7a56170991821c92 Mon Sep 17 00:00:00 2001 From: AgAngle <1323481023@qq.com> Date: Thu, 15 Aug 2024 20:14:27 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E7=94=A8=E4=BE=8B=E6=89=A7=E8=A1=8C=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E9=80=9A=E7=9F=A5=E5=8F=98=E9=87=8F=E7=BC=BA=E5=A4=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1045218 --user=陈建星 【消息管理】配置接口CASE执行成功or失败的消息模板,有一些参数没有获取到值 https://www.tapd.cn/55049933/s/1563620 --- .../service/ApiReportSendNoticeService.java | 76 ++++++++++++------- .../definition/ApiTestCaseService.java | 2 + .../controller/ApiReportSendNoticeTests.java | 30 +++++++- 3 files changed, 80 insertions(+), 28 deletions(-) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiReportSendNoticeService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiReportSendNoticeService.java index 4a4cf2b686..66db54f2c0 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiReportSendNoticeService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiReportSendNoticeService.java @@ -2,10 +2,7 @@ package io.metersphere.api.service; import io.metersphere.api.constants.ApiDefinitionStatus; import io.metersphere.api.constants.ApiScenarioStatus; -import io.metersphere.api.domain.ApiReport; -import io.metersphere.api.domain.ApiScenario; -import io.metersphere.api.domain.ApiScenarioReport; -import io.metersphere.api.domain.ApiTestCase; +import io.metersphere.api.domain.*; import io.metersphere.api.dto.share.ApiReportShareRequest; import io.metersphere.api.dto.share.ShareInfoDTO; import io.metersphere.api.mapper.*; @@ -20,6 +17,7 @@ import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.CommonBeanFactory; import io.metersphere.sdk.util.Translator; import io.metersphere.system.domain.User; +import io.metersphere.system.dto.sdk.ApiDefinitionCaseDTO; import io.metersphere.system.dto.sdk.BaseSystemConfigDTO; import io.metersphere.system.mapper.UserMapper; import io.metersphere.system.notice.NoticeModel; @@ -59,6 +57,8 @@ public class ApiReportSendNoticeService { private ExtApiScenarioMapper extApiScenarioMapper; @Resource private ExtApiTestCaseMapper extApiTestCaseMapper; + @Resource + private ApiDefinitionMapper apiDefinitionMapper; public void sendNotice(ApiNoticeDTO noticeDTO) { String noticeType = null; @@ -122,7 +122,21 @@ public class ApiReportSendNoticeService { return; } - beanMap = new BeanMap(testCase); + ApiDefinition apiDefinition = apiDefinitionMapper.selectByPrimaryKey(testCase.getApiDefinitionId()); + + ApiDefinitionCaseDTO caseDTO = BeanUtils.copyBean(new ApiDefinitionCaseDTO(), testCase); + caseDTO.setCaseName(testCase.getName()); + caseDTO.setCaseStatus(getTranslateStatus(testCase.getStatus())); + caseDTO.setCaseCreateTime(testCase.getCreateTime()); + caseDTO.setCaseCreateUser(testCase.getCreateUser()); + caseDTO.setCaseUpdateTime(testCase.getUpdateTime()); + caseDTO.setCaseUpdateUser(testCase.getUpdateUser()); + caseDTO.setLastReportStatus(getTranslateReportStatus(report.getStatus())); + + caseDTO.setPath(apiDefinition.getPath()); + caseDTO.setMethod(apiDefinition.getMethod()); + + beanMap = new BeanMap(caseDTO); // TODO 是否需要区分场景和用例 noticeType = NoticeConstants.TaskType.API_DEFINITION_TASK; @@ -145,29 +159,12 @@ public class ApiReportSendNoticeService { Map paramMap = new HashMap<>(beanMap); noticeSendService.setLanguage(user.getLanguage()); paramMap.put(NoticeConstants.RelatedUser.OPERATOR, user != null ? user.getName() : ""); - // TODO 是否需要国际化 根据状态判断给不同的key - String status = paramMap.containsKey("status") ? paramMap.get("status").toString() : null; - if (StringUtils.isNotBlank(status)) { - if (List.of(ApiScenarioStatus.UNDERWAY.name(), ApiDefinitionStatus.PROCESSING.name()).contains(status)) { - status = Translator.get("api_definition.status.ongoing"); - } else if (List.of(ApiScenarioStatus.COMPLETED.name(), ApiDefinitionStatus.DONE.name()).contains(status)) { - status = Translator.get("api_definition.status.completed"); - } else if (StringUtils.equals(ApiScenarioStatus.DEPRECATED.name(), status)) { - status = Translator.get("api_definition.status.abandoned"); - } else if (StringUtils.equals(ApiDefinitionStatus.DEBUGGING.name(), status)) { - status = Translator.get("api_definition.status.continuous"); - } - } + String status = paramMap.containsKey("status") ? paramMap.get("status").toString() : null; + status = getTranslateStatus(status); String reportStatus = report.getStatus(); - if (StringUtils.endsWithIgnoreCase(reportStatus, ResultStatus.SUCCESS.name())) { - reportStatus = Translator.get("report.status.success"); - } else if (StringUtils.endsWithIgnoreCase(reportStatus, ResultStatus.FAKE_ERROR.name())) { - reportStatus = Translator.get("report.status.fake_error"); - } else { - reportStatus = Translator.get("report.status.error"); - } + reportStatus = getTranslateReportStatus(reportStatus); paramMap.put("status", status); paramMap.put("reportName", report.getName()); @@ -187,8 +184,6 @@ public class ApiReportSendNoticeService { paramMap.put("requestPassRate", report.getRequestPassRate()); paramMap.put("assertionPassRate", report.getAssertionPassRate()); - // TODO 这里状态是否是国际化 还有分享链接需要补充 - // TODO 暂时取一个环境处理 String environmentId = noticeDTO.getRunModeConfig().getEnvironmentId(); if (StringUtils.isNotEmpty(environmentId)) { @@ -202,6 +197,7 @@ public class ApiReportSendNoticeService { paramMap.put("reportUrl", reportUrl); paramMap.put("scenarioShareUrl", shareUrl); + paramMap.put("shareUrl", shareUrl); Map defaultTemplateMap = MessageTemplateUtils.getDefaultTemplateMap(); String template = defaultTemplateMap.get(noticeType + "_" + event); @@ -212,4 +208,30 @@ public class ApiReportSendNoticeService { noticeSendService.send(project, noticeType, noticeModel); } + + private String getTranslateReportStatus(String reportStatus) { + if (StringUtils.endsWithIgnoreCase(reportStatus, ResultStatus.SUCCESS.name())) { + reportStatus = Translator.get("report.status.success"); + } else if (StringUtils.endsWithIgnoreCase(reportStatus, ResultStatus.FAKE_ERROR.name())) { + reportStatus = Translator.get("report.status.fake_error"); + } else { + reportStatus = Translator.get("report.status.error"); + } + return reportStatus; + } + + private String getTranslateStatus(String status) { + if (StringUtils.isNotBlank(status)) { + if (List.of(ApiScenarioStatus.UNDERWAY.name(), ApiDefinitionStatus.PROCESSING.name()).contains(status)) { + status = Translator.get("api_definition.status.ongoing"); + } else if (List.of(ApiScenarioStatus.COMPLETED.name(), ApiDefinitionStatus.DONE.name()).contains(status)) { + status = Translator.get("api_definition.status.completed"); + } else if (StringUtils.equals(ApiScenarioStatus.DEPRECATED.name(), status)) { + status = Translator.get("api_definition.status.abandoned"); + } else if (StringUtils.equals(ApiDefinitionStatus.DEBUGGING.name(), status)) { + status = Translator.get("api_definition.status.continuous"); + } + } + return status; + } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseService.java index bc7313d642..71b89d5c68 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseService.java @@ -769,6 +769,8 @@ public class ApiTestCaseService extends MoveNodeService { // 设置环境 apiParamConfig.setEnvConfig(environmentService.get(envId)); + + taskRequest.getTaskInfo().getRunModeConfig().setEnvironmentId(envId); // 设置 method 等信息 apiCommonService.setApiDefinitionExecuteInfo(runRequest.getTestElement(), apiDefinition); diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiReportSendNoticeTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiReportSendNoticeTests.java index 17fd9be7e8..8684ecfefc 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiReportSendNoticeTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiReportSendNoticeTests.java @@ -1,17 +1,23 @@ package io.metersphere.api.controller; +import io.metersphere.api.constants.ApiConstants; +import io.metersphere.api.constants.ApiDefinitionStatus; import io.metersphere.api.domain.*; +import io.metersphere.api.mapper.ApiDefinitionMapper; import io.metersphere.api.mapper.ApiScenarioMapper; import io.metersphere.api.mapper.ApiTestCaseMapper; import io.metersphere.api.service.ApiReportSendNoticeService; import io.metersphere.api.service.definition.ApiReportService; import io.metersphere.api.service.scenario.ApiScenarioReportService; import io.metersphere.sdk.constants.ApiExecuteResourceType; +import io.metersphere.sdk.constants.ApplicationNumScope; import io.metersphere.sdk.constants.ResultStatus; import io.metersphere.sdk.domain.Environment; import io.metersphere.sdk.dto.api.notice.ApiNoticeDTO; import io.metersphere.sdk.mapper.EnvironmentMapper; import io.metersphere.system.base.BaseTest; +import io.metersphere.system.uid.IDGenerator; +import io.metersphere.system.uid.NumGenerator; import jakarta.annotation.Resource; import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.MethodOrderer; @@ -45,6 +51,8 @@ public class ApiReportSendNoticeTests extends BaseTest { private ApiReportSendNoticeService apiReportSendNoticeService; @Resource private ApiScenarioMapper apiScenarioMapper; + @Resource + private ApiDefinitionMapper apiDefinitionMapper; @Test @Order(0) @@ -61,9 +69,29 @@ public class ApiReportSendNoticeTests extends BaseTest { environment.setPos(1L); environmentMapper.insertSelective(environment); + ApiDefinition apiDefinition = new ApiDefinition(); + apiDefinition.setId(IDGenerator.nextStr()); + apiDefinition.setProjectId(DEFAULT_PROJECT_ID); + apiDefinition.setName(StringUtils.join("接口定义", apiDefinition.getId())); + apiDefinition.setModuleId("case-moduleId"); + apiDefinition.setProtocol(ApiConstants.HTTP_PROTOCOL); + apiDefinition.setMethod("GET"); + apiDefinition.setStatus(ApiDefinitionStatus.DEBUGGING.name()); + apiDefinition.setNum(NumGenerator.nextNum(DEFAULT_PROJECT_ID, ApplicationNumScope.API_DEFINITION)); + apiDefinition.setPos(0L); + apiDefinition.setPath(StringUtils.join("api/definition/", apiDefinition.getId())); + apiDefinition.setLatest(true); + apiDefinition.setVersionId("1.0"); + apiDefinition.setRefId(apiDefinition.getId()); + apiDefinition.setCreateTime(System.currentTimeMillis()); + apiDefinition.setUpdateTime(System.currentTimeMillis()); + apiDefinition.setCreateUser("admin"); + apiDefinition.setUpdateUser("admin"); + apiDefinitionMapper.insertSelective(apiDefinition); + ApiTestCase apiTestCase = new ApiTestCase(); apiTestCase.setId("send-api-case-id"); - apiTestCase.setApiDefinitionId("api-definition-id"); + apiTestCase.setApiDefinitionId(apiDefinition.getId()); apiTestCase.setProjectId(DEFAULT_PROJECT_ID); apiTestCase.setName(StringUtils.join("接口用例", apiTestCase.getId())); apiTestCase.setPriority("P0");