feat: 服务集成

This commit is contained in:
shiziyuan9527 2020-08-12 17:37:14 +08:00
parent 6847ff0530
commit 47fad33be1
9 changed files with 801 additions and 1 deletions

View File

@ -0,0 +1,15 @@
package io.metersphere.base.domain;
import java.io.Serializable;
import lombok.Data;
@Data
public class TestCaseIssues implements Serializable {
private String id;
private String testCaseId;
private String issuesId;
private static final long serialVersionUID = 1L;
}

View File

@ -0,0 +1,410 @@
package io.metersphere.base.domain;
import java.util.ArrayList;
import java.util.List;
public class TestCaseIssuesExample {
protected String orderByClause;
protected boolean distinct;
protected List<Criteria> oredCriteria;
public TestCaseIssuesExample() {
oredCriteria = new ArrayList<Criteria>();
}
public void setOrderByClause(String orderByClause) {
this.orderByClause = orderByClause;
}
public String getOrderByClause() {
return orderByClause;
}
public void setDistinct(boolean distinct) {
this.distinct = distinct;
}
public boolean isDistinct() {
return distinct;
}
public List<Criteria> getOredCriteria() {
return oredCriteria;
}
public void or(Criteria criteria) {
oredCriteria.add(criteria);
}
public Criteria or() {
Criteria criteria = createCriteriaInternal();
oredCriteria.add(criteria);
return criteria;
}
public Criteria createCriteria() {
Criteria criteria = createCriteriaInternal();
if (oredCriteria.size() == 0) {
oredCriteria.add(criteria);
}
return criteria;
}
protected Criteria createCriteriaInternal() {
Criteria criteria = new Criteria();
return criteria;
}
public void clear() {
oredCriteria.clear();
orderByClause = null;
distinct = false;
}
protected abstract static class GeneratedCriteria {
protected List<Criterion> criteria;
protected GeneratedCriteria() {
super();
criteria = new ArrayList<Criterion>();
}
public boolean isValid() {
return criteria.size() > 0;
}
public List<Criterion> getAllCriteria() {
return criteria;
}
public List<Criterion> getCriteria() {
return criteria;
}
protected void addCriterion(String condition) {
if (condition == null) {
throw new RuntimeException("Value for condition cannot be null");
}
criteria.add(new Criterion(condition));
}
protected void addCriterion(String condition, Object value, String property) {
if (value == null) {
throw new RuntimeException("Value for " + property + " cannot be null");
}
criteria.add(new Criterion(condition, value));
}
protected void addCriterion(String condition, Object value1, Object value2, String property) {
if (value1 == null || value2 == null) {
throw new RuntimeException("Between values for " + property + " cannot be null");
}
criteria.add(new Criterion(condition, value1, value2));
}
public Criteria andIdIsNull() {
addCriterion("id is null");
return (Criteria) this;
}
public Criteria andIdIsNotNull() {
addCriterion("id is not null");
return (Criteria) this;
}
public Criteria andIdEqualTo(String value) {
addCriterion("id =", value, "id");
return (Criteria) this;
}
public Criteria andIdNotEqualTo(String value) {
addCriterion("id <>", value, "id");
return (Criteria) this;
}
public Criteria andIdGreaterThan(String value) {
addCriterion("id >", value, "id");
return (Criteria) this;
}
public Criteria andIdGreaterThanOrEqualTo(String value) {
addCriterion("id >=", value, "id");
return (Criteria) this;
}
public Criteria andIdLessThan(String value) {
addCriterion("id <", value, "id");
return (Criteria) this;
}
public Criteria andIdLessThanOrEqualTo(String value) {
addCriterion("id <=", value, "id");
return (Criteria) this;
}
public Criteria andIdLike(String value) {
addCriterion("id like", value, "id");
return (Criteria) this;
}
public Criteria andIdNotLike(String value) {
addCriterion("id not like", value, "id");
return (Criteria) this;
}
public Criteria andIdIn(List<String> values) {
addCriterion("id in", values, "id");
return (Criteria) this;
}
public Criteria andIdNotIn(List<String> values) {
addCriterion("id not in", values, "id");
return (Criteria) this;
}
public Criteria andIdBetween(String value1, String value2) {
addCriterion("id between", value1, value2, "id");
return (Criteria) this;
}
public Criteria andIdNotBetween(String value1, String value2) {
addCriterion("id not between", value1, value2, "id");
return (Criteria) this;
}
public Criteria andTestCaseIdIsNull() {
addCriterion("test_case_id is null");
return (Criteria) this;
}
public Criteria andTestCaseIdIsNotNull() {
addCriterion("test_case_id is not null");
return (Criteria) this;
}
public Criteria andTestCaseIdEqualTo(String value) {
addCriterion("test_case_id =", value, "testCaseId");
return (Criteria) this;
}
public Criteria andTestCaseIdNotEqualTo(String value) {
addCriterion("test_case_id <>", value, "testCaseId");
return (Criteria) this;
}
public Criteria andTestCaseIdGreaterThan(String value) {
addCriterion("test_case_id >", value, "testCaseId");
return (Criteria) this;
}
public Criteria andTestCaseIdGreaterThanOrEqualTo(String value) {
addCriterion("test_case_id >=", value, "testCaseId");
return (Criteria) this;
}
public Criteria andTestCaseIdLessThan(String value) {
addCriterion("test_case_id <", value, "testCaseId");
return (Criteria) this;
}
public Criteria andTestCaseIdLessThanOrEqualTo(String value) {
addCriterion("test_case_id <=", value, "testCaseId");
return (Criteria) this;
}
public Criteria andTestCaseIdLike(String value) {
addCriterion("test_case_id like", value, "testCaseId");
return (Criteria) this;
}
public Criteria andTestCaseIdNotLike(String value) {
addCriterion("test_case_id not like", value, "testCaseId");
return (Criteria) this;
}
public Criteria andTestCaseIdIn(List<String> values) {
addCriterion("test_case_id in", values, "testCaseId");
return (Criteria) this;
}
public Criteria andTestCaseIdNotIn(List<String> values) {
addCriterion("test_case_id not in", values, "testCaseId");
return (Criteria) this;
}
public Criteria andTestCaseIdBetween(String value1, String value2) {
addCriterion("test_case_id between", value1, value2, "testCaseId");
return (Criteria) this;
}
public Criteria andTestCaseIdNotBetween(String value1, String value2) {
addCriterion("test_case_id not between", value1, value2, "testCaseId");
return (Criteria) this;
}
public Criteria andIssuesIdIsNull() {
addCriterion("issues_id is null");
return (Criteria) this;
}
public Criteria andIssuesIdIsNotNull() {
addCriterion("issues_id is not null");
return (Criteria) this;
}
public Criteria andIssuesIdEqualTo(String value) {
addCriterion("issues_id =", value, "issuesId");
return (Criteria) this;
}
public Criteria andIssuesIdNotEqualTo(String value) {
addCriterion("issues_id <>", value, "issuesId");
return (Criteria) this;
}
public Criteria andIssuesIdGreaterThan(String value) {
addCriterion("issues_id >", value, "issuesId");
return (Criteria) this;
}
public Criteria andIssuesIdGreaterThanOrEqualTo(String value) {
addCriterion("issues_id >=", value, "issuesId");
return (Criteria) this;
}
public Criteria andIssuesIdLessThan(String value) {
addCriterion("issues_id <", value, "issuesId");
return (Criteria) this;
}
public Criteria andIssuesIdLessThanOrEqualTo(String value) {
addCriterion("issues_id <=", value, "issuesId");
return (Criteria) this;
}
public Criteria andIssuesIdLike(String value) {
addCriterion("issues_id like", value, "issuesId");
return (Criteria) this;
}
public Criteria andIssuesIdNotLike(String value) {
addCriterion("issues_id not like", value, "issuesId");
return (Criteria) this;
}
public Criteria andIssuesIdIn(List<String> values) {
addCriterion("issues_id in", values, "issuesId");
return (Criteria) this;
}
public Criteria andIssuesIdNotIn(List<String> values) {
addCriterion("issues_id not in", values, "issuesId");
return (Criteria) this;
}
public Criteria andIssuesIdBetween(String value1, String value2) {
addCriterion("issues_id between", value1, value2, "issuesId");
return (Criteria) this;
}
public Criteria andIssuesIdNotBetween(String value1, String value2) {
addCriterion("issues_id not between", value1, value2, "issuesId");
return (Criteria) this;
}
}
public static class Criteria extends GeneratedCriteria {
protected Criteria() {
super();
}
}
public static class Criterion {
private String condition;
private Object value;
private Object secondValue;
private boolean noValue;
private boolean singleValue;
private boolean betweenValue;
private boolean listValue;
private String typeHandler;
public String getCondition() {
return condition;
}
public Object getValue() {
return value;
}
public Object getSecondValue() {
return secondValue;
}
public boolean isNoValue() {
return noValue;
}
public boolean isSingleValue() {
return singleValue;
}
public boolean isBetweenValue() {
return betweenValue;
}
public boolean isListValue() {
return listValue;
}
public String getTypeHandler() {
return typeHandler;
}
protected Criterion(String condition) {
super();
this.condition = condition;
this.typeHandler = null;
this.noValue = true;
}
protected Criterion(String condition, Object value, String typeHandler) {
super();
this.condition = condition;
this.value = value;
this.typeHandler = typeHandler;
if (value instanceof List<?>) {
this.listValue = true;
} else {
this.singleValue = true;
}
}
protected Criterion(String condition, Object value) {
this(condition, value, null);
}
protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {
super();
this.condition = condition;
this.value = value;
this.secondValue = secondValue;
this.typeHandler = typeHandler;
this.betweenValue = true;
}
protected Criterion(String condition, Object value, Object secondValue) {
this(condition, value, secondValue, null);
}
}
}

