From a27cca0733570f051c52dc7e873b7fd6d26dc660 Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Wed, 15 Sep 2021 16:54:46 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20jira=E3=80=81tapd=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=A4=8D=E5=88=B6=E7=BC=96=E8=BE=91=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../track/issue/AbstractIssuePlatform.java | 21 +++++-- .../metersphere/track/issue/IssueFactory.java | 14 +---- .../metersphere/track/issue/JiraPlatform.java | 50 ++++++++++------- .../track/issue/LocalPlatform.java | 5 -- .../metersphere/track/issue/TapdPlatform.java | 51 ++++++++++------- .../track/issue/ZentaoPlatform.java | 56 ++++++++++--------- .../issue/client/JiraAbstractClient.java | 19 +++++++ .../track/issue/client/TapdClient.java | 9 ++- .../track/service/IssuesService.java | 28 ++++------ .../components/track/issue/IssueList.vue | 6 +- 10 files changed, 152 insertions(+), 107 deletions(-) diff --git a/backend/src/main/java/io/metersphere/track/issue/AbstractIssuePlatform.java b/backend/src/main/java/io/metersphere/track/issue/AbstractIssuePlatform.java index f3923c13a5..c454aa1eeb 100644 --- a/backend/src/main/java/io/metersphere/track/issue/AbstractIssuePlatform.java +++ b/backend/src/main/java/io/metersphere/track/issue/AbstractIssuePlatform.java @@ -91,6 +91,14 @@ public abstract class AbstractIssuePlatform implements IssuesPlatform { } public AbstractIssuePlatform(IssuesRequest issuesRequest) { + this(); + this.testCaseId = issuesRequest.getTestCaseId(); + this.projectId = issuesRequest.getProjectId(); + this.orgId = issuesRequest.getOrganizationId(); + this.userId = issuesRequest.getUserId(); + } + + public AbstractIssuePlatform() { this.integrationService = CommonBeanFactory.getBean(IntegrationService.class); this.testCaseIssuesMapper = CommonBeanFactory.getBean(TestCaseIssuesMapper.class); this.projectService = CommonBeanFactory.getBean(ProjectService.class); @@ -99,10 +107,6 @@ public abstract class AbstractIssuePlatform implements IssuesPlatform { this.issuesMapper = CommonBeanFactory.getBean(IssuesMapper.class); this.extIssuesMapper = CommonBeanFactory.getBean(ExtIssuesMapper.class); this.resourceService = CommonBeanFactory.getBean(ResourceService.class); - this.testCaseId = issuesRequest.getTestCaseId(); - this.projectId = issuesRequest.getProjectId(); - this.orgId = issuesRequest.getOrganizationId(); - this.userId = issuesRequest.getUserId(); this.restTemplateIgnoreSSL = restTemplate; } @@ -324,4 +328,13 @@ public abstract class AbstractIssuePlatform implements IssuesPlatform { protected UserDTO.PlatformInfo getUserPlatInfo(String orgId) { return userService.getCurrentPlatformInfo(orgId); } + + @Override + public void deleteIssue(String id) { + issuesMapper.deleteByPrimaryKey(id); + TestCaseIssuesExample example = new TestCaseIssuesExample(); + example.createCriteria() + .andIssuesIdEqualTo(id); + testCaseIssuesMapper.deleteByExample(example); + } } 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 51e84a662c..bb5d36bd54 100644 --- a/backend/src/main/java/io/metersphere/track/issue/IssueFactory.java +++ b/backend/src/main/java/io/metersphere/track/issue/IssueFactory.java @@ -1,11 +1,11 @@ package io.metersphere.track.issue; import io.metersphere.commons.constants.IssuesManagePlatform; +import io.metersphere.commons.utils.LogUtil; import io.metersphere.track.request.testcase.IssuesRequest; import org.apache.commons.lang3.StringUtils; import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; import java.util.*; public class IssueFactory { @@ -23,16 +23,8 @@ public class IssueFactory { Constructor cons = clazz.getDeclaredConstructor(new Class[] { IssuesRequest.class }); AbstractIssuePlatform azureDevopsPlatform = (AbstractIssuePlatform) cons.newInstance(addIssueRequest); return azureDevopsPlatform; - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); + } catch (Throwable e) { + LogUtil.error(e); } } else if (StringUtils.equalsIgnoreCase(IssuesManagePlatform.Local.toString(), platform)) { return new LocalPlatform(addIssueRequest); 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 fc23e2a6d7..48de181322 100644 --- a/backend/src/main/java/io/metersphere/track/issue/JiraPlatform.java +++ b/backend/src/main/java/io/metersphere/track/issue/JiraPlatform.java @@ -9,7 +9,6 @@ import io.metersphere.commons.constants.IssuesStatus; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.EncryptUtils; import io.metersphere.commons.utils.LogUtil; -import io.metersphere.commons.utils.SessionUtils; import io.metersphere.dto.CustomFieldItemDTO; import io.metersphere.dto.UserDTO; import io.metersphere.track.dto.DemandDTO; @@ -197,6 +196,30 @@ public class JiraPlatform extends AbstractIssuePlatform { @Override public void addIssue(IssuesUpdateRequest issuesRequest) { + + JSONObject addJiraIssueParam = buildUpdateParam(issuesRequest); + + JiraAddIssueResponse result = jiraClientV2.addIssue(JSONObject.toJSONString(addJiraIssueParam)); + JiraIssue issues = jiraClientV2.getIssues(result.getId()); + + List imageFiles = getImageFiles(issuesRequest.getDescription()); + + imageFiles.forEach(img -> { + jiraClientV2.uploadAttachment(result.getKey(), img); + }); + String status = getStatus(issues.getFields()); + issuesRequest.setPlatformStatus(status); + + issuesRequest.setId(result.getKey()); + // 用例与第三方缺陷平台中的缺陷关联 + handleTestCaseIssues(issuesRequest); + + // 插入缺陷表 + insertIssues(result.getKey(), issuesRequest); + } + + private JSONObject buildUpdateParam(IssuesUpdateRequest issuesRequest) { + issuesRequest.setPlatform(IssuesManagePlatform.Jira.toString()); JiraConfig config = getUserConfig(); @@ -208,7 +231,6 @@ public class JiraPlatform extends AbstractIssuePlatform { JSONObject project = new JSONObject(); String desc = issuesRequest.getDescription(); - List imageFiles = getImageFiles(desc); desc = removeImage(desc); fields.put("project", project); @@ -238,7 +260,7 @@ public class JiraPlatform extends AbstractIssuePlatform { fields.put(item.getCustomData(), param); } else if (StringUtils.isNotBlank(item.getType()) && StringUtils.equalsAny(item.getType(), "multipleSelect", "checkbox", "multipleMember")) { - JSONArray attrs = new JSONArray(); + JSONArray attrs = new JSONArray(); if (StringUtils.isNotBlank(item.getValue())) { JSONArray values = JSONObject.parseArray(item.getValue()); values.forEach(v -> { @@ -254,32 +276,22 @@ public class JiraPlatform extends AbstractIssuePlatform { } } }); - JiraAddIssueResponse result = jiraClientV2.addIssue(JSONObject.toJSONString(addJiraIssueParam)); - JiraIssue issues = jiraClientV2.getIssues(result.getId()); - imageFiles.forEach(img -> { - jiraClientV2.uploadAttachment(result.getKey(), img); - }); - String status = getStatus(issues.getFields()); - issuesRequest.setPlatformStatus(status); - - issuesRequest.setId(result.getKey()); - // 用例与第三方缺陷平台中的缺陷关联 - handleTestCaseIssues(issuesRequest); - - // 插入缺陷表 - insertIssues(result.getKey(), issuesRequest); + return addJiraIssueParam; } @Override public void updateIssue(IssuesUpdateRequest request) { - // todo 调用接口 - request.setDescription(null); + JSONObject param = buildUpdateParam(request); handleIssueUpdate(request); + jiraClientV2.updateIssue(request.getId(), JSONObject.toJSONString(param)); } @Override public void deleteIssue(String id) { + super.deleteIssue(id); + setConfig(); + jiraClientV2.deleteIssue(id); } @Override diff --git a/backend/src/main/java/io/metersphere/track/issue/LocalPlatform.java b/backend/src/main/java/io/metersphere/track/issue/LocalPlatform.java index 1c09bb2c36..b544f5ac6f 100644 --- a/backend/src/main/java/io/metersphere/track/issue/LocalPlatform.java +++ b/backend/src/main/java/io/metersphere/track/issue/LocalPlatform.java @@ -60,9 +60,4 @@ public class LocalPlatform extends LocalAbstractPlatform { public void updateIssue(IssuesUpdateRequest request) { handleIssueUpdate(request); } - - @Override - public void deleteIssue(String id) { - issuesMapper.deleteByPrimaryKey(id); - } } diff --git a/backend/src/main/java/io/metersphere/track/issue/TapdPlatform.java b/backend/src/main/java/io/metersphere/track/issue/TapdPlatform.java index 890ada36ad..e5d1c944ce 100644 --- a/backend/src/main/java/io/metersphere/track/issue/TapdPlatform.java +++ b/backend/src/main/java/io/metersphere/track/issue/TapdPlatform.java @@ -86,7 +86,31 @@ public class TapdPlatform extends AbstractIssuePlatform { @Override public void addIssue(IssuesUpdateRequest issuesRequest) { + + MultiValueMap param = buildUpdateParam(issuesRequest); + TapdBug bug = tapdClient.addIssue(param); + Map statusMap = tapdClient.getStatusMap(getProjectId(this.projectId)); + issuesRequest.setPlatformStatus(statusMap.get(bug.getStatus())); + + issuesRequest.setId(bug.getId()); + // 用例与第三方缺陷平台中的缺陷关联 + handleTestCaseIssues(issuesRequest); + + // 插入缺陷表 + insertIssues(bug.getId(), issuesRequest); + } + + @Override + public void updateIssue(IssuesUpdateRequest request) { + MultiValueMap param = buildUpdateParam(request); + param.add("id", request.getId()); + handleIssueUpdate(request); + tapdClient.updateIssue(param); + } + + private MultiValueMap buildUpdateParam(IssuesUpdateRequest issuesRequest) { issuesRequest.setPlatform(IssuesManagePlatform.Tapd.toString()); + setConfig(); List customFields = getCustomFields(issuesRequest.getCustomFields()); @@ -107,7 +131,7 @@ public class TapdPlatform extends AbstractIssuePlatform { reporter = SessionUtils.getUser().getName(); } - MultiValueMap paramMap = new LinkedMultiValueMap<>(); + MultiValueMap paramMap = new LinkedMultiValueMap<>(); paramMap.add("title", issuesRequest.getTitle()); paramMap.add("workspace_id", tapdId); paramMap.add("description", msDescription2Tapd(issuesRequest.getDescription())); @@ -119,25 +143,7 @@ public class TapdPlatform extends AbstractIssuePlatform { } }); paramMap.add("reporter", reporter); - - setConfig(); - TapdBug bug = tapdClient.addIssue(paramMap); - Map statusMap = tapdClient.getStatusMap(getProjectId(this.projectId)); - issuesRequest.setPlatformStatus(statusMap.get(bug.getStatus())); - - issuesRequest.setId(bug.getId()); - // 用例与第三方缺陷平台中的缺陷关联 - handleTestCaseIssues(issuesRequest); - - // 插入缺陷表 - insertIssues(bug.getId(), issuesRequest); - } - - @Override - public void updateIssue(IssuesUpdateRequest request) { - // todo 调用接口 - request.setDescription(null); - handleIssueUpdate(request); + return paramMap; } private String msDescription2Tapd(String msDescription) { @@ -147,7 +153,10 @@ public class TapdPlatform extends AbstractIssuePlatform { } @Override - public void deleteIssue(String id) {} + public void deleteIssue(String id) { + super.deleteIssue(id); + // todo 暂无删除API + } @Override public void testAuth() { diff --git a/backend/src/main/java/io/metersphere/track/issue/ZentaoPlatform.java b/backend/src/main/java/io/metersphere/track/issue/ZentaoPlatform.java index 0824152bc4..742898d879 100644 --- a/backend/src/main/java/io/metersphere/track/issue/ZentaoPlatform.java +++ b/backend/src/main/java/io/metersphere/track/issue/ZentaoPlatform.java @@ -172,6 +172,35 @@ public class ZentaoPlatform extends AbstractIssuePlatform { @Override public void addIssue(IssuesUpdateRequest issuesRequest) { + + MultiValueMap param = buildUpdateParam(issuesRequest); + AddIssueResponse.Issue issue = zentaoClient.addIssue(param); + issuesRequest.setPlatformStatus(issue.getStatus()); + + String id = issue.getId(); + if (StringUtils.isNotBlank(id)) { + issuesRequest.setId(id); + // 用例与第三方缺陷平台中的缺陷关联 + handleTestCaseIssues(issuesRequest); + + IssuesExample issuesExample = new IssuesExample(); + issuesExample.createCriteria().andIdEqualTo(id) + .andPlatformEqualTo(IssuesManagePlatform.Zentao.toString()); + if (issuesMapper.selectByExample(issuesExample).size() <= 0) { + // 插入缺陷表 + insertIssues(id, issuesRequest); + } + } + } + + @Override + public void updateIssue(IssuesUpdateRequest request) { + // todo 调用接口 + request.setDescription(null); + handleIssueUpdate(request); + } + + private MultiValueMap buildUpdateParam(IssuesUpdateRequest issuesRequest) { issuesRequest.setPlatform(IssuesManagePlatform.Zentao.toString()); List customFields = getCustomFields(issuesRequest.getCustomFields()); @@ -212,32 +241,7 @@ public class ZentaoPlatform extends AbstractIssuePlatform { if (StringUtils.isNotBlank(issuesRequest.getZentaoAssigned())) { paramMap.add("assignedTo", issuesRequest.getZentaoAssigned()); } - - AddIssueResponse.Issue issue = zentaoClient.addIssue(paramMap); - issuesRequest.setPlatformStatus(issue.getStatus()); - - String id = issue.getId(); - if (StringUtils.isNotBlank(id)) { - issuesRequest.setId(id); - // 用例与第三方缺陷平台中的缺陷关联 - handleTestCaseIssues(issuesRequest); - - IssuesExample issuesExample = new IssuesExample(); - issuesExample.createCriteria().andIdEqualTo(id) - .andPlatformEqualTo(IssuesManagePlatform.Zentao.toString()); - if (issuesMapper.selectByExample(issuesExample).size() <= 0) { - // 插入缺陷表 - insertIssues(id, issuesRequest); - } - } - - } - - @Override - public void updateIssue(IssuesUpdateRequest request) { - // todo 调用接口 - request.setDescription(null); - handleIssueUpdate(request); + return paramMap; } @Override diff --git a/backend/src/main/java/io/metersphere/track/issue/client/JiraAbstractClient.java b/backend/src/main/java/io/metersphere/track/issue/client/JiraAbstractClient.java index 0998b5dd36..ed5f770166 100644 --- a/backend/src/main/java/io/metersphere/track/issue/client/JiraAbstractClient.java +++ b/backend/src/main/java/io/metersphere/track/issue/client/JiraAbstractClient.java @@ -52,6 +52,25 @@ public abstract class JiraAbstractClient extends BaseClient { return (JiraAddIssueResponse) getResultForObject(JiraAddIssueResponse.class, response); } + public void updateIssue(String id, String body) { + LogUtil.info("addIssue: " + body); + HttpHeaders headers = getAuthHeader(); + headers.setContentType(MediaType.APPLICATION_JSON); + HttpEntity requestEntity = new HttpEntity<>(body, headers); + try { + restTemplate.exchange(getBaseUrl() + "/issue/" + id, HttpMethod.PUT, requestEntity, String.class); + } catch (Exception e) { + LogUtil.error(e.getMessage(), e); + MSException.throwException(e.getMessage()); + } + } + + public void deleteIssue(String id) { + LogUtil.info("deleteIssue: " + id); + restTemplate.exchange(getBaseUrl() + "/issue/" + id, HttpMethod.DELETE, getAuthHttpEntity(), String.class); + } + + public void uploadAttachment(String issueKey, File file) { HttpHeaders authHeader = getAuthHeader(); authHeader.add("X-Atlassian-Token", "no-check"); diff --git a/backend/src/main/java/io/metersphere/track/issue/client/TapdClient.java b/backend/src/main/java/io/metersphere/track/issue/client/TapdClient.java index e5289cd1d4..decda1d9c6 100644 --- a/backend/src/main/java/io/metersphere/track/issue/client/TapdClient.java +++ b/backend/src/main/java/io/metersphere/track/issue/client/TapdClient.java @@ -57,9 +57,9 @@ public class TapdClient extends BaseClient { return (TapdGetIssueResponse) getResultForObject(TapdGetIssueResponse.class, response); } - public TapdBug addIssue(MultiValueMap paramMap) { + public TapdBug addIssue(MultiValueMap paramMap) { String url = getBaseUrl() + "/bugs"; - HttpEntity> requestEntity = new HttpEntity<>(paramMap, getAuthHeader()); + HttpEntity> requestEntity = new HttpEntity<>(paramMap, getAuthHeader()); ResponseEntity response = null; try { response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class); @@ -70,6 +70,11 @@ public class TapdClient extends BaseClient { return ((AddTapdIssueResponse) getResultForObject(AddTapdIssueResponse.class, response)).getData().getBug(); } + public TapdBug updateIssue(MultiValueMap paramMap) { + // 带id为更新 + return addIssue(paramMap); + } + protected HttpEntity getAuthHttpEntity() { return new HttpEntity<>(getAuthHeader()); } 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 83107450f6..6df3bc81ba 100644 --- a/backend/src/main/java/io/metersphere/track/service/IssuesService.java +++ b/backend/src/main/java/io/metersphere/track/service/IssuesService.java @@ -43,7 +43,6 @@ import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; import java.util.*; import java.util.function.BiConsumer; import java.util.stream.Collectors; @@ -295,11 +294,16 @@ public class IssuesService { } public void delete(String id) { - issuesMapper.deleteByPrimaryKey(id); - TestCaseIssuesExample example = new TestCaseIssuesExample(); - example.createCriteria() - .andIssuesIdEqualTo(id); - testCaseIssuesMapper.deleteByExample(example); + IssuesWithBLOBs issuesWithBLOBs = issuesMapper.selectByPrimaryKey(id); + List platforms = new ArrayList<>(); + platforms.add(issuesWithBLOBs.getPlatform()); + String projectId = issuesWithBLOBs.getProjectId(); + Project project = projectService.getProjectById(projectId); + Workspace workspace = workspaceMapper.selectByPrimaryKey(project.getWorkspaceId()); + IssuesRequest issuesRequest = new IssuesRequest(); + issuesRequest.setOrganizationId(workspace.getOrganizationId()); + AbstractIssuePlatform platform = IssueFactory.createPlatform(issuesWithBLOBs.getPlatform(), issuesRequest); + platform.deleteIssue(id); } public IssuesWithBLOBs get(String id) { @@ -461,16 +465,8 @@ public class IssuesService { Constructor cons = clazz.getDeclaredConstructor(new Class[]{IssuesRequest.class}); AbstractIssuePlatform azureDevopsPlatform = (AbstractIssuePlatform) cons.newInstance(issuesRequest); syncThirdPartyIssues(azureDevopsPlatform::syncIssues, project, azureDevopsIssues); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); + } catch (Throwable e) { + LogUtil.error(e); } } } diff --git a/frontend/src/business/components/track/issue/IssueList.vue b/frontend/src/business/components/track/issue/IssueList.vue index 1e39ebf5b2..6fe5e19ad0 100644 --- a/frontend/src/business/components/track/issue/IssueList.vue +++ b/frontend/src/business/components/track/issue/IssueList.vue @@ -314,10 +314,10 @@ export default { }); }, btnDisable(row) { - if (row.platform === 'Local') { + // if (row.platform === 'Local' || row.platform === 'Jira') { return false; - } - return true; + // } + // return true; }, syncIssues() { this.page.result = syncIssues(() => {