增加同步锁
This commit is contained in:
parent
20670df44c
commit
0ccb068bfd
@ -509,23 +509,14 @@ public class JenkinsServiceIntegrationImpl extends BaseExternalSystemIntegration
|
|||||||
@Override
|
@Override
|
||||||
public JenkinsBuildResponse getBuildDetails(ExternalSystem externalSystem, String jobName, Integer buildNumber) {
|
public JenkinsBuildResponse getBuildDetails(ExternalSystem externalSystem, String jobName, Integer buildNumber) {
|
||||||
try {
|
try {
|
||||||
// 构建 tree 参数,只获取我们需要的字段
|
// 不使用 tree 参数,直接获取完整的 build 详情(与老项目保持一致)
|
||||||
String treeQuery = "number,url,result,duration,timestamp,building," +
|
// 原因:使用 tree 参数时,如果构建没有代码变更,Jenkins 不会返回 changeSets 字段
|
||||||
// 变更集字段更精确: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]]]";
|
|
||||||
|
|
||||||
// 直接使用原始系统信息构建URL(URL不需要解密)
|
|
||||||
String url = UriComponentsBuilder.fromHttpUrl(externalSystem.getUrl())
|
String url = UriComponentsBuilder.fromHttpUrl(externalSystem.getUrl())
|
||||||
.path("/job/")
|
.path("/job/")
|
||||||
.path(jobName)
|
.path(jobName)
|
||||||
.path("/")
|
.path("/")
|
||||||
.path(buildNumber.toString())
|
.path(buildNumber.toString())
|
||||||
.path("/api/json")
|
.path("/api/json")
|
||||||
.queryParam("tree", treeQuery)
|
|
||||||
.build()
|
.build()
|
||||||
.toUriString();
|
.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;
|
String commitId = null;
|
||||||
try {
|
try {
|
||||||
// 先尝试从 changeSets 拿
|
// 1. 先尝试从 changeSet(单数,FreeStyle 任务)获取
|
||||||
if (buildResponse.getChangeSets() != null && !buildResponse.getChangeSets().isEmpty()) {
|
if (buildResponse.getChangeSet() != null &&
|
||||||
var first = buildResponse.getChangeSets().get(0);
|
buildResponse.getChangeSet().getItems() != null &&
|
||||||
if (first.getItems() != null && !first.getItems().isEmpty()) {
|
!buildResponse.getChangeSet().getItems().isEmpty()) {
|
||||||
commitId = first.getItems().get(0).getCommitId();
|
// 取最后一个 commit(最新的)
|
||||||
}
|
var items = buildResponse.getChangeSet().getItems();
|
||||||
|
commitId = items.get(items.size() - 1).getCommitId();
|
||||||
}
|
}
|
||||||
|
// 2. 兜底:解析原始 JSON 的 actions 查找 BuildData
|
||||||
if (commitId == null) {
|
if (commitId == null) {
|
||||||
// 兜底:解析原始 JSON 的 actions 查找 BuildData
|
|
||||||
com.fasterxml.jackson.databind.JsonNode root = mapper.readTree(response.getBody());
|
com.fasterxml.jackson.databind.JsonNode root = mapper.readTree(response.getBody());
|
||||||
com.fasterxml.jackson.databind.JsonNode actions = root.get("actions");
|
com.fasterxml.jackson.databind.JsonNode actions = root.get("actions");
|
||||||
if (actions != null && actions.isArray()) {
|
if (actions != null && actions.isArray()) {
|
||||||
|
|||||||
@ -104,9 +104,9 @@ public class JenkinsBuildResponse {
|
|||||||
private List<BuildParameter> actions;
|
private List<BuildParameter> actions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构建变更集
|
* 构建变更集(Jenkins FreeStyle 任务返回 changeSet 单数形式)
|
||||||
*/
|
*/
|
||||||
private List<ChangeSet> changeSets;
|
private ChangeSet changeSet;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构建制品
|
* 构建制品
|
||||||
@ -154,12 +154,23 @@ public class JenkinsBuildResponse {
|
|||||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
public static class ChangeSetItem {
|
public static class ChangeSetItem {
|
||||||
private String commitId;
|
private String commitId;
|
||||||
private String author;
|
private Author author; // Jenkins 返回的是对象,不是字符串
|
||||||
|
private String authorEmail;
|
||||||
private String msg;
|
private String msg;
|
||||||
|
private String comment;
|
||||||
private Long timestamp;
|
private Long timestamp;
|
||||||
|
private String date;
|
||||||
|
private String id;
|
||||||
private List<String> affectedPaths;
|
private List<String> affectedPaths;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
|
public static class Author {
|
||||||
|
private String absoluteUrl;
|
||||||
|
private String fullName;
|
||||||
|
}
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public static class Artifact {
|
public static class Artifact {
|
||||||
private String displayPath;
|
private String displayPath;
|
||||||
|
|||||||
@ -332,10 +332,10 @@ public class JenkinsBuildServiceImpl extends BaseServiceImpl<JenkinsBuild, Jenki
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 保存 actions 和 changeSets 到 JSON
|
// 保存 actions 和 changeSet 到 JSON
|
||||||
Map<String, Object> buildData = new HashMap<>();
|
Map<String, Object> buildData = new HashMap<>();
|
||||||
buildData.put("actions", response.getActions());
|
buildData.put("actions", response.getActions());
|
||||||
buildData.put("changeSets", response.getChangeSets());
|
buildData.put("changeSet", response.getChangeSet());
|
||||||
buildData.put("gitCommitId", response.getGitCommitId());
|
buildData.put("gitCommitId", response.getGitCommitId());
|
||||||
String actionsJson = objectMapper.writeValueAsString(buildData);
|
String actionsJson = objectMapper.writeValueAsString(buildData);
|
||||||
jenkinsBuild.setActions(actionsJson);
|
jenkinsBuild.setActions(actionsJson);
|
||||||
|
|||||||
@ -91,9 +91,10 @@ public class JenkinsBuildDelegate extends BaseNodeDelegate<JenkinsBuildInputMapp
|
|||||||
JenkinsBuildResponse buildDetails = jenkinsServiceIntegration.getBuildDetails(externalSystem, jobName, buildInfo.getBuildNumber());
|
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;
|
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. 设置输出结果(包含节点状态)
|
// 7. 设置输出结果(包含节点状态)
|
||||||
fillOutputsFrom(buildInfo, buildDetails, buildStatus, output);
|
fillOutputsFrom(buildInfo, buildDetails, buildStatus, output);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user