refactor: 缺陷同步自定义字段

This commit is contained in:
chenjianxing 2021-11-01 14:47:27 +08:00 committed by jianxing
parent 13fb6979de
commit baa6a831a2
7 changed files with 74 additions and 67 deletions

View File

@ -1,5 +1,7 @@
package io.metersphere.track.issue;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.IssuesMapper;
import io.metersphere.base.mapper.ProjectMapper;
@ -344,4 +346,40 @@ public abstract class AbstractIssuePlatform implements IssuesPlatform {
}
});
}
protected String syncIssueCustomField(String customFieldsStr, JSONObject issue) {
List<CustomFieldItemDTO> customFields = CustomFieldService.getCustomFields(customFieldsStr);
customFields.forEach(item -> {
String fieldName = item.getCustomData();
Object value = issue.get(fieldName);
if (value != null) {
if (value instanceof JSONObject) {
if (!fieldName.equals("assignee") && !fieldName.equals("reporter")) { // 获取不到账号名
item.setValue(((JSONObject)value).getString("id"));
}
} else {
if (StringUtils.isNotBlank(item.getType()) &&
StringUtils.equalsAny(item.getType(), "multipleSelect", "checkbox", "multipleMember")) {
List<String> values = new ArrayList<>();
if (item.getValue() != null) {
JSONArray attrs = (JSONArray) item.getValue();
attrs.forEach(attr -> {
if (attr instanceof JSONObject) {
values.add(((JSONObject)attr).getString("id"));
} else {
values.add((String) attr);
}
});
}
item.setValue(values);
} else {
item.setValue(value);
}
}
}
});
return JSONObject.toJSONString(customFields);
}
}

View File

