refactor: Jira接口使用v2版本

This commit is contained in:
chenjianxing 2021-06-10 13:49:42 +08:00 committed by jianxing
parent 346c4d3253
commit 86b10f72d7
8 changed files with 101 additions and 173 deletions

View File

@ -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<IssuesDao> 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("&nbsp;", "");
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<CustomFieldItemDTO> customFields = getCustomFields(issuesRequest.getCustomFields());
JiraClient.setConfig(config);
// List<JiraField> jiraFields = JiraClient.getFields();
jiraClientV2.setConfig(config);
// List<JiraField> jiraFields = JiraClientV2.getFields();
// Map<String, Boolean> 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<String> requestEntity = new HttpEntity<>(json, requestHeaders);
RestTemplate restTemplate = new RestTemplate();
//post
ResponseEntity<String> 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<MultiValueMap> 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<MultiValueMap> requestEntity = new HttpEntity<>(headers);
RestTemplate restTemplate = new RestTemplate();
//post
ResponseEntity<String> 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;
}
}

View File

@ -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<String> response) {
protected String getResult(ResponseEntity<String> 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<String> response) {
protected Object getResultForList(Class clazz, ResponseEntity<String> response) {
return Arrays.asList(JSONArray.parseArray(getResult(response), clazz).toArray());
}
protected static Object getResultForObject(Class clazz,ResponseEntity<String> response) {
protected Object getResultForObject(Class clazz,ResponseEntity<String> response) {
return JSONObject.parseObject(getResult(response), clazz);
}
}

View File

@ -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<JiraField> getFields() {
String url = getBaseUrl() + "/field";
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, getAuthHttpEntity(), String.class);
return (List<JiraField>) getResultForList(JiraField.class, response);
}
public static JiraIssue getIssues(String issuesId) {
HttpEntity<MultiValueMap> requestEntity = getAuthHttpEntity();
public JiraIssue getIssues(String issuesId) {
LogUtil.debug("getIssues: " + issuesId);
ResponseEntity<String> 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<JiraField> getFields() {
ResponseEntity<String> response = restTemplate.exchange(getBaseUrl() + "/field", HttpMethod.GET, getAuthHttpEntity(), String.class);
return (List<JiraField>) getResultForList(JiraField.class, response);
}
public JiraAddIssueResponse addIssue(String body) {
LogUtil.debug("addIssue: " + body);
HttpHeaders headers = getAuthHeader();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> requestEntity = new HttpEntity<>(body, headers);
ResponseEntity<String> entity = restTemplate.exchange(getBaseUrl() + "/issue", HttpMethod.POST, requestEntity, String.class);
return (JiraAddIssueResponse) getResultForObject(JiraAddIssueResponse.class, entity);
ResponseEntity<String> 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<String> response = restTemplate.exchange(getBaseUrl() + "/createmeta", HttpMethod.GET, getAuthHttpEntity(), String.class);
return (String) getResultForObject(String.class, response);
}
protected HttpEntity<MultiValueMap> 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<MultiValueMap> getAuthHttpEntity() {
return new HttpEntity<>(getAuthHeader());
}
private static HttpHeaders getAuthHeader() {
return getBasicHttpHeaders(USER_NAME, PASSWD);
}
private static String getBaseUrl() {
return ENDPOINT + PREFIX;
}
}

View File

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

View File

@ -0,0 +1,7 @@
package io.metersphere.track.issue.client;
public class JiraClientV3 extends JiraAbstractClient {
{
PREFIX = "/rest/api/3";
}
}

View File

@ -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> content;
public JiraIssueDescription(String text) {
public JiraIssueDescriptionV3(String text) {
List<Content> list = new ArrayList<>();
Content content = new Content(text);
list.add(content);

View File

@ -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);

View File

@ -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 = '--';