View File

@ -0,0 +1,30 @@
package io.metersphere.base.mapper;
import io.metersphere.base.domain.TestCaseIssues;
import io.metersphere.base.domain.TestCaseIssuesExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface TestCaseIssuesMapper {
long countByExample(TestCaseIssuesExample example);
int deleteByExample(TestCaseIssuesExample example);
int deleteByPrimaryKey(String id);
int insert(TestCaseIssues record);
int insertSelective(TestCaseIssues record);
List<TestCaseIssues> selectByExample(TestCaseIssuesExample example);
TestCaseIssues selectByPrimaryKey(String id);
int updateByExampleSelective(@Param("record") TestCaseIssues record, @Param("example") TestCaseIssuesExample example);
int updateByExample(@Param("record") TestCaseIssues record, @Param("example") TestCaseIssuesExample example);
int updateByPrimaryKeySelective(TestCaseIssues record);
int updateByPrimaryKey(TestCaseIssues record);
}

View File

@ -0,0 +1,181 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="io.metersphere.base.mapper.TestCaseIssuesMapper">
<resultMap id="BaseResultMap" type="io.metersphere.base.domain.TestCaseIssues">
<id column="id" jdbcType="VARCHAR" property="id" />
<result column="test_case_id" jdbcType="VARCHAR" property="testCaseId" />
<result column="issues_id" jdbcType="VARCHAR" property="issuesId" />
</resultMap>
<sql id="Example_Where_Clause">
<where>
<foreach collection="oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Update_By_Example_Where_Clause">
<where>
<foreach collection="example.oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Base_Column_List">
id, test_case_id, issues_id
</sql>
<select id="selectByExample" parameterType="io.metersphere.base.domain.TestCaseIssuesExample" resultMap="BaseResultMap">
select
<if test="distinct">
distinct
</if>
<include refid="Base_Column_List" />
from test_case_issues
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>
<select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from test_case_issues
where id = #{id,jdbcType=VARCHAR}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String">
delete from test_case_issues
where id = #{id,jdbcType=VARCHAR}
</delete>
<delete id="deleteByExample" parameterType="io.metersphere.base.domain.TestCaseIssuesExample">
delete from test_case_issues
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</delete>
<insert id="insert" parameterType="io.metersphere.base.domain.TestCaseIssues">
insert into test_case_issues (id, test_case_id, issues_id
)
values (#{id,jdbcType=VARCHAR}, #{testCaseId,jdbcType=VARCHAR}, #{issuesId,jdbcType=VARCHAR}
)
</insert>
<insert id="insertSelective" parameterType="io.metersphere.base.domain.TestCaseIssues">
insert into test_case_issues
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="testCaseId != null">
test_case_id,
</if>
<if test="issuesId != null">
issues_id,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=VARCHAR},
</if>
<if test="testCaseId != null">
#{testCaseId,jdbcType=VARCHAR},
</if>
<if test="issuesId != null">
#{issuesId,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<select id="countByExample" parameterType="io.metersphere.base.domain.TestCaseIssuesExample" resultType="java.lang.Long">
select count(*) from test_case_issues
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</select>
<update id="updateByExampleSelective" parameterType="map">
update test_case_issues
<set>
<if test="record.id != null">
id = #{record.id,jdbcType=VARCHAR},
</if>
<if test="record.testCaseId != null">
test_case_id = #{record.testCaseId,jdbcType=VARCHAR},
</if>
<if test="record.issuesId != null">
issues_id = #{record.issuesId,jdbcType=VARCHAR},
</if>
</set>
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExample" parameterType="map">
update test_case_issues
set id = #{record.id,jdbcType=VARCHAR},
test_case_id = #{record.testCaseId,jdbcType=VARCHAR},
issues_id = #{record.issuesId,jdbcType=VARCHAR}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByPrimaryKeySelective" parameterType="io.metersphere.base.domain.TestCaseIssues">
update test_case_issues
<set>
<if test="testCaseId != null">
test_case_id = #{testCaseId,jdbcType=VARCHAR},
</if>
<if test="issuesId != null">
issues_id = #{issuesId,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=VARCHAR}
</update>
<update id="updateByPrimaryKey" parameterType="io.metersphere.base.domain.TestCaseIssues">
update test_case_issues
set test_case_id = #{testCaseId,jdbcType=VARCHAR},
issues_id = #{issuesId,jdbcType=VARCHAR}
where id = #{id,jdbcType=VARCHAR}
</update>
</mapper>