@ -106,43 +106,7 @@ public class JiraPlatform extends AbstractIssuePlatform {
item.setDescription(description);
item.setPlatformStatus(status);
item.setPlatform(IssuesManagePlatform.Jira.toString());
item.setCustomFields(parseIssueCustomField(customFieldsStr, jiraIssue));
}
public String parseIssueCustomField(String customFieldsStr, JiraIssue jiraIssue) {
List<CustomFieldItemDTO> customFields = CustomFieldService.getCustomFields(customFieldsStr);
JSONObject fields = jiraIssue.getFields();
customFields.forEach(item -> {
String fieldName = item.getCustomData();
Object value = fields.get(fieldName);
if (value != null) {
if (value instanceof JSONObject) {
if (!fieldName.equals("assignee") && !fieldName.equals("reporter")) { // 获取不到账号名
item.setValue(((JSONObject)value).getString("id"));
}
} else {
if (StringUtils.isNotBlank(item.getType()) &&
StringUtils.equalsAny(item.getType(), "multipleSelect", "checkbox", "multipleMember")) {
List<String> values = new ArrayList<>();
if (item.getValue() != null) {
JSONArray attrs = (JSONArray) item.getValue();
attrs.forEach(attr -> {
if (attr instanceof JSONObject) {
values.add(((JSONObject)attr).getString("id"));
} else {
values.add((String) attr);
}
});
}
item.setValue(values);
} else {
item.setValue(value);
}
}
}
});
return JSONObject.toJSONString(customFields);
item.setCustomFields(syncIssueCustomField(customFieldsStr, jiraIssue.getFields()));
}
private String getStatus(JSONObject fields) {

View File

@ -3,7 +3,10 @@ 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.base.domain.IssuesDao;
import io.metersphere.base.domain.IssuesWithBLOBs;
import io.metersphere.base.domain.Project;
import io.metersphere.base.domain.TestCaseWithBLOBs;
import io.metersphere.commons.constants.IssuesManagePlatform;
import io.metersphere.commons.constants.IssuesStatus;
import io.metersphere.commons.exception.MSException;
@ -206,8 +209,6 @@ public class TapdPlatform extends AbstractIssuePlatform {
.map(IssuesDao::getPlatformId)
.collect(Collectors.toList());
LogUtil.info("ids: " + ids);
if (CollectionUtils.isEmpty(ids)) {
return;
}
@ -218,21 +219,24 @@ public class TapdPlatform extends AbstractIssuePlatform {
while (count == limit) {
TapdGetIssueResponse result = tapdClient.getIssueForPageByIds(project.getTapdId(), pageNum, limit, ids);
List<TapdGetIssueResponse.Data> data = result.getData();
count = data.size();
List<JSONObject> datas = result.getData();
count = datas.size();
pageNum++;
data.forEach(issue -> {
TapdBug bug = issue.getBug();
datas.forEach(issue -> {
JSONObject bug = issue.getJSONObject("Bug");
String id = idMap.get(bug.getString("id"));
IssuesDao issuesDao = new IssuesDao();
BeanUtils.copyBean(issuesDao, bug);
issuesDao.setId(idMap.get(issuesDao.getId()));
issuesDao.setPlatformStatus(statusMap.get(bug.getStatus()));
issuesDao.setId(id);
issuesDao.setPlatformStatus(statusMap.get(bug.getString("status")));
issuesDao.setDescription(htmlDesc2MsDesc(issuesDao.getDescription()));
IssuesWithBLOBs issuesWithBLOBs = issuesMapper.selectByPrimaryKey(id);
issuesDao.setCustomFields(syncIssueCustomField(issuesWithBLOBs.getCustomFields(), bug));
issuesMapper.updateByPrimaryKeySelective(issuesDao);
ids.remove(issue.getBug().getId());
ids.remove(bug.getString("id"));
});
}
// 查不到的置为删除
// 查不到的置为删除
ids.forEach((id) -> {
if (StringUtils.isNotBlank(idMap.get(id))) {
IssuesDao issuesDao = new IssuesDao();

View File

@ -9,13 +9,11 @@ import io.metersphere.commons.constants.IssuesManagePlatform;
import io.metersphere.commons.constants.IssuesStatus;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.dto.CustomFieldItemDTO;
import io.metersphere.dto.UserDTO;
import io.metersphere.track.dto.DemandDTO;
import io.metersphere.track.issue.client.ZentaoClient;
import io.metersphere.track.issue.domain.PlatformUser;
import io.metersphere.track.issue.domain.zentao.AddIssueResponse;
import io.metersphere.track.issue.domain.zentao.GetIssueResponse;
import io.metersphere.track.issue.domain.zentao.ZentaoBuild;
import io.metersphere.track.issue.domain.zentao.ZentaoConfig;
import io.metersphere.track.request.testcase.IssuesRequest;
@ -148,9 +146,9 @@ public class ZentaoPlatform extends AbstractIssuePlatform {
return list;
}
public IssuesDao getZentaoIssues(String bugId) {
GetIssueResponse.Issue bug = zentaoClient.getBugById(bugId);
String description = bug.getSteps();
public IssuesDao getZentaoIssues(IssuesDao issue) {
JSONObject bug = zentaoClient.getBugById(issue.getPlatformId());
String description = bug.getString("steps");
String steps = description;
try {
steps = zentao2MsDescription(description);
@ -158,14 +156,16 @@ public class ZentaoPlatform extends AbstractIssuePlatform {
LogUtil.error(e.getMessage(), e);
}
IssuesDao issues = new IssuesDao();
issues.setPlatformStatus(bug.getStatus());
if (StringUtils.equals(bug.getDeleted(),"1")) {
issues.setPlatformStatus(bug.getString("status"));
if (StringUtils.equals(bug.getString("deleted"),"1")) {
issues.setPlatformStatus(IssuesStatus.DELETE.toString());
issuesMapper.updateByPrimaryKeySelective(issues);
}
issues.setTitle(bug.getTitle());
issues.setTitle(bug.getString("title"));
issues.setDescription(steps);
issues.setReporter(bug.getOpenedBy());
issues.setReporter(bug.getString("openedBy"));
IssuesWithBLOBs issuesWithBLOBs = issuesMapper.selectByPrimaryKey(issue.getId());
issuesWithBLOBs.setCustomFields(syncIssueCustomField(issuesWithBLOBs.getCustomFields(), bug));
return issues;
}
@ -329,7 +329,7 @@ public class ZentaoPlatform extends AbstractIssuePlatform {
public void syncIssues(Project project, List<IssuesDao> issues) {
issues.forEach(item -> {
setConfig();
IssuesDao issuesDao = getZentaoIssues(item.getPlatformId());
IssuesDao issuesDao = getZentaoIssues(item);
issuesDao.setId(item.getId());
issuesMapper.updateByPrimaryKeySelective(issuesDao);
});

View File

@ -106,13 +106,13 @@ public abstract class ZentaoClient extends BaseClient {
}
}
public GetIssueResponse.Issue getBugById(String id) {
public JSONObject getBugById(String id) {
String sessionId = login();
String bugGet = requestUrl.getBugGet();
ResponseEntity<String> response = restTemplate.exchange(bugGet,
HttpMethod.GET, null, String.class, id, sessionId);
GetIssueResponse getIssueResponse = (GetIssueResponse) getResultForObject(GetIssueResponse.class, response);
return JSONObject.parseObject(getIssueResponse.getData(), GetIssueResponse.Issue.class);
return JSONObject.parseObject(getIssueResponse.getData());
}
protected String getBaseUrl() {

View File

@ -1,5 +1,6 @@
package io.metersphere.track.issue.domain.tapd;
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.Setter;
@ -10,12 +11,12 @@ import java.util.List;
public class TapdGetIssueResponse {
private int status;
private List<Data> data;
private List<JSONObject> data;
private String info;
@Getter
@Setter
public static class Data {
private TapdBug bug;
}
// @Getter
// @Setter
// public static class Data {
// private TapdBug bug;
// }
}

@ -1 +1 @@
Subproject commit 62c43d871336e35eb9faad05a1c81150d9d329af
Subproject commit f0f7c9894e04b050a2edd24f1b71bb4ddc2c1f59