From 86b10f72d79d9240c9e1d15eb87b5fede70a98b5 Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Thu, 10 Jun 2021 13:49:42 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20Jira=E6=8E=A5=E5=8F=A3=E4=BD=BF?= =?UTF-8?q?=E7=94=A8v2=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../metersphere/track/issue/JiraPlatform.java | 159 ++++-------------- .../track/issue/client/BaseClient.java | 16 +- ...iraClient.java => JiraAbstractClient.java} | 66 ++++---- .../track/issue/client/JiraClientV2.java | 10 ++ .../track/issue/client/JiraClientV3.java | 7 + ...ption.java => JiraIssueDescriptionV3.java} | 4 +- .../track/service/IssuesService.java | 10 +- frontend/src/network/Issue.js | 2 + 8 files changed, 101 insertions(+), 173 deletions(-) rename backend/src/main/java/io/metersphere/track/issue/client/{JiraClient.java => JiraAbstractClient.java} (54%) create mode 100644 backend/src/main/java/io/metersphere/track/issue/client/JiraClientV2.java create mode 100644 backend/src/main/java/io/metersphere/track/issue/client/JiraClientV3.java rename backend/src/main/java/io/metersphere/track/issue/domain/{JiraIssueDescription.java => JiraIssueDescriptionV3.java} (92%) diff --git a/backend/src/main/java/io/metersphere/track/issue/JiraPlatform.java b/backend/src/main/java/io/metersphere/track/issue/JiraPlatform.java index 9afb85ac3b..ca6966626a 100644 --- a/backend/src/main/java/io/metersphere/track/issue/JiraPlatform.java +++ b/backend/src/main/java/io/metersphere/track/issue/JiraPlatform.java @@ -11,8 +11,11 @@ import io.metersphere.commons.utils.EncryptUtils; import io.metersphere.commons.utils.LogUtil; import io.metersphere.dto.CustomFieldItemDTO; import io.metersphere.track.dto.DemandDTO; -import io.metersphere.track.issue.client.JiraClient; -import io.metersphere.track.issue.domain.*; +import io.metersphere.track.issue.client.JiraClientV2; +import io.metersphere.track.issue.domain.JiraAddIssueResponse; +import io.metersphere.track.issue.domain.JiraConfig; +import io.metersphere.track.issue.domain.JiraIssue; +import io.metersphere.track.issue.domain.PlatformUser; import io.metersphere.track.request.testcase.IssuesRequest; import io.metersphere.track.request.testcase.IssuesUpdateRequest; import org.apache.commons.lang3.StringUtils; @@ -26,18 +29,17 @@ import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; -import org.springframework.util.MultiValueMap; -import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestTemplate; import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; public class JiraPlatform extends AbstractIssuePlatform { protected String key = IssuesManagePlatform.Jira.toString(); + private JiraClientV2 jiraClientV2 = new JiraClientV2(); + public JiraPlatform(IssuesRequest issuesRequest) { super(issuesRequest); } @@ -62,11 +64,10 @@ public class JiraPlatform extends AbstractIssuePlatform { } else { issues = extIssuesMapper.getIssuesByCaseId(issuesRequest); } - JiraConfig config = getConfig(); - JiraClient.setConfig(config); + setConfig(); issues.forEach(item -> { String issuesId = item.getId(); - parseIssue(item, JiraClient.getIssues(issuesId)); + parseIssue(item, jiraClientV2.getIssues(issuesId)); if (StringUtils.isBlank(item.getId())) { // 缺陷不存在,解除用例和缺陷的关联 TestCaseIssuesExample issuesExample = new TestCaseIssuesExample(); @@ -85,7 +86,7 @@ public class JiraPlatform extends AbstractIssuePlatform { return list; } - public void parseIssue(IssuesDao item, JiraIssue jiraIssue) { + public void parseIssue(IssuesWithBLOBs item, JiraIssue jiraIssue) { String lastmodify = ""; String status = ""; JSONObject fields = jiraIssue.getFields(); @@ -115,31 +116,17 @@ public class JiraPlatform extends AbstractIssuePlatform { item.setPlatform(IssuesManagePlatform.Jira.toString()); } - public HttpHeaders getAuthHeader(JSONObject object) { - if (object == null) { - MSException.throwException("tapd config is null"); - } - - String account = object.getString("account"); - String password = object.getString("password"); - return auth(account, password); - } - @Override public void filter(List issues) { - String config = getPlatformConfig(IssuesManagePlatform.Jira.toString()); - JSONObject object = JSON.parseObject(config); - HttpHeaders headers = getAuthHeader(object); - String url = object.getString("url"); - + setConfig(); issues.forEach((issuesDao) -> { - IssuesDao dto = getJiraIssues(headers, url, issuesDao.getId()); - if (StringUtils.isBlank(dto.getId())) { + parseIssue(issuesDao, jiraClientV2.getIssues(issuesDao.getId())); + if (StringUtils.isBlank(issuesDao.getId())) { // 标记成删除 issuesDao.setStatus(IssuesStatus.DELETE.toString()); } else { // 缺陷状态为 完成,则不显示 - if (!StringUtils.equals("done", dto.getStatus())) { + if (!StringUtils.equals("done", issuesDao.getStatus())) { issuesDao.setStatus(IssuesStatus.RESOLVED.toString()); } } @@ -204,6 +191,8 @@ public class JiraPlatform extends AbstractIssuePlatform { issuesRequest.setPlatform(IssuesManagePlatform.Jira.toString()); JiraConfig config = getConfig(); + jiraClientV2.setConfig(config); + if (config == null) { MSException.throwException("jira config is null"); } @@ -227,7 +216,6 @@ public class JiraPlatform extends AbstractIssuePlatform { String desc = Jsoup.clean(s, "", Whitelist.none(), new Document.OutputSettings().prettyPrint(false)); desc = desc.replace(" ", ""); - JSONObject addJiraIssueParam = new JSONObject(); JSONObject fields = new JSONObject(); JSONObject project = new JSONObject(); @@ -239,13 +227,16 @@ public class JiraPlatform extends AbstractIssuePlatform { issuetype.put("name", config.getIssuetype()); fields.put("summary", issuesRequest.getTitle()); - fields.put("description", new JiraIssueDescription(desc)); +// fields.put("description", new JiraIssueDescription(desc)); + fields.put("description", desc); fields.put("issuetype", issuetype); + + JSONObject addJiraIssueParam = new JSONObject(); addJiraIssueParam.put("fields", fields); List customFields = getCustomFields(issuesRequest.getCustomFields()); - JiraClient.setConfig(config); -// List jiraFields = JiraClient.getFields(); + jiraClientV2.setConfig(config); +// List jiraFields = JiraClientV2.getFields(); // Map isCustomMap = jiraFields.stream(). // collect(Collectors.toMap(JiraField::getId, JiraField::isCustom)); @@ -261,7 +252,7 @@ public class JiraPlatform extends AbstractIssuePlatform { // } } }); - JiraAddIssueResponse result = JiraClient.addIssue(JSONObject.toJSONString(addJiraIssueParam)); + JiraAddIssueResponse result = jiraClientV2.addIssue(JSONObject.toJSONString(addJiraIssueParam)); issuesRequest.setId(result.getKey()); // 用例与第三方缺陷平台中的缺陷关联 @@ -278,46 +269,14 @@ public class JiraPlatform extends AbstractIssuePlatform { handleIssueUpdate(request); } - private String addJiraIssue(String url, String auth, String json) { - HttpHeaders requestHeaders = new HttpHeaders(); - requestHeaders.add("Authorization", "Basic " + auth); - requestHeaders.setContentType(org.springframework.http.MediaType.APPLICATION_JSON); - //HttpEntity - HttpEntity requestEntity = new HttpEntity<>(json, requestHeaders); - RestTemplate restTemplate = new RestTemplate(); - //post - ResponseEntity responseEntity = null; - try { - responseEntity = restTemplate.exchange(url + "/rest/api/2/issue", HttpMethod.POST, requestEntity, String.class); - } catch (Exception e) { - LogUtil.error(e.getMessage(), e); - MSException.throwException("调用Jira接口创建缺陷失败"); - } - - return responseEntity.getBody(); - } - @Override public void deleteIssue(String id) { - } @Override public void testAuth() { - try { - String config = getPlatformConfig(IssuesManagePlatform.Jira.toString()); - JSONObject object = JSON.parseObject(config); - String account = object.getString("account"); - String password = object.getString("password"); - String url = object.getString("url"); - HttpHeaders headers = auth(account, password); - HttpEntity requestEntity = new HttpEntity<>(headers); - // 忽略ssl - restTemplateIgnoreSSL.exchange(url + "rest/api/2/issue/createmeta", HttpMethod.GET, requestEntity, String.class); - } catch (Exception e) { - LogUtil.error(e.getMessage(), e); - MSException.throwException("验证失败!"); - } + setConfig(); + jiraClientV2.getIssueCreateMetadata(); } @Override @@ -335,66 +294,18 @@ public class JiraPlatform extends AbstractIssuePlatform { return project.getJiraKey(); } - public IssuesDao getJiraIssues(HttpHeaders headers, String url, String issuesId) { - HttpEntity requestEntity = new HttpEntity<>(headers); - RestTemplate restTemplate = new RestTemplate(); - //post - ResponseEntity responseEntity; - IssuesDao issues = new IssuesDao(); - try { - responseEntity = restTemplate.exchange(url + "/rest/api/2/issue/" + issuesId, HttpMethod.GET, requestEntity, String.class); - String body = responseEntity.getBody(); + public void setConfig() { + JiraConfig config = getConfig(); + jiraClientV2.setConfig(config); + } - JSONObject obj = JSONObject.parseObject(body); - LogUtil.info(obj); - - String lastmodify = ""; - String status = ""; - - JSONObject fields = (JSONObject) obj.get("fields"); - JSONObject statusObj = (JSONObject) fields.get("status"); - JSONObject assignee = (JSONObject) fields.get("assignee"); - - if (statusObj != null) { - JSONObject statusCategory = (JSONObject) statusObj.get("statusCategory"); - status = statusCategory.getString("key"); - } - - String id = obj.getString("key"); - String title = fields.getString("summary"); - String description = fields.getString("description"); - - Parser parser = Parser.builder().build(); - Node document = parser.parse(description); - HtmlRenderer renderer = HtmlRenderer.builder().build(); - description = renderer.render(document); - - Long createTime = fields.getLong("created"); - - if (assignee != null) { - lastmodify = assignee.getString("displayName"); - } - - issues.setId(id); - issues.setTitle(title); - issues.setCreateTime(createTime); - issues.setLastmodify(lastmodify); - issues.setDescription(description); - issues.setStatus(status); - issues.setPlatform(IssuesManagePlatform.Jira.toString()); - } catch (HttpClientErrorException.NotFound e) { - LogUtil.error(e.getStackTrace(), e); - return new IssuesDao(); - } catch (HttpClientErrorException.Unauthorized e) { - LogUtil.error(e.getStackTrace(), e); - MSException.throwException("获取Jira缺陷失败,检查Jira配置信息"); - } catch (Exception e) { - LogUtil.error(e.getMessage(), e); - MSException.throwException("调用Jira接口获取缺陷失败"); + public IssuesWithBLOBs getJiraIssues(IssuesWithBLOBs issuesDao, String issueId) { + setConfig(); + if (issuesDao == null) { + issuesDao = new IssuesDao(); } - - return issues; - + parseIssue(issuesDao, jiraClientV2.getIssues(issueId)); + return issuesDao; } } diff --git a/backend/src/main/java/io/metersphere/track/issue/client/BaseClient.java b/backend/src/main/java/io/metersphere/track/issue/client/BaseClient.java index 6afb711b26..ab1f438975 100644 --- a/backend/src/main/java/io/metersphere/track/issue/client/BaseClient.java +++ b/backend/src/main/java/io/metersphere/track/issue/client/BaseClient.java @@ -20,9 +20,9 @@ import java.util.Arrays; public abstract class BaseClient { - protected static RestTemplate restTemplate; + protected RestTemplate restTemplate; - static { + { try { TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true; SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom() @@ -41,28 +41,28 @@ public abstract class BaseClient { } } - protected static HttpHeaders getBasicHttpHeaders(String userName, String passWd) { + protected HttpHeaders getBasicHttpHeaders(String userName, String passWd) { String authKey = EncryptUtils.base64Encoding(userName + ":" + passWd); HttpHeaders headers = new HttpHeaders(); headers.add("Authorization", "Basic " + authKey); return headers; } - protected static String getResult(ResponseEntity response) { + protected String getResult(ResponseEntity response) { int statusCodeValue = response.getStatusCodeValue(); - LogUtil.info("responseCode: " + statusCodeValue); + LogUtil.debug("responseCode: " + statusCodeValue); if(statusCodeValue >= 400){ MSException.throwException(response.getBody()); } - LogUtil.info("result: " + response.getBody()); + LogUtil.debug("result: " + response.getBody()); return response.getBody(); } - protected static Object getResultForList(Class clazz, ResponseEntity response) { + protected Object getResultForList(Class clazz, ResponseEntity response) { return Arrays.asList(JSONArray.parseArray(getResult(response), clazz).toArray()); } - protected static Object getResultForObject(Class clazz,ResponseEntity response) { + protected Object getResultForObject(Class clazz,ResponseEntity response) { return JSONObject.parseObject(getResult(response), clazz); } } diff --git a/backend/src/main/java/io/metersphere/track/issue/client/JiraClient.java b/backend/src/main/java/io/metersphere/track/issue/client/JiraAbstractClient.java similarity index 54% rename from backend/src/main/java/io/metersphere/track/issue/client/JiraClient.java rename to backend/src/main/java/io/metersphere/track/issue/client/JiraAbstractClient.java index f4a329464f..636c8c3d2f 100644 --- a/backend/src/main/java/io/metersphere/track/issue/client/JiraClient.java +++ b/backend/src/main/java/io/metersphere/track/issue/client/JiraAbstractClient.java @@ -1,6 +1,7 @@ package io.metersphere.track.issue.client; import io.metersphere.commons.exception.MSException; +import io.metersphere.commons.utils.LogUtil; import io.metersphere.track.issue.domain.JiraAddIssueResponse; import io.metersphere.track.issue.domain.JiraConfig; import io.metersphere.track.issue.domain.JiraField; @@ -11,38 +12,55 @@ import org.springframework.util.MultiValueMap; import java.util.List; -public class JiraClient extends BaseClient { +public abstract class JiraAbstractClient extends BaseClient { - private static String ENDPOINT; + protected String ENDPOINT; - private static String PREFIX = "/rest/api/3"; + protected String PREFIX; - private static String USER_NAME; + protected String USER_NAME; - private static String PASSWD; + protected String PASSWD; - public static List getFields() { - String url = getBaseUrl() + "/field"; - ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, getAuthHttpEntity(), String.class); - return (List) getResultForList(JiraField.class, response); - } - - public static JiraIssue getIssues(String issuesId) { - HttpEntity requestEntity = getAuthHttpEntity(); + public JiraIssue getIssues(String issuesId) { + LogUtil.debug("getIssues: " + issuesId); ResponseEntity responseEntity; - responseEntity = restTemplate.exchange(ENDPOINT + "/rest/api/2/issue/" + issuesId, HttpMethod.GET, requestEntity, String.class); + responseEntity = restTemplate.exchange(getBaseUrl() + "/issue/" + issuesId, HttpMethod.GET, getAuthHttpEntity(), String.class); return (JiraIssue) getResultForObject(JiraIssue.class, responseEntity); } - public static JiraAddIssueResponse addIssue(String body) { + public List getFields() { + ResponseEntity response = restTemplate.exchange(getBaseUrl() + "/field", HttpMethod.GET, getAuthHttpEntity(), String.class); + return (List) getResultForList(JiraField.class, response); + } + + public JiraAddIssueResponse addIssue(String body) { + LogUtil.debug("addIssue: " + body); HttpHeaders headers = getAuthHeader(); headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity requestEntity = new HttpEntity<>(body, headers); - ResponseEntity entity = restTemplate.exchange(getBaseUrl() + "/issue", HttpMethod.POST, requestEntity, String.class); - return (JiraAddIssueResponse) getResultForObject(JiraAddIssueResponse.class, entity); + ResponseEntity response = restTemplate.exchange(getBaseUrl() + "/issue", HttpMethod.POST, requestEntity, String.class); + return (JiraAddIssueResponse) getResultForObject(JiraAddIssueResponse.class, response); } - public static void setConfig(JiraConfig config) { + public String getIssueCreateMetadata() { + ResponseEntity response = restTemplate.exchange(getBaseUrl() + "/createmeta", HttpMethod.GET, getAuthHttpEntity(), String.class); + return (String) getResultForObject(String.class, response); + } + + protected HttpEntity getAuthHttpEntity() { + return new HttpEntity<>(getAuthHeader()); + } + + protected HttpHeaders getAuthHeader() { + return getBasicHttpHeaders(USER_NAME, PASSWD); + } + + protected String getBaseUrl() { + return ENDPOINT + PREFIX; + } + + public void setConfig(JiraConfig config) { if (config == null) { MSException.throwException("config is null"); } @@ -55,16 +73,4 @@ public class JiraClient extends BaseClient { USER_NAME = config.getAccount(); PASSWD = config.getPassword(); } - - private static HttpEntity getAuthHttpEntity() { - return new HttpEntity<>(getAuthHeader()); - } - - private static HttpHeaders getAuthHeader() { - return getBasicHttpHeaders(USER_NAME, PASSWD); - } - - private static String getBaseUrl() { - return ENDPOINT + PREFIX; - } } diff --git a/backend/src/main/java/io/metersphere/track/issue/client/JiraClientV2.java b/backend/src/main/java/io/metersphere/track/issue/client/JiraClientV2.java new file mode 100644 index 0000000000..d5e0d4756c --- /dev/null +++ b/backend/src/main/java/io/metersphere/track/issue/client/JiraClientV2.java @@ -0,0 +1,10 @@ +package io.metersphere.track.issue.client; + +import org.springframework.stereotype.Component; + +@Component +public class JiraClientV2 extends JiraAbstractClient { + { + PREFIX = "/rest/api/2"; + } +} diff --git a/backend/src/main/java/io/metersphere/track/issue/client/JiraClientV3.java b/backend/src/main/java/io/metersphere/track/issue/client/JiraClientV3.java new file mode 100644 index 0000000000..cb1a225abf --- /dev/null +++ b/backend/src/main/java/io/metersphere/track/issue/client/JiraClientV3.java @@ -0,0 +1,7 @@ +package io.metersphere.track.issue.client; + +public class JiraClientV3 extends JiraAbstractClient { + { + PREFIX = "/rest/api/3"; + } +} diff --git a/backend/src/main/java/io/metersphere/track/issue/domain/JiraIssueDescription.java b/backend/src/main/java/io/metersphere/track/issue/domain/JiraIssueDescriptionV3.java similarity index 92% rename from backend/src/main/java/io/metersphere/track/issue/domain/JiraIssueDescription.java rename to backend/src/main/java/io/metersphere/track/issue/domain/JiraIssueDescriptionV3.java index bcb08bdd16..5d5aa9b1e8 100644 --- a/backend/src/main/java/io/metersphere/track/issue/domain/JiraIssueDescription.java +++ b/backend/src/main/java/io/metersphere/track/issue/domain/JiraIssueDescriptionV3.java @@ -8,12 +8,12 @@ import java.util.List; @Data @NoArgsConstructor -public class JiraIssueDescription { +public class JiraIssueDescriptionV3 { private String type; private int version; private List content; - public JiraIssueDescription(String text) { + public JiraIssueDescriptionV3(String text) { List list = new ArrayList<>(); Content content = new Content(text); list.add(content); diff --git a/backend/src/main/java/io/metersphere/track/service/IssuesService.java b/backend/src/main/java/io/metersphere/track/service/IssuesService.java index 8098038f3d..2a0d7d60ce 100644 --- a/backend/src/main/java/io/metersphere/track/service/IssuesService.java +++ b/backend/src/main/java/io/metersphere/track/service/IssuesService.java @@ -33,7 +33,6 @@ import io.metersphere.track.request.testcase.IssuesUpdateRequest; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.context.annotation.Lazy; -import org.springframework.http.HttpHeaders; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -380,14 +379,7 @@ public class IssuesService { issue.setStatus(tapdIssues.getStatus()); } else if (StringUtils.equals(platform, IssuesManagePlatform.Jira.name())) { JiraPlatform jiraPlatform = new JiraPlatform(new IssuesRequest()); - String config = getConfig(orgId, IssuesManagePlatform.Jira.toString()); - JSONObject object = JSON.parseObject(config); - HttpHeaders headers = jiraPlatform.getAuthHeader(object); - String url = object.getString("url"); - IssuesDao jiraIssues = jiraPlatform.getJiraIssues(headers, url, issue.getId()); - issue.setTitle(jiraIssues.getTitle()); - issue.setDescription(jiraIssues.getDescription()); - issue.setStatus(jiraIssues.getStatus()); + jiraPlatform.getJiraIssues(issue, issue.getId()); } else if (StringUtils.equals(platform, IssuesManagePlatform.Zentao.name())) { String config = getConfig(orgId, IssuesManagePlatform.Zentao.toString()); JSONObject object = JSON.parseObject(config); diff --git a/frontend/src/network/Issue.js b/frontend/src/network/Issue.js index 76e52e5385..4699d25337 100644 --- a/frontend/src/network/Issue.js +++ b/frontend/src/network/Issue.js @@ -32,12 +32,14 @@ export function getIssuesByCaseId(caseId, page) { } export function buildPlatformIssue(data) { + data.customFields = JSON.stringify(data.customFields); return post("issues/get/platform/issue", data).then(response => { let issues = response.data.data; if (issues) { data.title = issues.title ? issues.title : '--'; data.description = issues.description ? issues.description : '--'; data.status = issues.status ? issues.status : 'delete'; + data.customFields = JSON.parse(data.customFields); } }).catch(() => { data.title = '--';