|
|
@@ -9,9 +9,9 @@ import cn.seecoder.build.server.service.model.converter.TestResultConvertor;
|
|
|
import cn.seecoder.build.server.util.ApplicationProperties;
|
|
|
import cn.seecoder.build.server.util.LoggerUtil;
|
|
|
import cn.seecoder.build.server.util.ServiceException;
|
|
|
+
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
import com.offbytwo.jenkins.JenkinsServer;
|
|
|
-import com.offbytwo.jenkins.client.JenkinsHttpClient;
|
|
|
import com.offbytwo.jenkins.model.*;
|
|
|
import org.apache.commons.io.output.ByteArrayOutputStream;
|
|
|
import org.dom4j.*;
|
|
|
@@ -19,10 +19,8 @@ import org.slf4j.Logger;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import javax.annotation.PostConstruct;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
-import java.net.URI;
|
|
|
import java.net.URISyntaxException;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
@@ -32,26 +30,29 @@ public class JenkinsApiImpl implements JenkinsApi {
|
|
|
|
|
|
private static final Logger logger = LoggerUtil.getLogger(JenkinsApiImpl.class);
|
|
|
|
|
|
- private final ApplicationProperties properties;
|
|
|
+ //private final ApplicationProperties properties;
|
|
|
|
|
|
private JenkinsServer server;
|
|
|
private JenkinsInternalApi jenkinsInternalApi;
|
|
|
|
|
|
@Autowired
|
|
|
- public JenkinsApiImpl(ApplicationProperties properties, JenkinsInternalApi jenkinsInternalApi) {
|
|
|
- this.properties = properties;
|
|
|
+ //public JenkinsApiImpl(ApplicationProperties properties, JenkinsInternalApi jenkinsInternalApi) {
|
|
|
+
|
|
|
+ public JenkinsApiImpl(JenkinsInternalApi jenkinsInternalApi) {
|
|
|
+ //this.properties = properties;
|
|
|
this.jenkinsInternalApi = jenkinsInternalApi;
|
|
|
+ server = new JenkinsServer(jenkinsInternalApi.getClient());
|
|
|
}
|
|
|
|
|
|
- @PostConstruct
|
|
|
- private void initialize() throws URISyntaxException {
|
|
|
- ApplicationProperties.Jenkins jenkins = properties.getJenkins();
|
|
|
- URI internalURI = new URI(jenkins.getInternalHost());
|
|
|
- String username = jenkins.getUsername();
|
|
|
- String token = jenkins.getToken();
|
|
|
- JenkinsHttpClient client = new PoolingJenkinsHttpClient(internalURI, username, token);
|
|
|
- server = new JenkinsServer(client);
|
|
|
- }
|
|
|
+// @PostConstruct
|
|
|
+// private void initialize() throws URISyntaxException {
|
|
|
+// ApplicationProperties.Jenkins jenkins = properties.getJenkins();
|
|
|
+// URI internalURI = new URI(jenkins.getInternalHost());
|
|
|
+// String username = jenkins.getUsername();
|
|
|
+// String token = jenkins.getToken();
|
|
|
+// JenkinsHttpClient client = new PoolingJenkinsHttpClient(internalURI, username, token);
|
|
|
+// server = new JenkinsServer(client);
|
|
|
+// }
|
|
|
|
|
|
@Override
|
|
|
public void createJob(String jobName, String configTemplate) throws ServiceException {
|
|
|
@@ -76,8 +77,11 @@ public class JenkinsApiImpl implements JenkinsApi {
|
|
|
// }
|
|
|
|
|
|
JobWithDetails jobWithDetails = server.getJob(jobName);
|
|
|
- reference = jenkinsInternalApi.invokeBuild(jobWithDetails,params);
|
|
|
+ logger.info("getJobWithDetails成功");
|
|
|
+ reference = jenkinsInternalApi.invokeBuild(jobWithDetails, params);
|
|
|
+ logger.info("invokeBuild成功");
|
|
|
QueueItem item = server.getQueueItem(reference);
|
|
|
+ logger.info("第一次getQueueItem成功");
|
|
|
while (item.getExecutable() == null) {
|
|
|
try {
|
|
|
Thread.sleep(500);
|
|
|
@@ -114,6 +118,7 @@ public class JenkinsApiImpl implements JenkinsApi {
|
|
|
throw new ServiceException("101", e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
public String get(BuildWithDetails build, String path) throws ServiceException {
|
|
|
try {
|
|
|
//return build.getClient().get(build.getUrl() + path);
|
|
|
@@ -126,22 +131,19 @@ public class JenkinsApiImpl implements JenkinsApi {
|
|
|
|
|
|
|
|
|
/**
|
|
|
- *
|
|
|
* @param jobName job名
|
|
|
* @param buildId 构建id
|
|
|
* @return BuildDetailsVO
|
|
|
- * @throws ServiceException
|
|
|
- * 通过jobName和buildID获取BuildDetailsVO
|
|
|
- *
|
|
|
+ * @throws ServiceException 通过jobName和buildID获取BuildDetailsVO
|
|
|
*/
|
|
|
@Override
|
|
|
- public BuildDetailsVO getBuildDetailsVO(String jobName, Integer buildId) throws ServiceException{
|
|
|
+ public BuildDetailsVO getBuildDetailsVO(String jobName, Integer buildId) throws ServiceException {
|
|
|
|
|
|
- BuildWithDetails buildWithDetails=getBuildDetails(jobName, buildId, true);
|
|
|
+ BuildWithDetails buildWithDetails = getBuildDetails(jobName, buildId, true);
|
|
|
|
|
|
- if(buildWithDetails==null){
|
|
|
+ if (buildWithDetails == null) {
|
|
|
logger.error("buildWithDetails为空, jobName={}, buildId={}", jobName, buildId);
|
|
|
- throw new ServiceException("101","buildWithDetails为空");
|
|
|
+ throw new ServiceException("101", "buildWithDetails为空");
|
|
|
}
|
|
|
|
|
|
BuildDetailsVO buildDetailsVO = new BuildDetailsVO();
|
|
|
@@ -156,11 +158,11 @@ public class JenkinsApiImpl implements JenkinsApi {
|
|
|
//buildDetailsVO.setConsoleOutput(buildWithDetails.getConsoleOutputText());
|
|
|
buildDetailsVO.setConsoleOutput(jenkinsInternalApi.getBuildConsoleOutputText(buildWithDetails));
|
|
|
|
|
|
- setBuildResult(buildWithDetails,buildDetailsVO);
|
|
|
+ setBuildResult(buildWithDetails, buildDetailsVO);
|
|
|
|
|
|
logger.info(JSON.toJSONString(buildDetailsVO));
|
|
|
return buildDetailsVO;
|
|
|
- }catch (IOException e){
|
|
|
+ } catch (IOException e) {
|
|
|
logger.error("getBuildDetailsVO exception={}, jobName={}, buildId={}", e, jobName, buildId);
|
|
|
throw new ServiceException("101", e.getMessage());
|
|
|
}
|
|
|
@@ -189,7 +191,7 @@ public class JenkinsApiImpl implements JenkinsApi {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- void setBuildResult(BuildWithDetails buildWithDetails,BuildDetailsVO buildDetailsVO) throws ServiceException {
|
|
|
+ void setBuildResult(BuildWithDetails buildWithDetails, BuildDetailsVO buildDetailsVO) throws ServiceException {
|
|
|
|
|
|
TestResult testResult = getTestResult(buildWithDetails);
|
|
|
|
|
|
@@ -228,10 +230,10 @@ public class JenkinsApiImpl implements JenkinsApi {
|
|
|
buildDetailsVO.setMutationResult(mutationResult);
|
|
|
}
|
|
|
} catch (DocumentException e) {
|
|
|
- logger.error("documentParseText exception="+e);
|
|
|
+ logger.error("documentParseText exception=" + e);
|
|
|
throw new ServiceException("101", e.getMessage());
|
|
|
} catch (IOException e) {
|
|
|
- logger.error("writeArtifact exception="+e);
|
|
|
+ logger.error("writeArtifact exception=" + e);
|
|
|
throw new ServiceException("101", e.getMessage());
|
|
|
|
|
|
}
|
|
|
@@ -244,7 +246,7 @@ public class JenkinsApiImpl implements JenkinsApi {
|
|
|
//Artifact artifact = new Artifact();
|
|
|
//artifact.setRelativePath(relativePath);
|
|
|
//return build.downloadArtifact(artifact);
|
|
|
- return jenkinsInternalApi.downloadArtifact(build,relativePath);
|
|
|
+ return jenkinsInternalApi.downloadArtifact(build, relativePath);
|
|
|
} catch (IOException | URISyntaxException e) {
|
|
|
logger.error("downloadArtifact exception={}, relativePath={}", e, relativePath);
|
|
|
throw new ServiceException("101", e.getMessage());
|
|
|
@@ -255,8 +257,8 @@ public class JenkinsApiImpl implements JenkinsApi {
|
|
|
try {
|
|
|
//return buildWithDetails.getTestResult();
|
|
|
return jenkinsInternalApi.getTestResult(buildWithDetails);
|
|
|
- }catch (IOException e){
|
|
|
- logger.error("getTestResult exception="+e);
|
|
|
+ } catch (IOException e) {
|
|
|
+ logger.error("getTestResult exception=" + e);
|
|
|
throw new ServiceException("101", e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
@@ -265,14 +267,13 @@ public class JenkinsApiImpl implements JenkinsApi {
|
|
|
try {
|
|
|
//return buildWithDetails.getClient().get(buildWithDetails.getUrl() + "/cobertura/?depth=2", CoverageResult.class);
|
|
|
return jenkinsInternalApi.getCoverageResult(buildWithDetails);
|
|
|
- }catch (IOException e){
|
|
|
- logger.error("getCoverageResult exception="+e);
|
|
|
+ } catch (IOException e) {
|
|
|
+ logger.error("getCoverageResult exception=" + e);
|
|
|
throw new ServiceException("101", e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
@Override
|
|
|
public String getEntity(String jobName, Integer buildId, String path) throws ServiceException {
|
|
|
BuildWithDetails buildWithDetails = getBuildDetails(jobName, buildId, true);
|