|
| 1 | +package com.flowci.yaml.business.impl; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; |
| 6 | +import com.flowci.common.validator.ValidName; |
| 7 | +import com.flowci.yaml.business.ParseYamlV2; |
| 8 | +import com.flowci.yaml.exception.InvalidYamlException; |
| 9 | +import com.flowci.yaml.model.FlowV2; |
| 10 | +import com.flowci.yaml.model.StepV2; |
| 11 | +import lombok.extern.slf4j.Slf4j; |
| 12 | +import org.springframework.stereotype.Component; |
| 13 | +import org.springframework.util.CollectionUtils; |
| 14 | + |
| 15 | +import java.util.HashSet; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +import static java.lang.String.format; |
| 19 | +import static org.springframework.util.CollectionUtils.isEmpty; |
| 20 | +import static org.springframework.util.StringUtils.hasText; |
| 21 | + |
| 22 | +@Slf4j |
| 23 | +@Component |
| 24 | +public class ParseYamlV2Impl implements ParseYamlV2 { |
| 25 | + |
| 26 | + private static final ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory()); |
| 27 | + private static final ValidName.NameValidator nameValidator = new ValidName.NameValidator(); |
| 28 | + |
| 29 | + static { |
| 30 | + objectMapper.findAndRegisterModules(); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public FlowV2 invoke(String yaml) { |
| 35 | + try { |
| 36 | + var flowV2 = objectMapper.readValue(yaml, FlowV2.class); |
| 37 | + for (var step : flowV2.getSteps()) { |
| 38 | + step.setParent(flowV2); |
| 39 | + } |
| 40 | + return validateYaml(flowV2); |
| 41 | + } catch (JsonProcessingException e) { |
| 42 | + log.error("invalid YAML configuration", e); |
| 43 | + throw new InvalidYamlException("invalid YAML configuration"); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + private FlowV2 validateYaml(FlowV2 flowV2) { |
| 48 | + var steps = flowV2.getSteps(); |
| 49 | + validateSteps(steps); |
| 50 | + |
| 51 | + return flowV2; |
| 52 | + } |
| 53 | + |
| 54 | + private void validateSteps(List<StepV2> steps) { |
| 55 | + if (CollectionUtils.isEmpty(steps)) { |
| 56 | + throw new InvalidYamlException("at least one step is required"); |
| 57 | + } |
| 58 | + |
| 59 | + var stepNameSet = new HashSet<>(steps.size()); |
| 60 | + for (var step : steps) { |
| 61 | + if (!stepNameSet.add(step.getName())) { |
| 62 | + throw new InvalidYamlException(format("step name '%s' already exists", step.getName())); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + for (var step : steps) { |
| 67 | + if (!nameValidator.isValid(step.getName(), null)) { |
| 68 | + throw new InvalidYamlException(format("step name '%s' is invalid", step.getName())); |
| 69 | + } |
| 70 | + |
| 71 | + if (!isEmpty(step.getDependsOn())) { |
| 72 | + for (var dependsOn : step.getDependsOn()) { |
| 73 | + if (!stepNameSet.contains(dependsOn)) { |
| 74 | + throw new InvalidYamlException(format("depends on '%s' is not found", dependsOn)); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + validateCommands(step); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private void validateCommands(StepV2 step) { |
| 84 | + var commands = step.getCommands(); |
| 85 | + |
| 86 | + if (CollectionUtils.isEmpty(commands)) { |
| 87 | + throw new InvalidYamlException(format("at least one command under step '%s' is required", step.getName())); |
| 88 | + } |
| 89 | + |
| 90 | + for (var command : commands) { |
| 91 | + if (!hasText(command.getBash()) || !hasText(command.getPwsh())) { |
| 92 | + throw new InvalidYamlException("bash or powershell is required"); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments