feat(流水线解析): 增加对工具类节点的解析

This commit is contained in:
liuhuazhong 2024-11-29 16:42:09 +08:00
parent 57c4e62daa
commit da1ec52f91
2 changed files with 78 additions and 58 deletions

View File

@ -24,28 +24,37 @@ public class NodeActionVo {
this.name = name;
}
public void buildOn(PipelineVo.PipelineJson.Nodes node) {
// this.on.put("push", Collections.singletonMap("branches", Collections.singletonList(branchName)));
String fullName = node.getFull_name();
Map<String, Object> step = new LinkedHashMap<>();
public void parse(List<Map<String, Object>> steps, PipelineVo.PipelineJson.Nodes node){
String nodeName = Optional.ofNullable(node.getName()).orElse("");
if (nodeName.startsWith("on-")) {
buildOn(node);
}else if (nodeName.startsWith("setup-")) {
buildSetupStep(steps, node);
}else if (Objects.equals(nodeName,"shell")) {
buildShellStep(steps, node);
}else if (StringUtils.isNotEmpty(nodeName)) {
buildOtherStep(steps, node);
}
}
if (Objects.equals(fullName, "on-schedule")) {
List<PipelineVo.PipelineJson.Nodes.Inputs> inputs = Optional.ofNullable(node.getInputs()).orElse(new ArrayList<>());
public void buildOn(PipelineVo.PipelineJson.Nodes node) {
String nodeName = node.getName();
Map<String, Object> step = new LinkedHashMap<>();
List<PipelineVo.PipelineJson.Nodes.Inputs> inputs = getInputs(node);
if (Objects.equals(nodeName, "on-schedule")) {
inputs.forEach(e -> {
step.put(e.getName(), e.getValue());
});
this.on.put(fullName.split("-")[1], step);
this.on.put(nodeName.split("-")[1], step);
return;
}
if (fullName.startsWith("on-")) {
List<PipelineVo.PipelineJson.Nodes.Inputs> inputs = Optional.ofNullable(node.getInputs()).orElse(new ArrayList<>()).stream().filter(e -> StringUtils.isNotBlank(e.getValue())).collect(Collectors.toList());
inputs.forEach(e -> {
step.put(e.getName(), e.getValue().replace("\'","").split(","));
});
inputs.forEach(e -> {
step.put(e.getName(), e.getValue().replace("\'","").split(","));
});
this.on.put(fullName.split("-")[1], step);
}
this.on.put(nodeName.split("-")[1], step);
}
public void buildJobs(String jobKey, String jobName, List<Map<String, Object>> steps) {
@ -54,17 +63,19 @@ public class NodeActionVo {
job.put("runs-on", "ubuntu-latest");
job.put("name", jobName);
job.put("steps", steps);
this.jobs.put(jobKey, job);
this.jobs.put(jobKey+(this.jobs.size()), job);
}
public void buildSetupStep(List<Map<String, Object>> steps, PipelineVo.PipelineJson.Nodes node) {
String name = node.getName();
String nodeName = node.getName();
Map<String, Object> step = new LinkedHashMap<>();
if (name.startsWith("setup-java")) {
List<PipelineVo.PipelineJson.Nodes.Inputs> inputs = Optional.ofNullable(node.getInputs()).orElse(new ArrayList<>()).stream().filter(e -> StringUtils.isNotBlank(e.getValue())).collect(Collectors.toList());
List<PipelineVo.PipelineJson.Nodes.Inputs> inputs = getInputs(node);
Map<String, String> name2Value = inputs.stream().collect(Collectors.toMap(e -> e.getName(), e -> e.getValue(), (o, n) -> n));
if (nodeName.startsWith("setup-java")) {
Map<String, String> name2Value = inputs.stream()
.collect(Collectors.toMap(e -> e.getName(), e -> e.getValue(), (o, n) -> n));
Map<String, Object> with = new LinkedHashMap<>();
if (name2Value.containsKey("download_url") && StringUtils.isNotBlank(name2Value.get("download_url"))) {
@ -73,7 +84,7 @@ public class NodeActionVo {
with.put("jdkFile", "${{ runner.temp }}/java_package.tar.gz");
}
step.put("name", node.getLabel());
step.put("uses", node.getFull_name());
step.put("uses", node.getFull_name().trim());
if (!inputs.isEmpty()) {
inputs.forEach(i -> {
@ -86,35 +97,52 @@ public class NodeActionVo {
return;
}
if (name.startsWith("setup-")) {
List<PipelineVo.PipelineJson.Nodes.Inputs> inputs = Optional.ofNullable(node.getInputs()).orElse(new ArrayList<>()).stream().filter(e -> StringUtils.isNotBlank(e.getValue())).collect(Collectors.toList());
step.put("name", node.getLabel());
step.put("uses", node.getFull_name().trim());
if (!inputs.isEmpty()) {
Map<String, Object> with = new LinkedHashMap<>();
inputs.forEach(i -> {
with.put(i.getName(), Optional.ofNullable(i.getValue()).orElse(""));
});
step.put("name", node.getLabel());
step.put("uses", node.getFull_name());
if (!inputs.isEmpty()) {
Map<String, Object> with = new LinkedHashMap<>();
inputs.forEach(i -> {
with.put(i.getName(), Optional.ofNullable(i.getValue()).orElse(""));
});
step.put("with", with);
}
steps.add(step);
step.put("with", with);
}
steps.add(step);
}
public void buildShellStep(List<Map<String, Object>> steps, PipelineVo.PipelineJson.Nodes node) {
String fullName = node.getFull_name();
if (Objects.equals(fullName, "shell")) {
Map<String, Object> step = new LinkedHashMap<>();
step.put("name", node.getLabel());
// step.put("run","");
List<PipelineVo.PipelineJson.Nodes.Inputs> inputs = Optional.ofNullable(node.getInputs()).orElse(new ArrayList<>());
Map<String, Object> step = new LinkedHashMap<>();
step.put("name", node.getLabel());
List<PipelineVo.PipelineJson.Nodes.Inputs> inputs = Optional.ofNullable(node.getInputs()).orElse(new ArrayList<>());
inputs.forEach(i -> {
step.put(i.getName(), Optional.ofNullable(i.getValue()).orElse(""));
});
steps.add(step);
}
public void buildOtherStep(List<Map<String, Object>> steps, PipelineVo.PipelineJson.Nodes node) {
Map<String, Object> step = new LinkedHashMap<>();
List<PipelineVo.PipelineJson.Nodes.Inputs> inputs = getInputs(node);
step.put("name", node.getLabel());
step.put("uses", node.getFull_name().trim());
if (!inputs.isEmpty()) {
Map<String, Object> with = new LinkedHashMap<>();
inputs.forEach(i -> {
step.put(i.getName(), Optional.ofNullable(i.getValue()).orElse(""));
with.put(i.getName(), Optional.ofNullable(i.getValue()).orElse(""));
});
steps.add(step);
step.put("with", with);
}
steps.add(step);
}
private List<PipelineVo.PipelineJson.Nodes.Inputs> getInputs(PipelineVo.PipelineJson.Nodes node) {
return Optional.ofNullable(node.getInputs()).orElse(new ArrayList<>())
.stream()
.filter(e -> StringUtils.isNotBlank(e.getValue())).collect(Collectors.toList());
}
}

View File

@ -378,7 +378,15 @@ public class PmsCiPipelinesServiceImpl implements IPmsCiPipelinesService {
try {
PipelineVo vo = JSON.parseObject(JSON.toJSONString(pmsCiPipelineBuildInputVo), PipelineVo.class);
NodeActionVo actionVo = getNodeActionVo(vo);
List<PipelineVo.PipelineJson.Nodes> nodes = vo.getPipelineJson().getNodes();
String pipelineName = vo.getPipelineName();
NodeActionVo actionVo = new NodeActionVo();
actionVo.buildName(pipelineName);
nodes.forEach(s -> {
List<Map<String, Object>> steps = new ArrayList<>();
actionVo.parse(steps, s);
actionVo.buildJobs("job", s.getLabel()+String.format("[%s]",s.getId().trim()), steps);
});
String yaml = YamlUtil.toYaml(actionVo);
JSONObject result = new JSONObject();
result.put("pipeline_yaml", yaml);
@ -389,22 +397,6 @@ public class PmsCiPipelinesServiceImpl implements IPmsCiPipelinesService {
}
}
private NodeActionVo getNodeActionVo(PipelineVo vo) {
List<PipelineVo.PipelineJson.Nodes> nodes = vo.getPipelineJson().getNodes();
String pipelineName = vo.getPipelineName();
NodeActionVo actionVo = new NodeActionVo();
actionVo.buildName(pipelineName);
AtomicInteger i = new AtomicInteger();
nodes.forEach(s -> {
List<Map<String, Object>> steps = new ArrayList<>();
actionVo.buildOn(s);
actionVo.buildSetupStep(steps, s);
actionVo.buildShellStep(steps, s);
actionVo.buildJobs("job" + (i.getAndIncrement()), s.getLabel(), steps);
});
return actionVo;
}
@Override
public JSONObject reRunPipelineRunsJob(Long id, String run, String job) {
PmsCiPipelines pmsCiPipelines = pmsCiPipelinesMapper.selectPmsCiPipelinesById(id);