diff --git a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/pipeline/domain/vo/NodeActionVo.java b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/pipeline/domain/vo/NodeActionVo.java index 41cc9c0ad..62f1a26e0 100644 --- a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/pipeline/domain/vo/NodeActionVo.java +++ b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/pipeline/domain/vo/NodeActionVo.java @@ -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 step = new LinkedHashMap<>(); + public void parse(List> 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 inputs = Optional.ofNullable(node.getInputs()).orElse(new ArrayList<>()); + public void buildOn(PipelineVo.PipelineJson.Nodes node) { + String nodeName = node.getName(); + Map step = new LinkedHashMap<>(); + List 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 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> 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> steps, PipelineVo.PipelineJson.Nodes node) { - String name = node.getName(); + String nodeName = node.getName(); Map step = new LinkedHashMap<>(); - if (name.startsWith("setup-java")) { - List inputs = Optional.ofNullable(node.getInputs()).orElse(new ArrayList<>()).stream().filter(e -> StringUtils.isNotBlank(e.getValue())).collect(Collectors.toList()); + List inputs = getInputs(node); - Map name2Value = inputs.stream().collect(Collectors.toMap(e -> e.getName(), e -> e.getValue(), (o, n) -> n)); + if (nodeName.startsWith("setup-java")) { + + Map name2Value = inputs.stream() + .collect(Collectors.toMap(e -> e.getName(), e -> e.getValue(), (o, n) -> n)); Map 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 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 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 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> steps, PipelineVo.PipelineJson.Nodes node) { - String fullName = node.getFull_name(); - if (Objects.equals(fullName, "shell")) { - Map step = new LinkedHashMap<>(); - step.put("name", node.getLabel()); -// step.put("run",""); - List inputs = Optional.ofNullable(node.getInputs()).orElse(new ArrayList<>()); + Map step = new LinkedHashMap<>(); + step.put("name", node.getLabel()); + List 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> steps, PipelineVo.PipelineJson.Nodes node) { + Map step = new LinkedHashMap<>(); + + List inputs = getInputs(node); + + step.put("name", node.getLabel()); + step.put("uses", node.getFull_name().trim()); + if (!inputs.isEmpty()) { + Map 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 getInputs(PipelineVo.PipelineJson.Nodes node) { + return Optional.ofNullable(node.getInputs()).orElse(new ArrayList<>()) + .stream() + .filter(e -> StringUtils.isNotBlank(e.getValue())).collect(Collectors.toList()); } } diff --git a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/pipeline/service/impl/PmsCiPipelinesServiceImpl.java b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/pipeline/service/impl/PmsCiPipelinesServiceImpl.java index 4f7ada59b..d48bfbf49 100644 --- a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/pipeline/service/impl/PmsCiPipelinesServiceImpl.java +++ b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/pipeline/service/impl/PmsCiPipelinesServiceImpl.java @@ -378,7 +378,15 @@ public class PmsCiPipelinesServiceImpl implements IPmsCiPipelinesService { try { PipelineVo vo = JSON.parseObject(JSON.toJSONString(pmsCiPipelineBuildInputVo), PipelineVo.class); - NodeActionVo actionVo = getNodeActionVo(vo); + List nodes = vo.getPipelineJson().getNodes(); + String pipelineName = vo.getPipelineName(); + NodeActionVo actionVo = new NodeActionVo(); + actionVo.buildName(pipelineName); + nodes.forEach(s -> { + List> 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 nodes = vo.getPipelineJson().getNodes(); - String pipelineName = vo.getPipelineName(); - NodeActionVo actionVo = new NodeActionVo(); - actionVo.buildName(pipelineName); - AtomicInteger i = new AtomicInteger(); - nodes.forEach(s -> { - List> 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);