|
@@ -0,0 +1,277 @@
|
|
|
|
|
+package cn.seecoder.build.client;
|
|
|
|
|
+
|
|
|
|
|
+import cn.seecoder.build.model.vo.BuildDetailsVO;
|
|
|
|
|
+import cn.seecoder.build.model.vo.PipelineVO;
|
|
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
+import okhttp3.*;
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.io.InputStream;
|
|
|
|
|
+import java.util.Arrays;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
+
|
|
|
|
|
+import static cn.seecoder.build.client.SeecoderBuildException.BIZ_ERROR_CODE;
|
|
|
|
|
+
|
|
|
|
|
+public class SeecoderBuildClient implements SeecoderBuildApi {
|
|
|
|
|
+
|
|
|
|
|
+ private final Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
|
|
+
|
|
|
|
|
+ private OkHttpClient client;
|
|
|
|
|
+
|
|
|
|
|
+ private String token;
|
|
|
|
|
+
|
|
|
|
|
+ private String host;
|
|
|
|
|
+
|
|
|
|
|
+ public SeecoderBuildClient(String host, String token) {
|
|
|
|
|
+ this.token = token;
|
|
|
|
|
+ this.host = host;
|
|
|
|
|
+ client = new OkHttpClient.Builder()
|
|
|
|
|
+ .connectTimeout(60, TimeUnit.SECONDS)
|
|
|
|
|
+ .writeTimeout(60,TimeUnit.SECONDS)
|
|
|
|
|
+ .retryOnConnectionFailure(true)
|
|
|
|
|
+ .build();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private <T> T processResponse(ObjectMapper objectMapper, Response response, Class<T> clazz) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ validateResponseStatus(response);
|
|
|
|
|
+ String body = response.body().string();
|
|
|
|
|
+ JsonNode node = objectMapper.readTree(body);
|
|
|
|
|
+ Integer code = Integer.parseInt(node.get("code").asText());
|
|
|
|
|
+ String msg = node.get("msg").asText();
|
|
|
|
|
+ if (code != 0) {
|
|
|
|
|
+ throw new SeecoderBuildException(BIZ_ERROR_CODE, msg);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (clazz == null) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ return objectMapper.treeToValue(node.get("result"), clazz);
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ logger.error("IOException", e);
|
|
|
|
|
+ throw SeecoderBuildException.IO_ERROR;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void validateResponseStatus(Response response) {
|
|
|
|
|
+ if (response.code() == 403) {
|
|
|
|
|
+ throw SeecoderBuildException.UNAHTORIZATION_ERROR;
|
|
|
|
|
+ } else if (response.code() == 500) {
|
|
|
|
|
+ throw SeecoderBuildException.INTERNAL_SERVER_ERROR;
|
|
|
|
|
+ } else if (response.code() != 200) {
|
|
|
|
|
+ throw SeecoderBuildException.UNKNOWN_ERROR;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public PipelineVO createOrUpdatePipeline(PipelineVO vo, boolean create) throws SeecoderBuildException {
|
|
|
|
|
+ try {
|
|
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
+ Request.Builder builder = new Request.Builder()
|
|
|
|
|
+ .addHeader("Authorization", token);
|
|
|
|
|
+ if (create) {
|
|
|
|
|
+ builder = builder
|
|
|
|
|
+ .url(host + "/api/pipeline")
|
|
|
|
|
+ .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), objectMapper.writeValueAsString(vo)));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ builder = builder
|
|
|
|
|
+ .url(host + "/api/pipeline/" + vo.getId())
|
|
|
|
|
+ .put(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), objectMapper.writeValueAsString(vo)));
|
|
|
|
|
+ }
|
|
|
|
|
+ Request request = builder.build();
|
|
|
|
|
+ final Call call = client.newCall(request);
|
|
|
|
|
+ Response response = call.execute();
|
|
|
|
|
+ PipelineVO pipelineVO = processResponse(objectMapper, response, PipelineVO.class);
|
|
|
|
|
+ return pipelineVO;
|
|
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
|
|
+ logger.error("JSON writeValueAsString error", e);
|
|
|
|
|
+ throw SeecoderBuildException.JSON_ERROR;
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ logger.error("IOException", e);
|
|
|
|
|
+ throw SeecoderBuildException.IO_ERROR;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void delete(Integer id) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
+ Request request = new Request.Builder()
|
|
|
|
|
+ .url(host + "/api/pipeline/" + id)
|
|
|
|
|
+ .delete()
|
|
|
|
|
+ .addHeader("Authorization", token)
|
|
|
|
|
+ .build();
|
|
|
|
|
+ final Call call = client.newCall(request);
|
|
|
|
|
+ Response response = call.execute();
|
|
|
|
|
+ processResponse(objectMapper, response, null);
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ logger.error("IOException", e);
|
|
|
|
|
+ throw SeecoderBuildException.IO_ERROR;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<PipelineVO> getAll() {
|
|
|
|
|
+ try {
|
|
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
+ Request request = new Request.Builder()
|
|
|
|
|
+ .url(host + "/api/pipeline")
|
|
|
|
|
+ .get()
|
|
|
|
|
+ .addHeader("Authorization", token)
|
|
|
|
|
+ .build();
|
|
|
|
|
+ final Call call = client.newCall(request);
|
|
|
|
|
+ Response response = call.execute();
|
|
|
|
|
+ PipelineVO[] pipelines = processResponse(objectMapper, response, PipelineVO[].class);
|
|
|
|
|
+ return Arrays.asList(pipelines);
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ logger.error("IOException", e);
|
|
|
|
|
+ throw SeecoderBuildException.IO_ERROR;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public PipelineVO getDetailById(Integer id) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
+ Request request = new Request.Builder()
|
|
|
|
|
+ .url(host + "/api/pipeline/" + id)
|
|
|
|
|
+ .get()
|
|
|
|
|
+ .addHeader("Authorization", token)
|
|
|
|
|
+ .build();
|
|
|
|
|
+ final Call call = client.newCall(request);
|
|
|
|
|
+ Response response = call.execute();
|
|
|
|
|
+ return processResponse(objectMapper, response, PipelineVO.class);
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ logger.error("IOException", e);
|
|
|
|
|
+ throw SeecoderBuildException.IO_ERROR;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public String createJob(Integer pipelineId, String jobName) throws SeecoderBuildException {
|
|
|
|
|
+ try {
|
|
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
|
|
+ params.put("pipelineId", pipelineId);
|
|
|
|
|
+ if (jobName != null) {
|
|
|
|
|
+ params.put("jobName", jobName);
|
|
|
|
|
+ }
|
|
|
|
|
+ Request request = new Request.Builder()
|
|
|
|
|
+ .url(host + "/api/job")
|
|
|
|
|
+ .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), objectMapper.writeValueAsString(params)))
|
|
|
|
|
+ .addHeader("Authorization", token)
|
|
|
|
|
+ .build();
|
|
|
|
|
+ final Call call = client.newCall(request);
|
|
|
|
|
+ Response response = call.execute();
|
|
|
|
|
+ return processResponse(objectMapper, response, String.class);
|
|
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
|
|
+ logger.error("JSON writeValueAsString error", e);
|
|
|
|
|
+ throw SeecoderBuildException.JSON_ERROR;
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ logger.error("IOException", e);
|
|
|
|
|
+ throw SeecoderBuildException.IO_ERROR;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void deleteJob(String jobName) throws SeecoderBuildException {
|
|
|
|
|
+ try {
|
|
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
+ Request request = new Request.Builder()
|
|
|
|
|
+ .url(host + "/api/job/" + jobName)
|
|
|
|
|
+ .delete()
|
|
|
|
|
+ .addHeader("Authorization", token)
|
|
|
|
|
+ .build();
|
|
|
|
|
+ final Call call = client.newCall(request);
|
|
|
|
|
+ Response response = call.execute();
|
|
|
|
|
+ processResponse(objectMapper, response, null);
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ logger.error("IOException", e);
|
|
|
|
|
+ throw SeecoderBuildException.IO_ERROR;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public BuildDetailsVO getBuildDetails(String jobName, Integer buildId) throws SeecoderBuildException {
|
|
|
|
|
+ try {
|
|
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
+ Request request = new Request.Builder()
|
|
|
|
|
+ .url(host + "/api/job/" + jobName + "/build/" + buildId)
|
|
|
|
|
+ .get()
|
|
|
|
|
+ .addHeader("Authorization", token)
|
|
|
|
|
+ .build();
|
|
|
|
|
+ final Call call = client.newCall(request);
|
|
|
|
|
+ Response response = call.execute();
|
|
|
|
|
+ return processResponse(objectMapper, response, BuildDetailsVO.class);
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ logger.error("IOException", e);
|
|
|
|
|
+ throw SeecoderBuildException.IO_ERROR;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public String getEntity(String jobName, Integer buildId, String path) throws SeecoderBuildException {
|
|
|
|
|
+ try {
|
|
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
+ Request request = new Request.Builder()
|
|
|
|
|
+ .url(host + "/api/job/entity?jobName=" + jobName + "&buildId=" + buildId + "&path=" +path)
|
|
|
|
|
+ .get()
|
|
|
|
|
+ .addHeader("Authorization", token)
|
|
|
|
|
+ .build();
|
|
|
|
|
+ final Call call = client.newCall(request);
|
|
|
|
|
+ Response response = call.execute();
|
|
|
|
|
+ return processResponse(objectMapper, response, String.class);
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ logger.error("IOException", e);
|
|
|
|
|
+ throw SeecoderBuildException.IO_ERROR;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public InputStream getArtifact(String jobName, Integer buildId, String path) throws SeecoderBuildException {
|
|
|
|
|
+ try {
|
|
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
+ Request request = new Request.Builder()
|
|
|
|
|
+ .url(host + "/api/job/artifact?jobName=" + jobName + "&buildId=" + buildId + "&path=" +path)
|
|
|
|
|
+ .get()
|
|
|
|
|
+ .addHeader("Authorization", token)
|
|
|
|
|
+ .build();
|
|
|
|
|
+ final Call call = client.newCall(request);
|
|
|
|
|
+ Response response = call.execute();
|
|
|
|
|
+ validateResponseStatus(response);
|
|
|
|
|
+ return response.body().byteStream();
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ logger.error("IOException", e);
|
|
|
|
|
+ throw SeecoderBuildException.IO_ERROR;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void invokeBuild(Integer pipelineId, String jobName, Map<String, String> params) throws SeecoderBuildException {
|
|
|
|
|
+ try {
|
|
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
+ Map<String, Object> paramsMap = new HashMap<>();
|
|
|
|
|
+ paramsMap.put("pipelineId", pipelineId);
|
|
|
|
|
+ paramsMap.put("jobName", jobName);
|
|
|
|
|
+ paramsMap.put("params", params);
|
|
|
|
|
+ Request request = new Request.Builder()
|
|
|
|
|
+ .url(host + "/api/job/run")
|
|
|
|
|
+ .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), objectMapper.writeValueAsString(paramsMap)))
|
|
|
|
|
+ .addHeader("Authorization", token)
|
|
|
|
|
+ .build();
|
|
|
|
|
+ final Call call = client.newCall(request);
|
|
|
|
|
+ Response response = call.execute();
|
|
|
|
|
+ processResponse(objectMapper, response, null);
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ logger.error("IOException", e);
|
|
|
|
|
+ throw SeecoderBuildException.IO_ERROR;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|