增加生成后端服务代码。
This commit is contained in:
parent
20da0c0bc1
commit
6078c0228b
@ -1,5 +1,8 @@
|
|||||||
package com.qqchen.deploy.backend.deploy.service;
|
package com.qqchen.deploy.backend.deploy.service;
|
||||||
|
|
||||||
|
import com.qqchen.deploy.backend.deploy.entity.ExternalSystem;
|
||||||
|
import com.qqchen.deploy.backend.deploy.entity.JenkinsJob;
|
||||||
|
import com.qqchen.deploy.backend.deploy.entity.JenkinsView;
|
||||||
import com.qqchen.deploy.backend.framework.service.IBaseService;
|
import com.qqchen.deploy.backend.framework.service.IBaseService;
|
||||||
import com.qqchen.deploy.backend.deploy.entity.JenkinsBuild;
|
import com.qqchen.deploy.backend.deploy.entity.JenkinsBuild;
|
||||||
import com.qqchen.deploy.backend.deploy.dto.JenkinsBuildDTO;
|
import com.qqchen.deploy.backend.deploy.dto.JenkinsBuildDTO;
|
||||||
@ -13,20 +16,20 @@ public interface IJenkinsBuildService extends IBaseService<JenkinsBuild, Jenkins
|
|||||||
/**
|
/**
|
||||||
* 同步指定任务的构建信息
|
* 同步指定任务的构建信息
|
||||||
*
|
*
|
||||||
* @param externalSystemId 外部系统ID
|
* @param externalSystem 外部系统
|
||||||
* @param jobId 任务ID
|
* @param job 任务
|
||||||
* @return 同步的构建数量
|
* @return 同步的构建数量
|
||||||
*/
|
*/
|
||||||
Integer syncBuilds(Long externalSystemId, Long jobId);
|
Integer syncBuilds(ExternalSystem externalSystem, JenkinsJob job);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 同步指定视图下所有任务的构建信息
|
* 同步指定视图下所有任务的构建信息
|
||||||
*
|
*
|
||||||
* @param externalSystemId 外部系统ID
|
* @param externalSystem 外部系统
|
||||||
* @param viewId 视图ID
|
* @param view 视图
|
||||||
* @return 同步的构建总数
|
* @return 同步的构建总数
|
||||||
*/
|
*/
|
||||||
Integer syncBuildsByView(Long externalSystemId, Long viewId);
|
Integer syncBuildsByView(ExternalSystem externalSystem, JenkinsView view);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 同步外部系统下所有构建信息
|
* 同步外部系统下所有构建信息
|
||||||
|
|||||||
@ -74,7 +74,7 @@ public class JenkinsBuildServiceImpl extends BaseServiceImpl<JenkinsBuild, Jenki
|
|||||||
int totalSyncedBuilds = 0;
|
int totalSyncedBuilds = 0;
|
||||||
for (JenkinsView view : views) {
|
for (JenkinsView view : views) {
|
||||||
try {
|
try {
|
||||||
Integer syncedBuilds = syncBuildsByView(externalSystemId, view.getId());
|
Integer syncedBuilds = syncBuildsByView(externalSystem, view);
|
||||||
totalSyncedBuilds += syncedBuilds;
|
totalSyncedBuilds += syncedBuilds;
|
||||||
log.info("Successfully synchronized {} builds for view: {}", syncedBuilds, view.getViewName());
|
log.info("Successfully synchronized {} builds for view: {}", syncedBuilds, view.getViewName());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -89,11 +89,11 @@ public class JenkinsBuildServiceImpl extends BaseServiceImpl<JenkinsBuild, Jenki
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public Integer syncBuildsByView(Long externalSystemId, Long viewId) {
|
public Integer syncBuildsByView(ExternalSystem externalSystem, JenkinsView view) {
|
||||||
// 1. 查询视图下的所有任务
|
// 1. 查询视图下的所有任务
|
||||||
List<JenkinsJob> jobs = jenkinsJobRepository.findByExternalSystemIdAndViewId(externalSystemId, viewId);
|
List<JenkinsJob> jobs = jenkinsJobRepository.findByExternalSystemIdAndViewId(externalSystem.getId(), view.getId());
|
||||||
if (jobs.isEmpty()) {
|
if (jobs.isEmpty()) {
|
||||||
log.info("No jobs found for view: {}", viewId);
|
log.info("No jobs found for view: {}", view.getId());
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,7 +101,7 @@ public class JenkinsBuildServiceImpl extends BaseServiceImpl<JenkinsBuild, Jenki
|
|||||||
int totalSyncedBuilds = 0;
|
int totalSyncedBuilds = 0;
|
||||||
for (JenkinsJob job : jobs) {
|
for (JenkinsJob job : jobs) {
|
||||||
try {
|
try {
|
||||||
Integer syncedBuilds = syncBuilds(externalSystemId, job.getId());
|
Integer syncedBuilds = syncBuilds(externalSystem, job);
|
||||||
totalSyncedBuilds += syncedBuilds;
|
totalSyncedBuilds += syncedBuilds;
|
||||||
log.info("Successfully synchronized {} builds for job: {}", syncedBuilds, job.getJobName());
|
log.info("Successfully synchronized {} builds for job: {}", syncedBuilds, job.getJobName());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -114,18 +114,10 @@ public class JenkinsBuildServiceImpl extends BaseServiceImpl<JenkinsBuild, Jenki
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public Integer syncBuilds(Long externalSystemId, Long jobId) {
|
public Integer syncBuilds(ExternalSystem externalSystem, JenkinsJob job) {
|
||||||
// 1. 查询外部系统和任务
|
List<JenkinsBuildResponse> buildResponses = jenkinsServiceIntegration.listBuilds(externalSystem, job.getJobName());
|
||||||
ExternalSystem externalSystem = externalSystemRepository.findById(externalSystemId)
|
|
||||||
.orElseThrow(() -> new BusinessException(ResponseCode.EXTERNAL_SYSTEM_NOT_FOUND));
|
|
||||||
|
|
||||||
JenkinsJob jenkinsJob = jenkinsJobRepository.findById(jobId)
|
|
||||||
.orElseThrow(() -> new BusinessException(ResponseCode.DATA_NOT_FOUND));
|
|
||||||
|
|
||||||
// 2. 调用Jenkins API获取构建列表
|
|
||||||
List<JenkinsBuildResponse> buildResponses = jenkinsServiceIntegration.listBuilds(externalSystem, jenkinsJob.getJobName());
|
|
||||||
if (buildResponses.isEmpty()) {
|
if (buildResponses.isEmpty()) {
|
||||||
log.info("No builds found for job: {}", jenkinsJob.getJobName());
|
log.info("No builds found for job: {}", job.getJobName());
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,25 +125,22 @@ public class JenkinsBuildServiceImpl extends BaseServiceImpl<JenkinsBuild, Jenki
|
|||||||
List<JenkinsBuild> jenkinsBuilds = new ArrayList<>();
|
List<JenkinsBuild> jenkinsBuilds = new ArrayList<>();
|
||||||
for (JenkinsBuildResponse buildResponse : buildResponses) {
|
for (JenkinsBuildResponse buildResponse : buildResponses) {
|
||||||
// 查找是否存在相同的构建
|
// 查找是否存在相同的构建
|
||||||
Optional<JenkinsBuild> existingBuild = jenkinsBuildRepository
|
Optional<JenkinsBuild> existingBuild = jenkinsBuildRepository.findByExternalSystemIdAndJobIdAndBuildNumber(externalSystem.getId(), job.getId(), buildResponse.getNumber());
|
||||||
.findByExternalSystemIdAndJobIdAndBuildNumber(externalSystemId, jobId, buildResponse.getNumber());
|
|
||||||
|
|
||||||
JenkinsBuild jenkinsBuild;
|
JenkinsBuild jenkinsBuild;
|
||||||
if (existingBuild.isPresent()) {
|
if (existingBuild.isPresent()) {
|
||||||
// 更新已存在的构建
|
// 更新已存在的构建
|
||||||
jenkinsBuild = existingBuild.get();
|
jenkinsBuild = existingBuild.get();
|
||||||
updateBuildFromResponse(jenkinsBuild, buildResponse);
|
updateBuildFromResponse(jenkinsBuild, buildResponse);
|
||||||
log.debug("Updating existing Jenkins build: {} for job: {}",
|
log.debug("Updating existing Jenkins build: {} for job: {}", jenkinsBuild.getBuildNumber(), job.getJobName());
|
||||||
jenkinsBuild.getBuildNumber(), jenkinsJob.getJobName());
|
|
||||||
} else {
|
} else {
|
||||||
// 创建新的构建
|
// 创建新的构建
|
||||||
jenkinsBuild = new JenkinsBuild();
|
jenkinsBuild = new JenkinsBuild();
|
||||||
jenkinsBuild.setExternalSystemId(externalSystemId);
|
jenkinsBuild.setExternalSystemId(externalSystem.getId());
|
||||||
jenkinsBuild.setJobId(jobId);
|
jenkinsBuild.setJobId(job.getId());
|
||||||
jenkinsBuild.setBuildNumber(buildResponse.getNumber());
|
jenkinsBuild.setBuildNumber(buildResponse.getNumber());
|
||||||
updateBuildFromResponse(jenkinsBuild, buildResponse);
|
updateBuildFromResponse(jenkinsBuild, buildResponse);
|
||||||
log.debug("Creating new Jenkins build: {} for job: {}",
|
log.debug("Creating new Jenkins build: {} for job: {}", jenkinsBuild.getBuildNumber(), job.getJobName());
|
||||||
jenkinsBuild.getBuildNumber(), jenkinsJob.getJobName());
|
|
||||||
}
|
}
|
||||||
jenkinsBuilds.add(jenkinsBuild);
|
jenkinsBuilds.add(jenkinsBuild);
|
||||||
}
|
}
|
||||||
@ -159,7 +148,7 @@ public class JenkinsBuildServiceImpl extends BaseServiceImpl<JenkinsBuild, Jenki
|
|||||||
// 4. 批量保存或更新
|
// 4. 批量保存或更新
|
||||||
jenkinsBuildRepository.saveAll(jenkinsBuilds);
|
jenkinsBuildRepository.saveAll(jenkinsBuilds);
|
||||||
|
|
||||||
log.info("Successfully synchronized {} Jenkins builds for job: {}", jenkinsBuilds.size(), jenkinsJob.getJobName());
|
log.info("Successfully synchronized {} Jenkins builds for job: {}", jenkinsBuilds.size(), job.getJobName());
|
||||||
|
|
||||||
return jenkinsBuilds.size();
|
return jenkinsBuilds.size();
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user