From ff0d983c1ef0dc39b6c530196f6d16989b79c44e Mon Sep 17 00:00:00 2001 From: shiziyuan9527 Date: Fri, 9 Jul 2021 18:02:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=A6=85=E9=81=93=E6=94=AF=E6=8C=81GET?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=E8=B0=83=E7=94=A8=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../track/issue/ZentaoFactory.java | 18 ++++++++ .../track/issue/ZentaoPlatform.java | 22 ++++++---- .../track/issue/client/ZentaoClient.java | 44 ++++++++++++------- .../track/issue/client/ZentaoGetClient.java | 42 ++++++++++++++++++ .../issue/client/ZentaoPathInfoClient.java | 43 ++++++++++++++++++ .../track/issue/domain/zentao/RequestUrl.java | 21 +++++++++ .../issue/domain/zentao/ZentaoConfig.java | 1 + .../track/request/testcase/IssuesRequest.java | 2 + .../organization/components/JiraSetting.vue | 4 +- .../organization/components/TapdSetting.vue | 4 +- .../organization/components/ZentaoSetting.vue | 16 ++++++- frontend/src/i18n/en-US.js | 2 + frontend/src/i18n/zh-CN.js | 4 +- frontend/src/i18n/zh-TW.js | 4 +- 14 files changed, 198 insertions(+), 29 deletions(-) create mode 100644 backend/src/main/java/io/metersphere/track/issue/ZentaoFactory.java create mode 100644 backend/src/main/java/io/metersphere/track/issue/client/ZentaoGetClient.java create mode 100644 backend/src/main/java/io/metersphere/track/issue/client/ZentaoPathInfoClient.java create mode 100644 backend/src/main/java/io/metersphere/track/issue/domain/zentao/RequestUrl.java diff --git a/backend/src/main/java/io/metersphere/track/issue/ZentaoFactory.java b/backend/src/main/java/io/metersphere/track/issue/ZentaoFactory.java new file mode 100644 index 0000000000..495763056d --- /dev/null +++ b/backend/src/main/java/io/metersphere/track/issue/ZentaoFactory.java @@ -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); + } +} 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 babdd8d3a2..19e75f2043 100644 --- a/backend/src/main/java/io/metersphere/track/issue/ZentaoPlatform.java +++ b/backend/src/main/java/io/metersphere/track/issue/ZentaoPlatform.java @@ -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> requestEntity = new HttpEntity<>(new HttpHeaders()); RestTemplate restTemplate = new RestTemplate(); - ResponseEntity responseEntity = restTemplate.exchange(url + "api-getModel-story-getProductStories-productID={key}?zentaosid=" + session, + String storyGet = zentaoClient.requestUrl.getStoryGet(); + ResponseEntity 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> requestEntity = new HttpEntity<>(httpHeaders); RestTemplate restTemplate = new RestTemplate(); - ResponseEntity responseEntity = restTemplate.exchange(url + "api-getModel-user-getList?zentaosid=" + session, + String getUser = zentaoClient.requestUrl.getUserGet(); + ResponseEntity 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> requestEntity = new HttpEntity<>(httpHeaders); RestTemplate restTemplate = new RestTemplate(); - ResponseEntity responseEntity = restTemplate.exchange(url + "api-getModel-build-getProductBuildPairs-productID={projectId}?zentaosid=" + session, + String buildGet = zentaoClient.requestUrl.getBuildsGet(); + ResponseEntity 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 responseEntity = restTemplate.exchange(url + "api-getModel-file-saveUpload.json?zentaosid=" + session, + String fileUpload = zentaoClient.requestUrl.getFileUpload(); + ResponseEntity 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, ""); - 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); diff --git a/backend/src/main/java/io/metersphere/track/issue/client/ZentaoClient.java b/backend/src/main/java/io/metersphere/track/issue/client/ZentaoClient.java index b1491772db..8759cf6d85 100644 --- a/backend/src/main/java/io/metersphere/track/issue/client/ZentaoClient.java +++ b/backend/src/main/java/io/metersphere/track/issue/client/ZentaoClient.java @@ -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 paramMap = new LinkedMultiValueMap<>(); - paramMap.add("account", USER_NAME); - paramMap.add("password", PASSWD); - HttpEntity> requestEntity = new HttpEntity<>(paramMap, new HttpHeaders()); - ResponseEntity 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 paramMap = new LinkedMultiValueMap<>(); + paramMap.add("account", USER_NAME); + paramMap.add("password", PASSWD); + HttpEntity> requestEntity = new HttpEntity<>(paramMap, new HttpHeaders()); + ResponseEntity 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 response = restTemplate.exchange(getBaseUrl() + "/api-getsessionid.json", + String getSessionUrl = requestUrl.getSessionGet(); + ResponseEntity 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 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 response = restTemplate.exchange(url, + String bugGet = requestUrl.getBugGet(); + ResponseEntity 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); diff --git a/backend/src/main/java/io/metersphere/track/issue/client/ZentaoGetClient.java b/backend/src/main/java/io/metersphere/track/issue/client/ZentaoGetClient.java new file mode 100644 index 0000000000..ac6ea599c2 --- /dev/null +++ b/backend/src/main/java/io/metersphere/track/issue/client/ZentaoGetClient.java @@ -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¶ms=bugID={1}&t=json&zentaosid={2}"; + private static final String STORY_GET="&module=story&methodName=getProductStories¶ms=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=""; + 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; + } +} diff --git a/backend/src/main/java/io/metersphere/track/issue/client/ZentaoPathInfoClient.java b/backend/src/main/java/io/metersphere/track/issue/client/ZentaoPathInfoClient.java new file mode 100644 index 0000000000..459f91c9f8 --- /dev/null +++ b/backend/src/main/java/io/metersphere/track/issue/client/ZentaoPathInfoClient.java @@ -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 = ""; + 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; + } +} diff --git a/backend/src/main/java/io/metersphere/track/issue/domain/zentao/RequestUrl.java b/backend/src/main/java/io/metersphere/track/issue/domain/zentao/RequestUrl.java new file mode 100644 index 0000000000..f8363bd70a --- /dev/null +++ b/backend/src/main/java/io/metersphere/track/issue/domain/zentao/RequestUrl.java @@ -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; +} diff --git a/backend/src/main/java/io/metersphere/track/issue/domain/zentao/ZentaoConfig.java b/backend/src/main/java/io/metersphere/track/issue/domain/zentao/ZentaoConfig.java index 2ef16f7ea7..31d3ce1cd3 100644 --- a/backend/src/main/java/io/metersphere/track/issue/domain/zentao/ZentaoConfig.java +++ b/backend/src/main/java/io/metersphere/track/issue/domain/zentao/ZentaoConfig.java @@ -9,4 +9,5 @@ public class ZentaoConfig { private String account; private String password; private String url; + private String requestType; } diff --git a/backend/src/main/java/io/metersphere/track/request/testcase/IssuesRequest.java b/backend/src/main/java/io/metersphere/track/request/testcase/IssuesRequest.java index b5aeebe612..c79787fc48 100644 --- a/backend/src/main/java/io/metersphere/track/request/testcase/IssuesRequest.java +++ b/backend/src/main/java/io/metersphere/track/request/testcase/IssuesRequest.java @@ -32,4 +32,6 @@ public class IssuesRequest extends BaseQueryRequest { private String platform; private String customFields; private List testCaseIds; + + private String requestType; } diff --git a/frontend/src/business/components/settings/organization/components/JiraSetting.vue b/frontend/src/business/components/settings/organization/components/JiraSetting.vue index 7ab60a16dc..4286c08d9e 100644 --- a/frontend/src/business/components/settings/organization/components/JiraSetting.vue +++ b/frontend/src/business/components/settings/organization/components/JiraSetting.vue @@ -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() { diff --git a/frontend/src/business/components/settings/organization/components/TapdSetting.vue b/frontend/src/business/components/settings/organization/components/TapdSetting.vue index 6f83415443..6a55443b81 100644 --- a/frontend/src/business/components/settings/organization/components/TapdSetting.vue +++ b/frontend/src/business/components/settings/organization/components/TapdSetting.vue @@ -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() { diff --git a/frontend/src/business/components/settings/organization/components/ZentaoSetting.vue b/frontend/src/business/components/settings/organization/components/ZentaoSetting.vue index 120a4f4448..390e56eb5e 100644 --- a/frontend/src/business/components/settings/organization/components/ZentaoSetting.vue +++ b/frontend/src/business/components/settings/organization/components/ZentaoSetting.vue @@ -13,6 +13,10 @@ + + PATH_INFO + GET + @@ -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() { diff --git a/frontend/src/i18n/en-US.js b/frontend/src/i18n/en-US.js index 37ea86490c..816c68e837 100644 --- a/frontend/src/i18n/en-US.js +++ b/frontend/src/i18n/en-US.js @@ -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"', diff --git a/frontend/src/i18n/zh-CN.js b/frontend/src/i18n/zh-CN.js index 96a5e9df6b..00814c46b8 100644 --- a/frontend/src/i18n/zh-CN.js +++ b/frontend/src/i18n/zh-CN.js @@ -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 账号信息在"公司管理-安全与集成-开放平台"中查询', diff --git a/frontend/src/i18n/zh-TW.js b/frontend/src/i18n/zh-TW.js index 42091ba24d..a175abb7b2 100644 --- a/frontend/src/i18n/zh-TW.js +++ b/frontend/src/i18n/zh-TW.js @@ -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 賬號信息在"公司管理-安全與集成-開放平臺"中查詢',