增加生成后端服务代码。
This commit is contained in:
parent
9a01ae8d30
commit
394315f7f0
@ -27,16 +27,16 @@ public class JenkinsJob extends Entity<Long> {
|
|||||||
@Column(name = "job_url", nullable = false)
|
@Column(name = "job_url", nullable = false)
|
||||||
private String jobUrl;
|
private String jobUrl;
|
||||||
|
|
||||||
@Column(name = "next_build_number", nullable = false)
|
@Column(name = "next_build_number")
|
||||||
private Integer nextBuildNumber;
|
private Integer nextBuildNumber;
|
||||||
|
|
||||||
@Column(name = "last_build_number", nullable = false)
|
@Column(name = "last_build_number")
|
||||||
private Integer lastBuildNumber;
|
private Integer lastBuildNumber;
|
||||||
|
|
||||||
@Column(name = "last_build_status", nullable = false)
|
@Column(name = "last_build_status")
|
||||||
private String lastBuildStatus;
|
private String lastBuildStatus;
|
||||||
|
|
||||||
@Column(name = "health_report_score", nullable = false)
|
@Column(name = "health_report_score")
|
||||||
private Integer healthReportScore;
|
private Integer healthReportScore;
|
||||||
|
|
||||||
@Column(name = "last_build_time")
|
@Column(name = "last_build_time")
|
||||||
|
|||||||
@ -269,7 +269,7 @@ public class JenkinsServiceIntegration implements IJenkinsServiceIntegration {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Failed to call Jenkins API: path={}, error={}, responseType={}",
|
log.error("Failed to call Jenkins API: path={}, error={}, responseType={}",
|
||||||
path, e.getMessage(), responseType.getSimpleName(), e);
|
path, e.getMessage(), responseType.getSimpleName(), e);
|
||||||
throw new RuntimeException("调用Jenkins API<EFBFBD><EFBFBD>败: " + path, e);
|
throw new RuntimeException("调用Jenkins API失败: " + path, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -314,9 +314,9 @@ public class JenkinsServiceIntegration implements IJenkinsServiceIntegration {
|
|||||||
* @return 任务列表
|
* @return 任务列表
|
||||||
*/
|
*/
|
||||||
public List<JenkinsJobResponse> listJobs(ExternalSystem externalSystem, String viewName) {
|
public List<JenkinsJobResponse> listJobs(ExternalSystem externalSystem, String viewName) {
|
||||||
// 只查询必要的字段
|
// 只查询必要的字段,确保包含lastBuild的timestamp字段
|
||||||
String treeQuery = "jobs[name,url,description,buildable,nextBuildNumber," +
|
String treeQuery = "jobs[name,url,description,buildable,nextBuildNumber," +
|
||||||
"lastBuild[number],color]";
|
"lastBuild[number,timestamp,result],color]";
|
||||||
|
|
||||||
List<JenkinsJobResponse> jobs = callJenkinsApi(
|
List<JenkinsJobResponse> jobs = callJenkinsApi(
|
||||||
externalSystem,
|
externalSystem,
|
||||||
@ -326,19 +326,15 @@ public class JenkinsServiceIntegration implements IJenkinsServiceIntegration {
|
|||||||
JenkinsJobResponse.class
|
JenkinsJobResponse.class
|
||||||
);
|
);
|
||||||
|
|
||||||
// 过滤和清洗数据
|
|
||||||
String baseUrl = StringUtils.removeEnd(externalSystem.getUrl(), "/");
|
String baseUrl = StringUtils.removeEnd(externalSystem.getUrl(), "/");
|
||||||
return jobs.stream()
|
return jobs.stream()
|
||||||
.peek(job -> {
|
.peek(job -> {
|
||||||
// 清理描述中的多余空格
|
|
||||||
if (job.getDescription() != null) {
|
if (job.getDescription() != null) {
|
||||||
job.setDescription(job.getDescription().trim());
|
job.setDescription(job.getDescription().trim());
|
||||||
}
|
}
|
||||||
// 转换URL为相对路径
|
|
||||||
if (job.getUrl() != null) {
|
if (job.getUrl() != null) {
|
||||||
job.setUrl(job.getUrl().replace(baseUrl, ""));
|
job.setUrl(job.getUrl().replace(baseUrl, ""));
|
||||||
}
|
}
|
||||||
// 设置默认值
|
|
||||||
if (job.getBuildable() == null) {
|
if (job.getBuildable() == null) {
|
||||||
job.setBuildable(false);
|
job.setBuildable(false);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -24,6 +24,12 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.OptionalDouble;
|
import java.util.OptionalDouble;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.ZoneId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Jenkins任务 Service实现
|
* Jenkins任务 Service实现
|
||||||
@ -92,42 +98,43 @@ public class JenkinsJobServiceImpl extends BaseServiceImpl<JenkinsJob, JenkinsJo
|
|||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public Integer syncJobsByView(ExternalSystem externalSystem, JenkinsView view) {
|
public Integer syncJobsByView(ExternalSystem externalSystem, JenkinsView view) {
|
||||||
|
// 1. 获取Jenkins任务列表
|
||||||
List<JenkinsJobResponse> jobResponses = jenkinsServiceIntegration.listJobs(externalSystem, view.getViewName());
|
List<JenkinsJobResponse> jobResponses = jenkinsServiceIntegration.listJobs(externalSystem, view.getViewName());
|
||||||
if (jobResponses.isEmpty()) {
|
if (jobResponses.isEmpty()) {
|
||||||
log.info("No jobs found in Jenkins view: {}", view.getViewName());
|
log.info("No jobs found in Jenkins view: {}", view.getViewName());
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 2. 查询现有任务
|
||||||
|
List<JenkinsJob> existingJobs = jenkinsJobRepository.findByExternalSystemIdAndViewId(
|
||||||
|
externalSystem.getId(),
|
||||||
|
view.getId()
|
||||||
|
);
|
||||||
|
Map<String, JenkinsJob> jobMap = existingJobs.stream()
|
||||||
|
.collect(Collectors.toMap(JenkinsJob::getJobName, Function.identity()));
|
||||||
|
|
||||||
// 3. 转换并保存/更新任务数据
|
// 3. 转换并保存/更新任务数据
|
||||||
List<JenkinsJob> jenkinsJobs = new ArrayList<>();
|
List<JenkinsJob> jenkinsJobs = new ArrayList<>();
|
||||||
for (JenkinsJobResponse jobResponse : jobResponses) {
|
for (JenkinsJobResponse jobResponse : jobResponses) {
|
||||||
// 查找是否存在相同的任务
|
JenkinsJob jenkinsJob = jobMap.getOrDefault(jobResponse.getName(), new JenkinsJob());
|
||||||
Optional<JenkinsJob> existingJob = jenkinsJobRepository.findByExternalSystemIdAndViewIdAndJobName(externalSystem.getId(), view.getId(), jobResponse.getName());
|
|
||||||
|
// 设置基本信息
|
||||||
JenkinsJob jenkinsJob;
|
jenkinsJob.setExternalSystemId(externalSystem.getId());
|
||||||
if (existingJob.isPresent()) {
|
jenkinsJob.setViewId(view.getId());
|
||||||
// 更新已存在的任务
|
jenkinsJob.setJobName(jobResponse.getName());
|
||||||
jenkinsJob = existingJob.get();
|
|
||||||
updateJobFromResponse(jenkinsJob, jobResponse);
|
// 更新任务信息
|
||||||
log.debug("Updating existing Jenkins job: {}", jenkinsJob.getJobName());
|
updateJobFromResponse(jenkinsJob, jobResponse);
|
||||||
} else {
|
|
||||||
// 创建新的任务
|
// 添加到待保存列表
|
||||||
jenkinsJob = new JenkinsJob();
|
|
||||||
jenkinsJob.setExternalSystemId(externalSystem.getId());
|
|
||||||
jenkinsJob.setViewId(view.getId());
|
|
||||||
jenkinsJob.setJobName(jobResponse.getName());
|
|
||||||
updateJobFromResponse(jenkinsJob, jobResponse);
|
|
||||||
log.debug("Creating new Jenkins job: {}", jenkinsJob.getJobName());
|
|
||||||
}
|
|
||||||
jenkinsJobs.add(jenkinsJob);
|
jenkinsJobs.add(jenkinsJob);
|
||||||
|
log.debug("{} Jenkins job: {}", jenkinsJob.getId() == null ? "Creating new" : "Updating existing", jenkinsJob.getJobName());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. 批量保存或更新
|
// 4. 批量保存或更新
|
||||||
jenkinsJobRepository.saveAll(jenkinsJobs);
|
jenkinsJobRepository.saveAll(jenkinsJobs);
|
||||||
|
|
||||||
log.info("Successfully synchronized {} Jenkins jobs for view: {}", jenkinsJobs.size(), view.getViewName());
|
log.info("Successfully synchronized {} Jenkins jobs for view: {}", jenkinsJobs.size(), view.getViewName());
|
||||||
|
|
||||||
return jenkinsJobs.size();
|
return jenkinsJobs.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,8 +150,20 @@ public class JenkinsJobServiceImpl extends BaseServiceImpl<JenkinsJob, JenkinsJo
|
|||||||
// 设置最后构建信息
|
// 设置最后构建信息
|
||||||
if (response.getLastBuild() != null) {
|
if (response.getLastBuild() != null) {
|
||||||
jenkinsJob.setLastBuildNumber(response.getLastBuild().getNumber());
|
jenkinsJob.setLastBuildNumber(response.getLastBuild().getNumber());
|
||||||
// 根据颜色判断构建状态
|
|
||||||
jenkinsJob.setLastBuildStatus(convertColorToStatus(response.getColor()));
|
jenkinsJob.setLastBuildStatus(convertColorToStatus(response.getColor()));
|
||||||
|
|
||||||
|
// 设置最后构建时间
|
||||||
|
if (response.getLastBuild().getTimestamp() != null) {
|
||||||
|
jenkinsJob.setLastBuildTime(LocalDateTime.ofInstant(
|
||||||
|
Instant.ofEpochMilli(response.getLastBuild().getTimestamp()),
|
||||||
|
ZoneId.systemDefault()
|
||||||
|
));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 如果没有最后构建信息,设置默认值
|
||||||
|
jenkinsJob.setLastBuildNumber(0);
|
||||||
|
jenkinsJob.setLastBuildStatus("NOT_BUILT");
|
||||||
|
jenkinsJob.setLastBuildTime(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置健康分数
|
// 设置健康分数
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user