View File

@ -1,5 +1,5 @@
package io.metersphere.commons.constants;
public enum DedectManagePlatform {
public enum IssuesManagePlatform {
Tapd, Jira
}

View File

@ -0,0 +1,42 @@
package io.metersphere.commons.utils;
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.RestTemplate;
public class RestTemplateUtils {
private static RestTemplate restTemplate;
private static void getTemplate() {
restTemplate = (RestTemplate) CommonBeanFactory.getBean("restTemplate");
}
public static String get(String url, HttpHeaders httpHeaders) {
getTemplate();
try {
HttpEntity<MultiValueMap> requestEntity = new HttpEntity<>(httpHeaders);
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
return responseEntity.getBody();
} catch (Exception e) {
throw new RuntimeException("调用接口失败", e);
}
}
public static String post(String url, Object paramMap, HttpHeaders httpHeaders) {
getTemplate();
try {
HttpEntity<MultiValueMap> requestEntity = new HttpEntity<>((MultiValueMap) paramMap, httpHeaders);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
return responseEntity.getBody();
} catch (Exception e) {
throw new RuntimeException("调用接口失败", e);
}
}
}

View File

@ -0,0 +1,101 @@
package io.metersphere.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.metersphere.base.domain.ServiceIntegration;
import io.metersphere.commons.constants.IssuesManagePlatform;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.user.SessionUser;
import io.metersphere.commons.utils.EncryptUtils;
import io.metersphere.commons.utils.RestTemplateUtils;
import io.metersphere.commons.utils.SessionUtils;
import io.metersphere.controller.ResultHolder;
import io.metersphere.controller.request.IntegrationRequest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.apache.commons.lang3.StringUtils;
import javax.annotation.Resource;
@Service
public class IssuesService {
@Resource
private IntegrationService integrationService;
public void testAuth() {
String url = "https://api.tapd.cn/quickstart/testauth";
ResultHolder call = call(url);
System.out.println(call.getData());
}
public void addIssues() {
String url = "https://api.tapd.cn/bugs";
MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
paramMap.add("title", "20200812bug2");
paramMap.add("workspace_id", "55049933");
paramMap.add("description", "20200812 api add bug 2");
ResultHolder call = call(url, HttpMethod.POST, paramMap);
System.out.println(call.getData());
}
private ResultHolder call(String url) {
return call(url, HttpMethod.GET, null);
}
private ResultHolder call(String url, HttpMethod httpMethod, Object params) {
String responseJson;
String config = tapdConfig();
JSONObject object = JSON.parseObject(config);
if (object == null) {
MSException.throwException("tapd config is null");
}
String account = object.getString("account");
String password = object.getString("password");
HttpHeaders header = tapdAuth(account, password);
if (httpMethod.equals(HttpMethod.GET)) {
responseJson = RestTemplateUtils.get(url, header);
} else {
responseJson = RestTemplateUtils.post(url, params, header);
}
ResultHolder result = JSON.parseObject(responseJson, ResultHolder.class);
if (!result.isSuccess()) {
MSException.throwException(result.getMessage());
}
return JSON.parseObject(responseJson, ResultHolder.class);
}
private String tapdConfig() {
SessionUser user = SessionUtils.getUser();
String orgId = user.getLastOrganizationId();
IntegrationRequest request = new IntegrationRequest();
if (StringUtils.isBlank(orgId)) {
MSException.throwException("organization id is null");
}
request.setOrgId(orgId);
request.setPlatform(IssuesManagePlatform.Tapd.toString());
ServiceIntegration integration = integrationService.get(request);
return integration.getConfiguration();
}
private HttpHeaders tapdAuth(String apiUser, String password) {
String authKey = EncryptUtils.base64Encoding(apiUser + ":" + password);
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + authKey);
return headers;
}
}

View File

@ -8,4 +8,11 @@ create table service_integration
primary key (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
create table if not exists test_case_issues
(
id varchar(50) not null
primary key,
test_case_id varchar(50) not null,
issues_id varchar(100) not null
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

View File

@ -180,13 +180,27 @@
@change="issuesChange"
:active-text="$t('test_track.plan_view.submit_issues')">
</el-switch>
<el-tooltip class="item" effect="dark"
content="在系统设置-组织-服务集成中集成缺陷管理平台可以自动提交缺陷到指定缺陷管理平台"
placement="right">
<i class="el-icon-info"/>
</el-tooltip>
</el-col>
</el-row>
<el-row v-if="testCase.issues && testCase.issues.hasIssues">
<el-col :span="20" :offset="1" class="issues-edit">
<el-input
type="text"
placeholder="请输入标题"
v-model="testCase.issues.title"
maxlength="100"
show-word-limit
/>
<ckeditor :editor="editor" :disabled="isReadOnly" :config="editorConfig"
v-model="testCase.issues.content"/>
<el-button type="primary" size="small">{{$t('commons.save')}}</el-button>
<el-button size="small" @click="cancel">{{$t('commons.cancel')}}</el-button>
</el-col>
</el-row>