增加构建通知
This commit is contained in:
parent
33f648d79f
commit
0795570470
@ -151,10 +151,11 @@ public class JenkinsBuildResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
public static class ChangeSetItem {
|
public static class ChangeSetItem {
|
||||||
private String commitId;
|
private String commitId;
|
||||||
private String author;
|
private String author;
|
||||||
private String message;
|
private String msg;
|
||||||
private Long timestamp;
|
private Long timestamp;
|
||||||
private List<String> affectedPaths;
|
private List<String> affectedPaths;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -560,10 +560,18 @@ public class JenkinsBuildServiceImpl extends BaseServiceImpl<JenkinsBuild, Jenki
|
|||||||
if (latestBuild == null) continue;
|
if (latestBuild == null) continue;
|
||||||
|
|
||||||
// 从 Jenkins API 实时获取最新状态(每个Job只调用一次)
|
// 从 Jenkins API 实时获取最新状态(每个Job只调用一次)
|
||||||
JenkinsBuildResponse latestBuildInfo = fetchLatestBuildInfo(externalSystem, job, latestBuild.getBuildNumber());
|
JenkinsBuildResponse latestBuildInfo = null;
|
||||||
String latestStatus = (latestBuildInfo != null && latestBuildInfo.getResult() != null)
|
String latestStatus = "BUILDING";
|
||||||
? latestBuildInfo.getResult()
|
try {
|
||||||
: "BUILDING";
|
latestBuildInfo = jenkinsServiceIntegration.getBuildDetails(externalSystem, job.getJobName(), latestBuild.getBuildNumber());
|
||||||
|
if (latestBuildInfo != null && latestBuildInfo.getResult() != null) {
|
||||||
|
latestStatus = latestBuildInfo.getResult();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("从Jenkins获取构建信息失败,跳过本次处理,等待下次重试: job={}, buildNumber={}, error={}",
|
||||||
|
job.getJobName(), latestBuild.getBuildNumber(), e.getMessage());
|
||||||
|
continue; // 跳过这个 Job,下次定时任务重试
|
||||||
|
}
|
||||||
|
|
||||||
// 更新数据库中的构建状态(如果有变化)
|
// 更新数据库中的构建状态(如果有变化)
|
||||||
if (latestBuildInfo != null && !latestStatus.equals(latestBuild.getBuildStatus())) {
|
if (latestBuildInfo != null && !latestStatus.equals(latestBuild.getBuildStatus())) {
|
||||||
@ -681,22 +689,6 @@ public class JenkinsBuildServiceImpl extends BaseServiceImpl<JenkinsBuild, Jenki
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 从 Jenkins API 实时获取指定构建的最新信息
|
|
||||||
*/
|
|
||||||
private JenkinsBuildResponse fetchLatestBuildInfo(ExternalSystem externalSystem, JenkinsJob job, Integer buildNumber) {
|
|
||||||
try {
|
|
||||||
List<JenkinsBuildResponse> builds = jenkinsServiceIntegration.listBuilds(externalSystem, job.getJobName());
|
|
||||||
return builds.stream()
|
|
||||||
.filter(b -> b.getNumber().equals(buildNumber))
|
|
||||||
.findFirst()
|
|
||||||
.orElse(null);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.warn("从Jenkins获取构建信息失败: job={}, buildNumber={}, error={}", job.getJobName(), buildNumber, e.getMessage());
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断状态是否为已完成
|
* 判断状态是否为已完成
|
||||||
*/
|
*/
|
||||||
@ -869,17 +861,18 @@ public class JenkinsBuildServiceImpl extends BaseServiceImpl<JenkinsBuild, Jenki
|
|||||||
JsonNode items = changeSet.get("items");
|
JsonNode items = changeSet.get("items");
|
||||||
if (items != null && items.isArray()) {
|
if (items != null && items.isArray()) {
|
||||||
for (JsonNode item : items) {
|
for (JsonNode item : items) {
|
||||||
String message = item.has("message") ? item.get("message").asText() : "";
|
// Jenkins API 返回的字段名是 msg
|
||||||
|
String msg = item.has("msg") ? item.get("msg").asText() : "";
|
||||||
String author = item.has("author") ? item.get("author").asText() : "";
|
String author = item.has("author") ? item.get("author").asText() : "";
|
||||||
String commitId = item.has("commitId") ? item.get("commitId").asText() : "";
|
String commitId = item.has("commitId") ? item.get("commitId").asText() : "";
|
||||||
|
|
||||||
if (!message.isEmpty()) {
|
if (!msg.isEmpty()) {
|
||||||
if (commitMessages.length() > 0) {
|
if (commitMessages.length() > 0) {
|
||||||
commitMessages.append("\n");
|
commitMessages.append("\n");
|
||||||
}
|
}
|
||||||
// 截取 commitId 前8位
|
// 截取 commitId 前8位
|
||||||
String shortCommitId = commitId.length() > 8 ? commitId.substring(0, 8) : commitId;
|
String shortCommitId = commitId.length() > 8 ? commitId.substring(0, 8) : commitId;
|
||||||
commitMessages.append(String.format("[%s] %s - %s", shortCommitId, message.trim(), author));
|
commitMessages.append(String.format("[%s] %s - %s", shortCommitId, msg.trim(), author));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user