增加生成后端服务代码。

This commit is contained in:
asp_ly 2024-12-29 00:14:01 +08:00
parent 9a01ae8d30
commit 394315f7f0
3 changed files with 47 additions and 32 deletions

View File

@ -27,16 +27,16 @@ public class JenkinsJob extends Entity<Long> {
@Column(name = "job_url", nullable = false)
private String jobUrl;
@Column(name = "next_build_number", nullable = false)
@Column(name = "next_build_number")
private Integer nextBuildNumber;
@Column(name = "last_build_number", nullable = false)
@Column(name = "last_build_number")
private Integer lastBuildNumber;
@Column(name = "last_build_status", nullable = false)
@Column(name = "last_build_status")
private String lastBuildStatus;
@Column(name = "health_report_score", nullable = false)
@Column(name = "health_report_score")
private Integer healthReportScore;
@Column(name = "last_build_time")

View File

@ -269,7 +269,7 @@ public class JenkinsServiceIntegration implements IJenkinsServiceIntegration {
} catch (Exception e) {
log.error("Failed to call Jenkins API: path={}, error={}, responseType={}",
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 任务列表
*/
public List<JenkinsJobResponse> listJobs(ExternalSystem externalSystem, String viewName) {
// 只查询必要的字段
// 只查询必要的字段确保包含lastBuild的timestamp字段
String treeQuery = "jobs[name,url,description,buildable,nextBuildNumber," +
"lastBuild[number],color]";
"lastBuild[number,timestamp,result],color]";
List<JenkinsJobResponse> jobs = callJenkinsApi(
externalSystem,
@ -326,19 +326,15 @@ public class JenkinsServiceIntegration implements IJenkinsServiceIntegration {
JenkinsJobResponse.class
);
// 过滤和清洗数据
String baseUrl = StringUtils.removeEnd(externalSystem.getUrl(), "/");
return jobs.stream()
.peek(job -> {
// 清理描述中的多余空格
if (job.getDescription() != null) {
job.setDescription(job.getDescription().trim());
}
// 转换URL为相对路径
if (job.getUrl() != null) {
job.setUrl(job.getUrl().replace(baseUrl, ""));
}
// 设置默认值
if (job.getBuildable() == null) {
job.setBuildable(false);
}

View File

@ -24,6 +24,12 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
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实现
@ -92,42 +98,43 @@ public class JenkinsJobServiceImpl extends BaseServiceImpl<JenkinsJob, JenkinsJo
@Override
@Transactional(rollbackFor = Exception.class)
public Integer syncJobsByView(ExternalSystem externalSystem, JenkinsView view) {
// 1. 获取Jenkins任务列表
List<JenkinsJobResponse> jobResponses = jenkinsServiceIntegration.listJobs(externalSystem, view.getViewName());
if (jobResponses.isEmpty()) {
log.info("No jobs found in Jenkins view: {}", view.getViewName());
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. 转换并保存/更新任务数据
List<JenkinsJob> jenkinsJobs = new ArrayList<>();
for (JenkinsJobResponse jobResponse : jobResponses) {
// 查找是否存在相同的任务
Optional<JenkinsJob> existingJob = jenkinsJobRepository.findByExternalSystemIdAndViewIdAndJobName(externalSystem.getId(), view.getId(), jobResponse.getName());
JenkinsJob jenkinsJob = jobMap.getOrDefault(jobResponse.getName(), new JenkinsJob());
JenkinsJob jenkinsJob;
if (existingJob.isPresent()) {
// 更新已存在的任务
jenkinsJob = existingJob.get();
updateJobFromResponse(jenkinsJob, jobResponse);
log.debug("Updating existing Jenkins job: {}", jenkinsJob.getJobName());
} 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());
}
// 设置基本信息
jenkinsJob.setExternalSystemId(externalSystem.getId());
jenkinsJob.setViewId(view.getId());
jenkinsJob.setJobName(jobResponse.getName());
// 更新任务信息
updateJobFromResponse(jenkinsJob, jobResponse);
// 添加到待保存列表
jenkinsJobs.add(jenkinsJob);
log.debug("{} Jenkins job: {}", jenkinsJob.getId() == null ? "Creating new" : "Updating existing", jenkinsJob.getJobName());
}
// 4. 批量保存或更新
jenkinsJobRepository.saveAll(jenkinsJobs);
log.info("Successfully synchronized {} Jenkins jobs for view: {}", jenkinsJobs.size(), view.getViewName());
return jenkinsJobs.size();
}
@ -143,8 +150,20 @@ public class JenkinsJobServiceImpl extends BaseServiceImpl<JenkinsJob, JenkinsJo
// 设置最后构建信息
if (response.getLastBuild() != null) {
jenkinsJob.setLastBuildNumber(response.getLastBuild().getNumber());
// 根据颜色判断构建状态
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);
}
// 设置健康分数