From 919f08431d078bb989918f849572ab2cbaada9f3 Mon Sep 17 00:00:00 2001 From: shiziyuan9527 Date: Fri, 13 Nov 2020 17:01:36 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=9B=86=E6=88=90zentao?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../constants/IssuesManagePlatform.java | 2 +- .../metersphere/track/issue/IssueFactory.java | 10 +- .../{JiraIssue.java => JiraPlatform.java} | 4 +- .../{LocalIssue.java => LocalPlatform.java} | 4 +- .../{TapdIssue.java => TapdPlatform.java} | 4 +- .../track/issue/ZentaoPlatform.java | 219 ++++++++++++++++++ .../metersphere/track/issue/ZentaoUtils.java | 26 +++ 7 files changed, 258 insertions(+), 11 deletions(-) rename backend/src/main/java/io/metersphere/track/issue/{JiraIssue.java => JiraPlatform.java} (98%) rename backend/src/main/java/io/metersphere/track/issue/{LocalIssue.java => LocalPlatform.java} (94%) rename backend/src/main/java/io/metersphere/track/issue/{TapdIssue.java => TapdPlatform.java} (98%) create mode 100644 backend/src/main/java/io/metersphere/track/issue/ZentaoPlatform.java create mode 100644 backend/src/main/java/io/metersphere/track/issue/ZentaoUtils.java 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 d1ace27122..0e6540740d 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, Local + Tapd, Jira, Local, Zentao } diff --git a/backend/src/main/java/io/metersphere/track/issue/IssueFactory.java b/backend/src/main/java/io/metersphere/track/issue/IssueFactory.java index 3592094890..b578caac05 100644 --- a/backend/src/main/java/io/metersphere/track/issue/IssueFactory.java +++ b/backend/src/main/java/io/metersphere/track/issue/IssueFactory.java @@ -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; } diff --git a/backend/src/main/java/io/metersphere/track/issue/JiraIssue.java b/backend/src/main/java/io/metersphere/track/issue/JiraPlatform.java similarity index 98% rename from backend/src/main/java/io/metersphere/track/issue/JiraIssue.java rename to backend/src/main/java/io/metersphere/track/issue/JiraPlatform.java index 40a1277f90..981b76e2ef 100644 --- a/backend/src/main/java/io/metersphere/track/issue/JiraIssue.java +++ b/backend/src/main/java/io/metersphere/track/issue/JiraPlatform.java @@ -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); } diff --git a/backend/src/main/java/io/metersphere/track/issue/LocalIssue.java b/backend/src/main/java/io/metersphere/track/issue/LocalPlatform.java similarity index 94% rename from backend/src/main/java/io/metersphere/track/issue/LocalIssue.java rename to backend/src/main/java/io/metersphere/track/issue/LocalPlatform.java index b75946b726..bbbd12df8f 100644 --- a/backend/src/main/java/io/metersphere/track/issue/LocalIssue.java +++ b/backend/src/main/java/io/metersphere/track/issue/LocalPlatform.java @@ -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); } diff --git a/backend/src/main/java/io/metersphere/track/issue/TapdIssue.java b/backend/src/main/java/io/metersphere/track/issue/TapdPlatform.java similarity index 98% rename from backend/src/main/java/io/metersphere/track/issue/TapdIssue.java rename to backend/src/main/java/io/metersphere/track/issue/TapdPlatform.java index 3df46f4291..2b01f5b468 100644 --- a/backend/src/main/java/io/metersphere/track/issue/TapdIssue.java +++ b/backend/src/main/java/io/metersphere/track/issue/TapdPlatform.java @@ -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); } diff --git a/backend/src/main/java/io/metersphere/track/issue/ZentaoPlatform.java b/backend/src/main/java/io/metersphere/track/issue/ZentaoPlatform.java new file mode 100644 index 0000000000..e0b7a99fa7 --- /dev/null +++ b/backend/src/main/java/io/metersphere/track/issue/ZentaoPlatform.java @@ -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 getIssue() { + List list = new ArrayList<>(); + + TestCaseIssuesExample example = new TestCaseIssuesExample(); + example.createCriteria().andTestCaseIdEqualTo(testCaseId); + + List issues = extIssuesMapper.getIssues(testCaseId, IssuesManagePlatform.Zentao.toString()); + + List 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 requestEntity = new HttpEntity<>(new HttpHeaders()); + RestTemplate restTemplate = new RestTemplate(); + ResponseEntity 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 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 requestEntity = new HttpEntity<>(paramMap, new HttpHeaders()); + RestTemplate restTemplate = new RestTemplate(); + System.out.println("zentao add bug"); + ResponseEntity 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 requestEntity = new HttpEntity<>(new HttpHeaders()); + RestTemplate restTemplate = new RestTemplate(); + ResponseEntity 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 paramMap = new LinkedMultiValueMap<>(); + paramMap.add("account", "admin"); + paramMap.add("password", "xx"); + + HttpEntity requestEntity = new HttpEntity<>(paramMap, new HttpHeaders()); + RestTemplate restTemplate = new RestTemplate(); + ResponseEntity responseEntity = restTemplate.exchange(loginUrl, HttpMethod.POST, requestEntity, String.class); + return session; + } + + private String getSession() { + RestTemplate restTemplate = new RestTemplate(); + HttpEntity requestEntity = new HttpEntity<>(new HttpHeaders()); + ResponseEntity 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 getPlatformUser() { + return null; + } +} diff --git a/backend/src/main/java/io/metersphere/track/issue/ZentaoUtils.java b/backend/src/main/java/io/metersphere/track/issue/ZentaoUtils.java new file mode 100644 index 0000000000..fa34a39224 --- /dev/null +++ b/backend/src/main/java/io/metersphere/track/issue/ZentaoUtils.java @@ -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); + } +}