feat: 集成zentao

This commit is contained in:
shiziyuan9527 2020-11-13 17:01:36 +08:00
parent 437630eb81
commit 919f08431d
7 changed files with 258 additions and 11 deletions

View File

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

View File

@ -10,11 +10,13 @@ import java.util.List;
public class IssueFactory {
public static AbstractIssuePlatform createPlatform(String platform, IssuesRequest addIssueRequest) {
if (StringUtils.equals(IssuesManagePlatform.Tapd.toString(), platform)) {
return new TapdIssue(addIssueRequest);
return new TapdPlatform(addIssueRequest);
} else if (StringUtils.equals(IssuesManagePlatform.Jira.toString(), platform)) {
return new JiraIssue(addIssueRequest);
} else if (StringUtils.equals("LOCAL", platform)) {
return new LocalIssue(addIssueRequest);
return new JiraPlatform(addIssueRequest);
} else if (StringUtils.equals(IssuesManagePlatform.Zentao.toString(), platform)) {
return new ZentaoPlatform(addIssueRequest);
} else if (StringUtils.equals("LOCAL", platform)) {
return new LocalPlatform(addIssueRequest);
}
return null;
}

View File

@ -28,10 +28,10 @@ import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
public class JiraIssue extends AbstractIssuePlatform {
public class JiraPlatform extends AbstractIssuePlatform {
public JiraIssue(IssuesRequest issuesRequest) {
public JiraPlatform(IssuesRequest issuesRequest) {
super(issuesRequest);
}

View File

@ -10,9 +10,9 @@ import io.metersphere.track.request.testcase.IssuesRequest;
import java.util.List;
import java.util.UUID;
public class LocalIssue extends AbstractIssuePlatform {
public class LocalPlatform extends AbstractIssuePlatform {
public LocalIssue(IssuesRequest issuesRequest) {
public LocalPlatform(IssuesRequest issuesRequest) {
super(issuesRequest);
}

View File

@ -24,10 +24,10 @@ import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
public class TapdIssue extends AbstractIssuePlatform {
public class TapdPlatform extends AbstractIssuePlatform {
public TapdIssue(IssuesRequest issueRequest) {
public TapdPlatform(IssuesRequest issueRequest) {
super(issueRequest);
}

View File

@ -0,0 +1,219 @@
package io.metersphere.track.issue;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.metersphere.base.domain.*;
import io.metersphere.commons.constants.IssuesManagePlatform;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.track.request.testcase.IssuesRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.util.*;
import java.util.stream.Collectors;
public class ZentaoPlatform extends AbstractIssuePlatform {
public ZentaoPlatform(IssuesRequest issuesRequest) {
super(issuesRequest);
}
@Override
String getProjectId() {
// TestCaseWithBLOBs testCase = testCaseService.getTestCase(testCaseId);
// Project project = projectService.getProjectById(testCase.getProjectId());
// todo return getZentao ID
return "002";
}
@Override
public List<Issues> getIssue() {
List<Issues> list = new ArrayList<>();
TestCaseIssuesExample example = new TestCaseIssuesExample();
example.createCriteria().andTestCaseIdEqualTo(testCaseId);
List<Issues> issues = extIssuesMapper.getIssues(testCaseId, IssuesManagePlatform.Zentao.toString());
List<String> issuesIds = issues.stream().map(Issues::getId).collect(Collectors.toList());
issuesIds.forEach(issuesId -> {
Issues dto = getZentaoIssues(issuesId);
if (StringUtils.isBlank(dto.getId())) {
// 缺陷不存在解除用例和缺陷的关联
TestCaseIssuesExample issuesExample = new TestCaseIssuesExample();
issuesExample.createCriteria()
.andTestCaseIdEqualTo(testCaseId)
.andIssuesIdEqualTo(issuesId);
testCaseIssuesMapper.deleteByExample(issuesExample);
issuesMapper.deleteByPrimaryKey(issuesId);
} else {
dto.setPlatform(IssuesManagePlatform.Zentao.toString());
// 缺陷状态为 关闭则不显示
if (!StringUtils.equals("closed", dto.getStatus())) {
list.add(dto);
}
}
});
return list;
}
private Issues getZentaoIssues(String bugId) {
String projectId = getProjectId();
String session = login();
String config = getPlatformConfig(IssuesManagePlatform.Zentao.toString());
JSONObject object = JSON.parseObject(config);
String account = object.getString("account");
String password = object.getString("password");
String url = object.getString("url");
url = ZentaoUtils.getUrl(url, account, password);
System.out.println(url);
HttpEntity<MultiValueMap> requestEntity = new HttpEntity<>(new HttpHeaders());
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> responseEntity = restTemplate.exchange("http://xx/zentao/bug-view-"+ bugId +".json?zentaosid=" + session, HttpMethod.POST, requestEntity, String.class);
String body = responseEntity.getBody();
JSONObject obj = JSONObject.parseObject(body);
System.out.println(obj);
if (obj != null) {
JSONObject data = obj.getJSONObject("data");
JSONArray bugs = data.getJSONArray("bugs");
for (int j = 0; j < bugs.size(); j++) {
JSONObject bug = bugs.getJSONObject(j);
String id = bug.getString("id");
String title = bug.getString("title");
String description = bug.getString("steps");
Long createTime = bug.getLong("openedDate");
String status = bug.getString("status");
String reporter = bug.getString("openedBy");
Issues issues = new Issues();
issues.setId(id);
issues.setTitle(title);
issues.setDescription(description);
issues.setCreateTime(createTime);
issues.setStatus(status);
issues.setReporter(reporter);
return issues;
}
}
return new Issues();
}
@Override
public void addIssue(IssuesRequest issuesRequest) {
String config = getPlatformConfig(IssuesManagePlatform.Zentao.toString());
String session = login();
JSONObject object = JSON.parseObject(config);
// String account = object.getString("account");
// String password = object.getString("password");
MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
paramMap.add("product", "003");
paramMap.add("title", issuesRequest.getTitle());
paramMap.add("openedBuild", "123");
paramMap.add("steps", issuesRequest.getContent());
paramMap.add("assignedTo", "admin");
HttpEntity<MultiValueMap> requestEntity = new HttpEntity<>(paramMap, new HttpHeaders());
RestTemplate restTemplate = new RestTemplate();
System.out.println("zentao add bug");
ResponseEntity<String> responseEntity = restTemplate.exchange("http://xx/zentao/bug-create-3.json?zentaosid=" + session, HttpMethod.POST, requestEntity, String.class);
String body = responseEntity.getBody();
JSONObject obj = JSONObject.parseObject(body);
if (obj != null) {
String id = obj.getString("data");
if (StringUtils.isNotBlank(id)) {
// 用例与第三方缺陷平台中的缺陷关联
TestCaseIssues testCaseIssues = new TestCaseIssues();
testCaseIssues.setId(UUID.randomUUID().toString());
testCaseIssues.setIssuesId(id);
testCaseIssues.setTestCaseId(testCaseId);
testCaseIssuesMapper.insert(testCaseIssues);
// 插入缺陷表
Issues issues = new Issues();
issues.setId(id);
issues.setPlatform(IssuesManagePlatform.Zentao.toString());
issuesMapper.insert(issues);
}
}
System.out.println(obj);
// return responseEntity.getBody();
}
@Override
public void deleteIssue(String id) {
}
@Override
public void testAuth() {
try {
String config = getPlatformConfig(IssuesManagePlatform.Zentao.toString());
JSONObject object = JSON.parseObject(config);
String account = object.getString("account");
String password = object.getString("password");
String url = object.getString("url");
url = ZentaoUtils.getUrl(url, account, password);
HttpEntity<MultiValueMap> requestEntity = new HttpEntity<>(new HttpHeaders());
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> exchange = restTemplate.exchange(url + "&m=user&f=apilogin", HttpMethod.GET, requestEntity, String.class);
String body = exchange.getBody();
JSONObject obj = JSONObject.parseObject(body);
System.out.println(obj);
if (obj != null) {
String errcode = obj.getString("errcode");
String errmsg = obj.getString("errmsg");
if (StringUtils.isNotBlank(errcode)) {
MSException.throwException(errmsg);
}
}
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
MSException.throwException("验证失败!");
}
}
private String login() {
String session = getSession();
String loginUrl = "http://xx/zentao/user-login.json?zentaosid=" + session;
MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
paramMap.add("account", "admin");
paramMap.add("password", "xx");
HttpEntity<MultiValueMap> requestEntity = new HttpEntity<>(paramMap, new HttpHeaders());
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> responseEntity = restTemplate.exchange(loginUrl, HttpMethod.POST, requestEntity, String.class);
return session;
}
private String getSession() {
RestTemplate restTemplate = new RestTemplate();
HttpEntity<MultiValueMap> requestEntity = new HttpEntity<>(new HttpHeaders());
ResponseEntity<String> responseEntity = restTemplate.exchange("http://xx.xxx.xxx.xxx/zentao/api-getsessionid.json", HttpMethod.GET, requestEntity, String.class);
String body = responseEntity.getBody();
JSONObject obj = JSONObject.parseObject(body);
JSONObject data = obj.getJSONObject("data");
String session = data.getString("sessionID");
return session;
}
@Override
public List<PlatformUser> getPlatformUser() {
return null;
}
}

View File

@ -0,0 +1,26 @@
package io.metersphere.track.issue;
import io.metersphere.commons.utils.EncryptUtils;
public class ZentaoUtils {
/**
* @param code Zentao 应用代号
* @param key Zentao 密钥
* @return token
*/
public static String getToken(String code, String key, String time) {
return (String) EncryptUtils.md5Encrypt(code + key + time);
}
/**
* @param url Zentao url
* @param code Zentao 应用代号
* @param key Zentao 密钥
* @return url
*/
public static String getUrl(String url, String code, String key) {
String time = String.valueOf(System.currentTimeMillis());;
return url + "api.php?" + "code=" + code + "&time=" + time + "&token=" + getToken(code, key, time);
}
}