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