From f52a5cc07881aa730cd32171aa73ae96ee066f29 Mon Sep 17 00:00:00 2001 From: shiziyuan9527 Date: Fri, 14 Aug 2020 18:07:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=9B=86=E6=88=90Tapd?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../base/mapper/ext/ExtIssuesMapper.java | 11 ++ .../base/mapper/ext/ExtIssuesMapper.xml | 12 ++ .../constants/IssuesManagePlatform.java | 2 +- .../commons/utils/RestTemplateUtils.java | 7 +- .../io/metersphere/service/IssuesService.java | 187 ++++++++++++++---- .../controller/TestCaseIssuesController.java | 4 +- .../view/comonents/TestPlanTestCaseEdit.vue | 1 + 7 files changed, 181 insertions(+), 43 deletions(-) create mode 100644 backend/src/main/java/io/metersphere/base/mapper/ext/ExtIssuesMapper.java create mode 100644 backend/src/main/java/io/metersphere/base/mapper/ext/ExtIssuesMapper.xml diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtIssuesMapper.java b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtIssuesMapper.java new file mode 100644 index 0000000000..8bd29e8c0b --- /dev/null +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtIssuesMapper.java @@ -0,0 +1,11 @@ +package io.metersphere.base.mapper.ext; + +import io.metersphere.base.domain.Issues; +import org.apache.ibatis.annotations.Param; + +import java.util.List; + +public interface ExtIssuesMapper { + + List getIssues(@Param("caseId") String caseId, @Param("platform") String platform); +} diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtIssuesMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtIssuesMapper.xml new file mode 100644 index 0000000000..1b94ec96c5 --- /dev/null +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtIssuesMapper.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/commons/constants/IssuesManagePlatform.java b/backend/src/main/java/io/metersphere/commons/constants/IssuesManagePlatform.java index e898914154..d1ace27122 100644 --- a/backend/src/main/java/io/metersphere/commons/constants/IssuesManagePlatform.java +++ b/backend/src/main/java/io/metersphere/commons/constants/IssuesManagePlatform.java @@ -1,5 +1,5 @@ package io.metersphere.commons.constants; public enum IssuesManagePlatform { - Tapd, Jira + Tapd, Jira, Local } diff --git a/backend/src/main/java/io/metersphere/commons/utils/RestTemplateUtils.java b/backend/src/main/java/io/metersphere/commons/utils/RestTemplateUtils.java index 20a1b9323e..b4f6fdd323 100644 --- a/backend/src/main/java/io/metersphere/commons/utils/RestTemplateUtils.java +++ b/backend/src/main/java/io/metersphere/commons/utils/RestTemplateUtils.java @@ -1,5 +1,6 @@ package io.metersphere.commons.utils; +import io.metersphere.commons.exception.MSException; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; @@ -22,7 +23,8 @@ public class RestTemplateUtils { ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class); return responseEntity.getBody(); } catch (Exception e) { - throw new RuntimeException("调用接口失败", e); + MSException.throwException("接口调用失败:" + e.getMessage()); + return null; } } @@ -34,7 +36,8 @@ public class RestTemplateUtils { ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class); return responseEntity.getBody(); } catch (Exception e) { - throw new RuntimeException("调用接口失败", e); + MSException.throwException("接口调用失败:" + e.getMessage()); + return null; } } diff --git a/backend/src/main/java/io/metersphere/service/IssuesService.java b/backend/src/main/java/io/metersphere/service/IssuesService.java index d8135f9222..e1302b103c 100644 --- a/backend/src/main/java/io/metersphere/service/IssuesService.java +++ b/backend/src/main/java/io/metersphere/service/IssuesService.java @@ -3,7 +3,9 @@ package io.metersphere.service; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import io.metersphere.base.domain.*; +import io.metersphere.base.mapper.IssuesMapper; import io.metersphere.base.mapper.TestCaseIssuesMapper; +import io.metersphere.base.mapper.ext.ExtIssuesMapper; import io.metersphere.commons.constants.IssuesManagePlatform; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.user.SessionUser; @@ -12,7 +14,6 @@ import io.metersphere.commons.utils.RestTemplateUtils; import io.metersphere.commons.utils.SessionUtils; import io.metersphere.controller.ResultHolder; import io.metersphere.controller.request.IntegrationRequest; -import io.metersphere.track.dto.IssuesDTO; import io.metersphere.track.request.testcase.IssuesRequest; import io.metersphere.track.service.TestCaseService; import org.springframework.http.HttpHeaders; @@ -41,6 +42,10 @@ public class IssuesService { private ProjectService projectService; @Resource private TestCaseService testCaseService; + @Resource + private IssuesMapper issuesMapper; + @Resource + private ExtIssuesMapper extIssuesMapper; public void testAuth() { @@ -49,34 +54,6 @@ public class IssuesService { System.out.println(call.getData()); } - public void addIssues(IssuesRequest issuesRequest) { - String url = "https://api.tapd.cn/bugs"; - String testCaseId = issuesRequest.getTestCaseId(); - String tapdId = getTapdProjectId(testCaseId); - - if (StringUtils.isBlank(tapdId)) { - MSException.throwException("未关联Tapd 项目ID"); - } - - MultiValueMap paramMap = new LinkedMultiValueMap<>(); - paramMap.add("title", issuesRequest.getTitle()); - paramMap.add("workspace_id", tapdId); - paramMap.add("description", issuesRequest.getContent()); - - ResultHolder result = call(url, HttpMethod.POST, paramMap); - - String listJson = JSON.toJSONString(result.getData()); - JSONObject jsonObject = JSONObject.parseObject(listJson); - String issuesId = jsonObject.getObject("Bug", IssuesDTO.class).getId(); - - // 用例与第三方缺陷平台中的缺陷关联 - TestCaseIssues issues = new TestCaseIssues(); - issues.setId(UUID.randomUUID().toString()); - issues.setIssuesId(issuesId); - issues.setTestCaseId(testCaseId); - testCaseIssuesMapper.insert(issues); - } - private ResultHolder call(String url) { return call(url, HttpMethod.GET, null); } @@ -133,28 +110,142 @@ public class IssuesService { return headers; } - public IssuesDTO getTapdIssues(String projectId, String issuesId) { + public void addIssues(IssuesRequest issuesRequest) { + SessionUser user = SessionUtils.getUser(); + String orgId = user.getLastOrganizationId(); + + boolean tapd = isIntegratedPlatform(orgId, IssuesManagePlatform.Tapd.toString()); + boolean jira = isIntegratedPlatform(orgId, IssuesManagePlatform.Jira.toString()); + + String tapdId = getTapdProjectId(issuesRequest.getTestCaseId()); + String jiraId = ""; + + if (tapd || jira) { + if (StringUtils.isBlank(tapdId) && StringUtils.isBlank(jiraId)) { + MSException.throwException("集成了缺陷管理平台,但未进行项目关联!"); + } + } + + if (tapd) { + // 是否关联了项目 + if (StringUtils.isNotBlank(tapdId)) { + addTapdIssues(issuesRequest); + } + } + + if (jira) { + // addJiraIssues(issuesRequest); + } + + if (!tapd && !jira) { + addLocalIssues(issuesRequest); + } + + } + + public void addTapdIssues(IssuesRequest issuesRequest) { + String url = "https://api.tapd.cn/bugs"; + String testCaseId = issuesRequest.getTestCaseId(); + String tapdId = getTapdProjectId(testCaseId); + + if (StringUtils.isBlank(tapdId)) { + MSException.throwException("未关联Tapd 项目ID"); + } + + MultiValueMap paramMap = new LinkedMultiValueMap<>(); + paramMap.add("title", issuesRequest.getTitle()); + paramMap.add("workspace_id", tapdId); + paramMap.add("description", issuesRequest.getContent()); + + ResultHolder result = call(url, HttpMethod.POST, paramMap); + + String listJson = JSON.toJSONString(result.getData()); + JSONObject jsonObject = JSONObject.parseObject(listJson); + String issuesId = jsonObject.getObject("Bug", Issues.class).getId(); + + // 用例与第三方缺陷平台中的缺陷关联 + TestCaseIssues testCaseIssues = new TestCaseIssues(); + testCaseIssues.setId(UUID.randomUUID().toString()); + testCaseIssues.setIssuesId(issuesId); + testCaseIssues.setTestCaseId(testCaseId); + testCaseIssuesMapper.insert(testCaseIssues); + + // 插入缺陷表 + Issues issues = new Issues(); + issues.setId(issuesId); + issues.setPlatform(IssuesManagePlatform.Tapd.toString()); + issuesMapper.insert(issues); + } + + public void addLocalIssues(IssuesRequest request) { + SessionUser user = SessionUtils.getUser(); + String id = UUID.randomUUID().toString(); + Issues issues = new Issues(); + issues.setId(id); + issues.setStatus("new"); + issues.setReporter(user.getId()); + issues.setTitle(request.getTitle()); + issues.setDescription(request.getContent()); + issues.setCreateTime(System.currentTimeMillis()); + issues.setUpdateTime(System.currentTimeMillis()); + issues.setPlatform(IssuesManagePlatform.Local.toString()); + issuesMapper.insert(issues); + + TestCaseIssues testCaseIssues = new TestCaseIssues(); + testCaseIssues.setId(UUID.randomUUID().toString()); + testCaseIssues.setIssuesId(id); + testCaseIssues.setTestCaseId(request.getTestCaseId()); + testCaseIssuesMapper.insert(testCaseIssues); + } + + public Issues getTapdIssues(String projectId, String issuesId) { String url = "https://api.tapd.cn/bugs?workspace_id=" + projectId + "&id=" + issuesId; ResultHolder call = call(url); String listJson = JSON.toJSONString(call.getData()); if (StringUtils.equals(Boolean.FALSE.toString(), listJson)) { - return new IssuesDTO(); + return new Issues(); } JSONObject jsonObject = JSONObject.parseObject(listJson); - return jsonObject.getObject("Bug", IssuesDTO.class); + return jsonObject.getObject("Bug", Issues.class); } - public List getIssues(String caseId) { - List list = new ArrayList<>(); + public List getIssues(String caseId) { + List list = new ArrayList<>(); + SessionUser user = SessionUtils.getUser(); + String orgId = user.getLastOrganizationId(); + + boolean tapd = isIntegratedPlatform(orgId, IssuesManagePlatform.Tapd.toString()); + boolean jira = isIntegratedPlatform(orgId, IssuesManagePlatform.Jira.toString()); + + if (tapd) { + // 是否关联了项目 + String tapdId = getTapdProjectId(caseId); + if (StringUtils.isNotBlank(tapdId)) { + list.addAll(getTapdIssues(caseId)); + } + + } + + if (jira) { + // getJiraIssues(caseId); + } + + list.addAll(getLocalIssues(caseId)); + return list; + } + + public List getTapdIssues(String caseId) { + List list = new ArrayList<>(); String tapdId = getTapdProjectId(caseId); TestCaseIssuesExample example = new TestCaseIssuesExample(); example.createCriteria().andTestCaseIdEqualTo(caseId); - List issues = testCaseIssuesMapper.selectByExample(example); - List issuesIds = issues.stream().map(issue -> issue.getIssuesId()).collect(Collectors.toList()); + List issues = extIssuesMapper.getIssues(caseId, IssuesManagePlatform.Tapd.toString()); + + List issuesIds = issues.stream().map(issue -> issue.getId()).collect(Collectors.toList()); issuesIds.forEach(issuesId -> { - IssuesDTO dto = getTapdIssues(tapdId, issuesId); + Issues dto = getTapdIssues(tapdId, issuesId); if (StringUtils.isBlank(dto.getId())) { // 缺陷不存在,解除用例和缺陷的关联 TestCaseIssuesExample issuesExample = new TestCaseIssuesExample(); @@ -163,15 +254,35 @@ public class IssuesService { .andIssuesIdEqualTo(issuesId); testCaseIssuesMapper.deleteByExample(issuesExample); } else { - list.add(dto); + dto.setPlatform(IssuesManagePlatform.Tapd.toString()); + // 缺陷状态为 关闭,则不显示 + if (!StringUtils.equals("closed", dto.getStatus())) { + list.add(dto); + } } }); return list; } + public List getLocalIssues(String caseId) { + return extIssuesMapper.getIssues(caseId, IssuesManagePlatform.Local.toString()); + } + public String getTapdProjectId(String testCaseId) { TestCaseWithBLOBs testCase = testCaseService.getTestCase(testCaseId); Project project = projectService.getProjectById(testCase.getProjectId()); return project.getTapdId(); } + + /** + * 是否关联平台 + */ + public boolean isIntegratedPlatform(String orgId, String platform) { + IntegrationRequest request = new IntegrationRequest(); + request.setPlatform(platform); + request.setOrgId(orgId); + ServiceIntegration integration = integrationService.get(request); + return StringUtils.isNotBlank(integration.getId()); + } + } diff --git a/backend/src/main/java/io/metersphere/track/controller/TestCaseIssuesController.java b/backend/src/main/java/io/metersphere/track/controller/TestCaseIssuesController.java index f832b05940..b0c28a0646 100644 --- a/backend/src/main/java/io/metersphere/track/controller/TestCaseIssuesController.java +++ b/backend/src/main/java/io/metersphere/track/controller/TestCaseIssuesController.java @@ -1,7 +1,7 @@ package io.metersphere.track.controller; +import io.metersphere.base.domain.Issues; import io.metersphere.service.IssuesService; -import io.metersphere.track.dto.IssuesDTO; import io.metersphere.track.request.testcase.IssuesRequest; import org.springframework.web.bind.annotation.*; @@ -21,7 +21,7 @@ public class TestCaseIssuesController { } @GetMapping("/get/{id}") - public List getIssues(@PathVariable String id) { + public List getIssues(@PathVariable String id) { return issuesService.getIssues(id); } } diff --git a/frontend/src/business/components/track/plan/view/comonents/TestPlanTestCaseEdit.vue b/frontend/src/business/components/track/plan/view/comonents/TestPlanTestCaseEdit.vue index 4603afece3..e9e5010f3c 100644 --- a/frontend/src/business/components/track/plan/view/comonents/TestPlanTestCaseEdit.vue +++ b/frontend/src/business/components/track/plan/view/comonents/TestPlanTestCaseEdit.vue @@ -211,6 +211,7 @@ +