refactor(测试跟踪): 禅道创建缺陷失败给出提示

--bug=1014318 --user=陈建星 【测试跟踪】缺陷管理新添加的自定义字段,输入为空保存会报错 https://www.tapd.cn/55049933/s/1189354
This commit is contained in:
AnAngle 2022-06-23 16:33:15 +08:00 committed by jianxing
parent dff8015412
commit d795144d61
2 changed files with 80 additions and 2 deletions

View File

@ -0,0 +1,74 @@
package io.metersphere.commons.utils;
import java.util.regex.Pattern;
public class UnicodeConvertUtils {
// 单个字符的正则表达式
private static final String singlePattern = "[0-9|a-f|A-F]";
// 4个字符的正则表达式
private static final String pattern = singlePattern + singlePattern +
singlePattern + singlePattern;
/**
* \\u 开头的单字转成汉字 \\u6B65 -> 
* @param str
* @return
*/
private static String ustartToCn(final String str) {
StringBuilder sb = new StringBuilder().append("0x")
.append(str.substring(2, 6));
Integer codeInteger = Integer.decode(sb.toString());
int code = codeInteger.intValue();
char c = (char)code;
return String.valueOf(c);
}
/**
* 字符串是否以Unicode字符开头约定Unicode字符以 \\u开头
* @param str 字符串
* @return true表示以Unicode字符开头.
*/
private static boolean isStartWithUnicode(final String str) {
if (null == str || str.length() == 0) {
return false;
}
if (!str.startsWith("\\u")) {
return false;
}
// \u6B65
if (str.length() < 6) {
return false;
}
String content = str.substring(2, 6);
boolean isMatch = Pattern.matches(pattern, content);
return isMatch;
}
/**
* 字符串中所有以 \\u 开头的UNICODE字符串全部替换成汉字
* @return
*/
public static String unicodeToCn(final String str) {
// 用于构建新的字符串
StringBuilder sb = new StringBuilder();
// 从左向右扫描字符串tmpStr是还没有被扫描的剩余字符串
// 下面有两个判断分支
// 1. 如果剩余字符串是Unicode字符开头就把Unicode转换成汉字加到StringBuilder中然后跳过这个Unicode字符
// 2.反之 如果剩余字符串不是Unicode字符开头把普通字符加入StringBuilder向右跳过1.
int length = str.length();
for (int i = 0; i < length;) {
String tmpStr = str.substring(i);
if (isStartWithUnicode(tmpStr)) { // 分支1
sb.append(ustartToCn(tmpStr));
i += 6;
} else { // 分支2
sb.append(str.substring(i, i + 1));
i++;
}
}
return sb.toString();
}
}

View File

@ -1,11 +1,11 @@
package io.metersphere.track.issue.client;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.UnicodeConvertUtils;
import io.metersphere.i18n.Translator;
import io.metersphere.track.issue.domain.zentao.*;
import org.apache.commons.lang3.StringUtils;
@ -85,7 +85,11 @@ public abstract class ZentaoClient extends BaseClient {
MSException.throwException(e.getMessage());
}
AddIssueResponse addIssueResponse = (AddIssueResponse) getResultForObject(AddIssueResponse.class, response);
return JSONObject.parseObject(addIssueResponse.getData(), AddIssueResponse.Issue.class);
AddIssueResponse.Issue issue = JSONObject.parseObject(addIssueResponse.getData(), AddIssueResponse.Issue.class);
if (issue == null) {
MSException.throwException(UnicodeConvertUtils.unicodeToCn(response.getBody()));
}
return issue;
}
public void updateIssue(String id, MultiValueMap<String, Object> paramMap) {