增加同步锁

This commit is contained in:
dengqichen 2025-12-02 13:54:26 +08:00
parent 20670df44c
commit 0ccb068bfd
4 changed files with 30 additions and 26 deletions

View File

@ -509,23 +509,14 @@ public class JenkinsServiceIntegrationImpl extends BaseExternalSystemIntegration
@Override
public JenkinsBuildResponse getBuildDetails(ExternalSystem externalSystem, String jobName, Integer buildNumber) {
try {
// 构建 tree 参数只获取我们需要的字段
String treeQuery = "number,url,result,duration,timestamp,building," +
// 变更集字段更精确author.fullName msg
"changeSets[kind,items[commitId,author[fullName],msg,timestamp,affectedPaths]]," +
// 制品
"artifacts[fileName,relativePath,displayPath]," +
// Git 插件 BuildData兜底的 commit 信息
"actions[_class,lastBuiltRevision[SHA1,branch[name,SHA1]]]";
// 直接使用原始系统信息构建URLURL不需要解密
// 不使用 tree 参数直接获取完整的 build 详情与老项目保持一致
// 原因使用 tree 参数时如果构建没有代码变更Jenkins 不会返回 changeSets 字段
String url = UriComponentsBuilder.fromHttpUrl(externalSystem.getUrl())
.path("/job/")
.path(jobName)
.path("/")
.path(buildNumber.toString())
.path("/api/json")
.queryParam("tree", treeQuery)
.build()
.toUriString();
@ -563,18 +554,19 @@ public class JenkinsServiceIntegrationImpl extends BaseExternalSystemIntegration
}
}
// 提取 gitCommitId优先 changeSets.items[0].commitId兜底 actions.BuildData.lastBuiltRevision.SHA1
// 提取 gitCommitId优先 changeSet/changeSets兜底 actions.BuildData.lastBuiltRevision.SHA1
String commitId = null;
try {
// 先尝试从 changeSets
if (buildResponse.getChangeSets() != null && !buildResponse.getChangeSets().isEmpty()) {
var first = buildResponse.getChangeSets().get(0);
if (first.getItems() != null && !first.getItems().isEmpty()) {
commitId = first.getItems().get(0).getCommitId();
}
// 1. 先尝试从 changeSet单数FreeStyle 任务获取
if (buildResponse.getChangeSet() != null &&
buildResponse.getChangeSet().getItems() != null &&
!buildResponse.getChangeSet().getItems().isEmpty()) {
// 取最后一个 commit最新的
var items = buildResponse.getChangeSet().getItems();
commitId = items.get(items.size() - 1).getCommitId();
}
// 2. 兜底解析原始 JSON actions 查找 BuildData
if (commitId == null) {
// 兜底解析原始 JSON actions 查找 BuildData
com.fasterxml.jackson.databind.JsonNode root = mapper.readTree(response.getBody());
com.fasterxml.jackson.databind.JsonNode actions = root.get("actions");
if (actions != null && actions.isArray()) {

View File

@ -104,9 +104,9 @@ public class JenkinsBuildResponse {
private List<BuildParameter> actions;
/**
* 构建变更集
* 构建变更集Jenkins FreeStyle 任务返回 changeSet 单数形式
*/
private List<ChangeSet> changeSets;
private ChangeSet changeSet;
/**
* 构建制品
@ -154,12 +154,23 @@ public class JenkinsBuildResponse {
@JsonIgnoreProperties(ignoreUnknown = true)
public static class ChangeSetItem {
private String commitId;
private String author;
private Author author; // Jenkins 返回的是对象不是字符串
private String authorEmail;
private String msg;
private String comment;
private Long timestamp;
private String date;
private String id;
private List<String> affectedPaths;
}
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Author {
private String absoluteUrl;
private String fullName;
}
@Data
public static class Artifact {
private String displayPath;

View File

@ -332,10 +332,10 @@ public class JenkinsBuildServiceImpl extends BaseServiceImpl<JenkinsBuild, Jenki
}
try {
// 保存 actions changeSets JSON
// 保存 actions changeSet JSON
Map<String, Object> buildData = new HashMap<>();
buildData.put("actions", response.getActions());
buildData.put("changeSets", response.getChangeSets());
buildData.put("changeSet", response.getChangeSet());
buildData.put("gitCommitId", response.getGitCommitId());
String actionsJson = objectMapper.writeValueAsString(buildData);
jenkinsBuild.setActions(actionsJson);

View File

@ -91,9 +91,10 @@ public class JenkinsBuildDelegate extends BaseNodeDelegate<JenkinsBuildInputMapp
JenkinsBuildResponse buildDetails = jenkinsServiceIntegration.getBuildDetails(externalSystem, jobName, buildInfo.getBuildNumber());
// 打印调试信息仅数量避免大对象噪音
int changeSetsCount = (buildDetails.getChangeSets() != null) ? buildDetails.getChangeSets().size() : 0;
int changeSetItemsCount = (buildDetails.getChangeSet() != null && buildDetails.getChangeSet().getItems() != null)
? buildDetails.getChangeSet().getItems().size() : 0;
int artifactsCount = (buildDetails.getArtifacts() != null) ? buildDetails.getArtifacts().size() : 0;
log.info("Build details - changeSetsCount={}, artifactsCount={}", changeSetsCount, artifactsCount);
log.info("Build details - changeSetItemsCount={}, artifactsCount={}", changeSetItemsCount, artifactsCount);
// 7. 设置输出结果包含节点状态
fillOutputsFrom(buildInfo, buildDetails, buildStatus, output);