feat: 集成Tapd

This commit is contained in:
shiziyuan9527 2020-08-14 18:07:42 +08:00
parent b86fbafa00
commit f52a5cc078
7 changed files with 181 additions and 43 deletions

View File

@ -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<Issues> getIssues(@Param("caseId") String caseId, @Param("platform") String platform);
}

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="io.metersphere.base.mapper.ext.ExtIssuesMapper">
<select id="getIssues" resultType="io.metersphere.base.domain.Issues">
select issues.*
from test_case_issues, issues
where test_case_issues.issues_id = issues.id
and test_case_issues.test_case_id = #{caseId}
and issues.platform = #{platform};
</select>
</mapper>

View File

@ -1,5 +1,5 @@
package io.metersphere.commons.constants;
public enum IssuesManagePlatform {
Tapd, Jira
Tapd, Jira, Local
}

View File

@ -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<String> 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<String> 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;
}
}

View File

@ -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<String, Object> 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<String, Object> 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<IssuesDTO> getIssues(String caseId) {
List<IssuesDTO> list = new ArrayList<>();
public List<Issues> getIssues(String caseId) {
List<Issues> 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<Issues> getTapdIssues(String caseId) {
List<Issues> list = new ArrayList<>();
String tapdId = getTapdProjectId(caseId);
TestCaseIssuesExample example = new TestCaseIssuesExample();
example.createCriteria().andTestCaseIdEqualTo(caseId);
List<TestCaseIssues> issues = testCaseIssuesMapper.selectByExample(example);
List<String> issuesIds = issues.stream().map(issue -> issue.getIssuesId()).collect(Collectors.toList());
List<Issues> issues = extIssuesMapper.getIssues(caseId, IssuesManagePlatform.Tapd.toString());
List<String> 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 {
dto.setPlatform(IssuesManagePlatform.Tapd.toString());
// 缺陷状态为 关闭则不显示
if (!StringUtils.equals("closed", dto.getStatus())) {
list.add(dto);
}
}
});
return list;
}
public List<Issues> 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());
}
}

View File

@ -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<IssuesDTO> getIssues(@PathVariable String id) {
public List<Issues> getIssues(@PathVariable String id) {
return issuesService.getIssues(id);
}
}

View File

@ -211,6 +211,7 @@
<el-table-column prop="title" label="缺陷标题"/>
<el-table-column prop="status" label="缺陷状态"/>
<el-table-column prop="description" label="缺陷描述" show-overflow-tooltip/>
<el-table-column prop="platform" label="平台"/>
</el-table>
</el-col>
</el-row>