feat: 禅道支持GET方式调用接口

This commit is contained in:
shiziyuan9527 2021-07-09 18:02:08 +08:00 committed by 刘瑞斌
parent 2dc22ba7d7
commit ff0d983c1e
14 changed files with 198 additions and 29 deletions

View File

@ -0,0 +1,18 @@
package io.metersphere.track.issue;
import io.metersphere.track.issue.client.ZentaoClient;
import io.metersphere.track.issue.client.ZentaoGetClient;
import io.metersphere.track.issue.client.ZentaoPathInfoClient;
import org.apache.commons.lang3.StringUtils;
public class ZentaoFactory {
public static ZentaoClient getInstance(String url, String type) {
if (StringUtils.equals(type, "PATH_INFO")) {
return new ZentaoPathInfoClient(url);
} else if (StringUtils.equals(type, "GET")) {
return new ZentaoGetClient(url);
}
return new ZentaoPathInfoClient(url);
}
}

View File

@ -49,9 +49,7 @@ public class ZentaoPlatform extends AbstractIssuePlatform {
*/
private final String url;
private ZentaoClient zentaoClient = new ZentaoClient();
private static final Pattern PATTERN = Pattern.compile("file-read-(.*?)\"/>");
private final ZentaoClient zentaoClient;
protected String key = IssuesManagePlatform.Zentao.toString();
@ -66,7 +64,9 @@ public class ZentaoPlatform extends AbstractIssuePlatform {
this.account = object.getString("account");
this.password = object.getString("password");
this.url = object.getString("url");
String type = object.getString("request");
this.orgId = issuesRequest.getOrganizationId();
this.zentaoClient = ZentaoFactory.getInstance(this.url, type);
}
@Override
@ -101,7 +101,8 @@ public class ZentaoPlatform extends AbstractIssuePlatform {
String key = getProjectId(projectId);
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(new HttpHeaders());
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> responseEntity = restTemplate.exchange(url + "api-getModel-story-getProductStories-productID={key}?zentaosid=" + session,
String storyGet = zentaoClient.requestUrl.getStoryGet();
ResponseEntity<String> responseEntity = restTemplate.exchange(storyGet + session,
HttpMethod.POST, requestEntity, String.class, key);
String body = responseEntity.getBody();
JSONObject obj = JSONObject.parseObject(body);
@ -278,7 +279,8 @@ public class ZentaoPlatform extends AbstractIssuePlatform {
HttpHeaders httpHeaders = new HttpHeaders();
HttpEntity<MultiValueMap<String,String>> requestEntity = new HttpEntity<>(httpHeaders);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> responseEntity = restTemplate.exchange(url + "api-getModel-user-getList?zentaosid=" + session,
String getUser = zentaoClient.requestUrl.getUserGet();
ResponseEntity<String> responseEntity = restTemplate.exchange(getUser + session,
HttpMethod.GET, requestEntity, String.class);
String body = responseEntity.getBody();
JSONObject obj = JSONObject.parseObject(body);
@ -316,7 +318,8 @@ public class ZentaoPlatform extends AbstractIssuePlatform {
HttpHeaders httpHeaders = new HttpHeaders();
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(httpHeaders);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> responseEntity = restTemplate.exchange(url + "api-getModel-build-getProductBuildPairs-productID={projectId}?zentaosid=" + session,
String buildGet = zentaoClient.requestUrl.getBuildsGet();
ResponseEntity<String> responseEntity = restTemplate.exchange(buildGet + session,
HttpMethod.GET, requestEntity, String.class, projectId1);
String body = responseEntity.getBody();
JSONObject obj = JSONObject.parseObject(body);
@ -349,7 +352,8 @@ public class ZentaoPlatform extends AbstractIssuePlatform {
paramMap.add("files", resource);
RestTemplate restTemplate = new RestTemplate();
try {
ResponseEntity<String> responseEntity = restTemplate.exchange(url + "api-getModel-file-saveUpload.json?zentaosid=" + session,
String fileUpload = zentaoClient.requestUrl.getFileUpload();
ResponseEntity<String> responseEntity = restTemplate.exchange(fileUpload + session,
HttpMethod.POST, requestEntity, String.class);
String body = responseEntity.getBody();
JSONObject obj = JSONObject.parseObject(body);
@ -367,8 +371,8 @@ public class ZentaoPlatform extends AbstractIssuePlatform {
private String ms2ZentaoDescription(String msDescription) {
String imgUrlRegex = "!\\[.*?]\\(/resource/md/get/(.*?\\..*?)\\)";
String zentaoSteps = msDescription.replaceAll(imgUrlRegex, "<img src=\"/zentao/file-read-$1\"/>");
Matcher matcher = PATTERN.matcher(zentaoSteps);
String zentaoSteps = msDescription.replaceAll(imgUrlRegex, zentaoClient.requestUrl.getReplaceImgUrl());
Matcher matcher = zentaoClient.requestUrl.getImgPattern().matcher(zentaoSteps);
while (matcher.find()) {
// get file name
String fileName = matcher.group(1);

View File

@ -9,13 +9,11 @@ import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
@Component
public class ZentaoClient extends BaseClient {
public abstract class ZentaoClient extends BaseClient {
protected String ENDPOINT;
@ -23,15 +21,29 @@ public class ZentaoClient extends BaseClient {
protected String PASSWD;
public RequestUrl requestUrl;
protected String url;
public ZentaoClient(String url) {
ENDPOINT = url;
}
public String login() {
String sessionId = getSessionId();
String url = getBaseUrl() + "/user-login.json?zentaosid=" + sessionId;
MultiValueMap<String, String> paramMap = new LinkedMultiValueMap<>();
paramMap.add("account", USER_NAME);
paramMap.add("password", PASSWD);
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(paramMap, new HttpHeaders());
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
GetUserResponse getUserResponse = (GetUserResponse) getResultForObject(GetUserResponse.class, response);
GetUserResponse getUserResponse = new GetUserResponse();
String sessionId = "";
try {
sessionId = getSessionId();
String loginUrl = requestUrl.getLogin();
MultiValueMap<String, String> paramMap = new LinkedMultiValueMap<>();
paramMap.add("account", USER_NAME);
paramMap.add("password", PASSWD);
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(paramMap, new HttpHeaders());
ResponseEntity<String> response = restTemplate.exchange(loginUrl + sessionId, HttpMethod.POST, requestEntity, String.class);
getUserResponse = (GetUserResponse) getResultForObject(GetUserResponse.class, response);
} catch (Exception e) {
LogUtil.error("get result for object error," + e.getMessage());
MSException.throwException("zentao login fail");
}
GetUserResponse.User user = getUserResponse.getUser();
if (user == null) {
LogUtil.error(JSONObject.toJSON(getUserResponse));
@ -46,7 +58,8 @@ public class ZentaoClient extends BaseClient {
}
public String getSessionId() {
ResponseEntity<String> response = restTemplate.exchange(getBaseUrl() + "/api-getsessionid.json",
String getSessionUrl = requestUrl.getSessionGet();
ResponseEntity<String> response = restTemplate.exchange(getSessionUrl,
HttpMethod.GET, null, String.class);
GetSessionResponse getSessionResponse = (GetSessionResponse) getResultForObject(GetSessionResponse.class, response);
return JSONObject.parseObject(getSessionResponse.getData(), GetSessionResponse.Session.class).getSessionID();
@ -58,7 +71,8 @@ public class ZentaoClient extends BaseClient {
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = null;
try {
response = restTemplate.exchange(getBaseUrl() + "/api-getModel-bug-create.json?zentaosid=" + sessionId,
String bugCreate = requestUrl.getBugCreate();
response = restTemplate.exchange(bugCreate + sessionId,
HttpMethod.POST, requestEntity, String.class);
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
@ -70,8 +84,8 @@ public class ZentaoClient extends BaseClient {
public GetIssueResponse.Issue getBugById(String id) {
String sessionId = login();
String url = getBaseUrl() + "/api-getModel-bug-getById-bugID={1}?zentaosid={2}";
ResponseEntity<String> response = restTemplate.exchange(url,
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);

View File

@ -0,0 +1,42 @@
package io.metersphere.track.issue.client;
import io.metersphere.track.issue.domain.zentao.RequestUrl;
import java.util.regex.Pattern;
public class ZentaoGetClient extends ZentaoClient {
private static final String LOGIN = "/?m=user&f=login&t=json&zentaosid=";
private static final String SESSION_GET="/?m=api&f=getSessionID&t=json";
private static final String BUG_CREATE="&module=bug&methodName=create&t=json&zentaosid=";
private static final String BUG_GET="&module=bug&methodName=getById&params=bugID={1}&t=json&zentaosid={2}";
private static final String STORY_GET="&module=story&methodName=getProductStories&params=productID={key}&t=json&zentaosid=";
private static final String USER_GET="&module=user&methodName=getList&t=json&zentaosid=";
private static final String BUILDS_GET="&module=build&methodName=getProductBuildPairs&productID={0}&zentaosid=";
private static final String FILE_UPLOAD="&module=file&methodName=saveUpload&t=json&zentaosid=";
private static final String REPLACE_IMG_URL="<img src=\"/zentao/index.php?m=file&f=read&fileID=$1\"/>";
private static final Pattern IMG_PATTERN = Pattern.compile("m=file&f=read&fileID=(.*?)\"/>");
{
RequestUrl request = new RequestUrl();
request.setLogin(getBaseUrl() + LOGIN);
request.setSessionGet(getBaseUrl() + SESSION_GET);
request.setBugCreate(getUrl(BUG_CREATE));
request.setBugGet(getUrl(BUG_GET));
request.setStoryGet(getUrl(STORY_GET));
request.setUserGet(getUrl(USER_GET));
request.setBuildsGet(getUrl(BUILDS_GET));
request.setFileUpload(getUrl(FILE_UPLOAD));
request.setReplaceImgUrl(REPLACE_IMG_URL);
request.setImgPattern(IMG_PATTERN);
requestUrl = request;
}
public ZentaoGetClient(String url) {
super(url);
}
private String getUrl(String url) {
return getBaseUrl() + "/?m=api&f=getModel" + url;
}
}

View File

@ -0,0 +1,43 @@
package io.metersphere.track.issue.client;
import io.metersphere.track.issue.domain.zentao.RequestUrl;
import java.util.regex.Pattern;
public class ZentaoPathInfoClient extends ZentaoClient {
private static final String LOGIN = "/user-login.json?zentaosid=";
private static final String SESSION_GET = "/api-getsessionid.json";
private static final String BUG_CREATE = "/api-getModel-bug-create.json?zentaosid=";
private static final String BUG_GET = "/api-getModel-bug-getById-bugID={1}?zentaosid={2}";
private static final String STORY_GET = "/api-getModel-story-getProductStories-productID={key}?zentaosid=";
private static final String USER_GET = "/api-getModel-user-getList?zentaosid=";
private static final String BUILDS_GET = "/api-getModel-build-getProductBuildPairs-productID={projectId}?zentaosid=";
private static final String FILE_UPLOAD = "/api-getModel-file-saveUpload.json?zentaosid=";
private static final String REPLACE_IMG_URL = "<img src=\"/zentao/file-read-$1\"/>";
private static final Pattern IMG_PATTERN = Pattern.compile("file-read-(.*?)\"/>");
{
RequestUrl request = new RequestUrl();
request.setLogin(getUrl(LOGIN));
request.setSessionGet(getUrl(SESSION_GET));
request.setBugCreate(getUrl(BUG_CREATE));
request.setBugGet(getUrl(BUG_GET));
request.setStoryGet(getUrl(STORY_GET));
request.setUserGet(getUrl(USER_GET));
request.setBuildsGet(getUrl(BUILDS_GET));
request.setFileUpload(getUrl(FILE_UPLOAD));
request.setReplaceImgUrl(REPLACE_IMG_URL);
request.setImgPattern(IMG_PATTERN);
requestUrl = request;
}
public ZentaoPathInfoClient(String url) {
super(url);
}
private String getUrl(String url) {
return getBaseUrl() + url;
}
}

View File

@ -0,0 +1,21 @@
package io.metersphere.track.issue.domain.zentao;
import lombok.Getter;
import lombok.Setter;
import java.util.regex.Pattern;
@Getter
@Setter
public class RequestUrl {
private String login;
private String sessionGet;
private String bugCreate;
private String bugGet;
private String storyGet;
private String userGet;
private String buildsGet;
private String fileUpload;
private String replaceImgUrl;
private Pattern imgPattern;
}

View File

@ -9,4 +9,5 @@ public class ZentaoConfig {
private String account;
private String password;
private String url;
private String requestType;
}

View File

@ -32,4 +32,6 @@ public class IssuesRequest extends BaseQueryRequest {
private String platform;
private String customFields;
private List<String> testCaseIds;
private String requestType;
}

View File

@ -169,7 +169,9 @@ export default {
this.$set(this.form, 'issuetype', '');
this.$set(this.form, 'storytype', '');
this.$nextTick(() => {
this.$refs.form.clearValidate();
if (this.$refs.form) {
this.$refs.form.clearValidate();
}
});
},
testConnection() {

View File

@ -125,7 +125,9 @@ export default {
this.$set(this.form, 'account', '');
this.$set(this.form, 'password', '');
this.$nextTick(() => {
this.$refs.form.clearValidate();
if (this.$refs.form) {
this.$refs.form.clearValidate();
}
});
},
testConnection() {

View File

@ -13,6 +13,10 @@
<el-form-item :label="$t('organization.integration.zentao_url')" prop="url">
<el-input v-model="form.url" :placeholder="$t('organization.integration.input_zentao_url')"/>
</el-form-item>
<el-form-item :label="$t('organization.integration.zentao_request')" prop="request">
<el-radio v-model="form.request" label="PATH_INFO" size="small" border> PATH_INFO</el-radio>
<el-radio v-model="form.request" label="GET" size="small" border>GET</el-radio>
</el-form-item>
</el-form>
</div>
@ -81,6 +85,11 @@ export default {
message: this.$t('organization.integration.input_zentao_url'),
trigger: ['change', 'blur']
},
request: {
required: true,
message: this.$t('organization.integration.input_zentao_request'),
trigger: ['change', 'blur']
},
},
}
},
@ -98,6 +107,7 @@ export default {
account: this.form.account,
password: this.form.password,
url: formatUrl,
request: this.form.request
};
param.organizationId = lastOrganizationId;
param.platform = ZEN_TAO;
@ -129,6 +139,7 @@ export default {
this.$set(this.form, 'account', config.account);
this.$set(this.form, 'password', config.password);
this.$set(this.form, 'url', config.url);
this.$set(this.form, 'request', config.request ? config.request : 'PATH_INFO');
} else {
this.clear();
}
@ -138,8 +149,11 @@ export default {
this.$set(this.form, 'account', '');
this.$set(this.form, 'password', '');
this.$set(this.form, 'url', '');
this.$set(this.form, 'request', '');
this.$nextTick(() => {
this.$refs.form.clearValidate();
if (this.$refs.form) {
this.$refs.form.clearValidate();
}
});
},
testConnection() {

View File

@ -395,6 +395,8 @@ export default {
input_jira_issuetype: 'Please enter the issue type',
input_jira_storytype: 'Please enter the story type',
zentao_url: 'Zentao url',
zentao_request: 'Zentao request',
input_zentao_request: 'Please enter zentao request type',
input_zentao_url: 'Please enter Zentao address, for example: http://xx.xx.xx.xx/zentao/',
use_tip: 'Usage guidelines:',
use_tip_tapd: 'Basic Auth account information is queried in "Company Management-Security and Integration-Open Platform"',

View File

@ -392,7 +392,9 @@ export default {
input_jira_url: '请输入Jira地址https://metersphere.atlassian.net/',
input_jira_issuetype: '请输入问题类型',
input_jira_storytype: '请输入需求类型',
zentao_url: 'Zentao 地址',
zentao_url: '地址',
zentao_request: '请求方式',
input_zentao_request: '请输入Zentao请求方式',
input_zentao_url: '请输入Zentao地址http://xx.xx.xx.xx/zentao/',
use_tip: '使用指引:',
use_tip_tapd: 'Tapd Basic Auth 账号信息在"公司管理-安全与集成-开放平台"中查询',

View File

@ -392,7 +392,9 @@ export default {
input_jira_url: '請輸入Jira地址https://metersphere.atlassian.net/',
input_jira_issuetype: '請輸入問題類型',
input_jira_storytype: '請輸入需求類型',
zentao_url: 'Zentao 地址',
zentao_url: '地址',
zentao_request: '請求方式',
input_zentao_request: '請輸入Zentao請求方式',
input_zentao_url: '請輸入Zentao地址http://xx.xx.xx.xx/zentao/',
use_tip: '使用指引:',
use_tip_tapd: 'Tapd Basic Auth 賬號信息在"公司管理-安全與集成-開放平臺"中查詢',