增加生成后端服务代码。

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) @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")

View File

@ -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);
} }

View File

@ -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; // 设置基本信息
if (existingJob.isPresent()) { jenkinsJob.setExternalSystemId(externalSystem.getId());
// 更新已存在的任务 jenkinsJob.setViewId(view.getId());
jenkinsJob = existingJob.get(); jenkinsJob.setJobName(jobResponse.getName());
updateJobFromResponse(jenkinsJob, jobResponse);
log.debug("Updating existing Jenkins job: {}", jenkinsJob.getJobName()); // 更新任务信息
} else { updateJobFromResponse(jenkinsJob, jobResponse);
// 创建新的任务
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);
} }
// 设置健康分数 // 设置健康分数