[core] Validate YAML config files. Resolves #184

This commit is contained in:
Andres Almiray
2021-05-31 00:33:11 +02:00
parent b8533c4f2e
commit 1ebbe0c592
7 changed files with 63 additions and 0 deletions

View File

@@ -43,6 +43,11 @@ public class JsonJReleaserConfigParser implements JReleaserConfigParser {
return configFile.getFileName().toString().endsWith(".json");
}
@Override
public void validate(Path configFile) throws IOException {
// noop
}
@Override
public JReleaserModel parse(InputStream inputStream) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();

View File

@@ -43,6 +43,11 @@ public class TomlJReleaserConfigParser implements JReleaserConfigParser {
return configFile.getFileName().toString().endsWith(".toml");
}
@Override
public void validate(Path configFile) throws IOException {
// noop
}
@Override
public JReleaserModel parse(InputStream inputStream) throws IOException {
TomlMapper mapper = TomlMapper.builder().build();

View File

@@ -20,6 +20,10 @@ dependencies {
api "com.fasterxml.jackson.core:jackson-core:$jacksonVersion"
api "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"
api "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:$jacksonVersion"
api("com.github.sbaudoin:yamllint:1.3.1") {
exclude group: 'commons-cli', module: 'commons-cli'
exclude group: 'com.google.code.findbugs', module: 'jsr305'
}
compileOnly "org.kordamp.jipsy:jipsy-annotations:${jipsyVersion}"
annotationProcessor "org.kordamp.jipsy:jipsy-processor:${jipsyVersion}"

View File

@@ -18,6 +18,11 @@
package org.jreleaser.config.yaml;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import com.github.sbaudoin.yamllint.Format;
import com.github.sbaudoin.yamllint.LintProblem;
import com.github.sbaudoin.yamllint.Linter;
import com.github.sbaudoin.yamllint.YamlLintConfig;
import com.github.sbaudoin.yamllint.YamlLintConfigException;
import org.jreleaser.config.JReleaserConfigParser;
import org.jreleaser.model.JReleaserModel;
import org.kordamp.jipsy.annotations.ServiceProviderFor;
@@ -25,14 +30,27 @@ import org.kordamp.jipsy.annotations.ServiceProviderFor;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import static java.lang.System.lineSeparator;
import static java.util.Arrays.asList;
/**
* @author Andres Almiray
* @since 0.1.0
*/
@ServiceProviderFor(JReleaserConfigParser.class)
public class YamlJReleaserConfigParser implements JReleaserConfigParser {
private static final String YAML_LINT_CONFIG = String.join(lineSeparator(), asList(
"---",
"rules:",
" indentation:",
" spaces: consistent",
" check-multi-line-strings: true",
" indent-sequences: true",
" comments-indentation: {}")) + lineSeparator();
@Override
public String getPreferredFileExtension() {
return "yml";
@@ -44,6 +62,24 @@ public class YamlJReleaserConfigParser implements JReleaserConfigParser {
return fileName.endsWith(".yml") || fileName.endsWith(".yaml");
}
@Override
public void validate(Path configFile) throws IOException {
YamlLintConfig config = null;
try {
config = new YamlLintConfig(YAML_LINT_CONFIG);
} catch (YamlLintConfigException e) {
return;
}
List<LintProblem> problems = Linter.run(config, configFile.toFile());
if (!problems.isEmpty()) {
throw new IOException(Format.format(configFile.toAbsolutePath().toString(),
problems,
Format.OutputFormat.AUTO));
}
}
@Override
public JReleaserModel parse(InputStream inputStream) throws IOException {
YAMLMapper mapper = YAMLMapper.builder().build();

View File

@@ -36,6 +36,11 @@ public class JReleaserConfigLoader {
for (JReleaserConfigParser parser : parsers) {
if (parser.supports(configFile)) {
try {
parser.validate(configFile);
} catch (IOException e) {
throw new JReleaserException("Invalid config file. " + configFile, e);
}
try (InputStream inputStream = configFile.toUri().toURL().openStream()) {
return parser.parse(inputStream);
} catch (IOException e) {

View File

@@ -47,6 +47,13 @@ public interface JReleaserConfigParser {
*/
boolean supports(Path configFile);
/**
* Checks the contents of the config file for syntax compliance.
*
* @param configFile the configuration file to inspect
*/
void validate(Path configFile) throws IOException;
/**
* Reads and parses external configuration into a {@code JReleaserModel} instance.
*

View File

@@ -52,6 +52,7 @@ wiremockVersion = 2.28.0
slf4jVersion = 1.7.30
snakeYamlVersion = 1.27
twitter4jVersion = 4.0.7
yamllintVersion = 1.3.1
ztexecVersion = 1.12
org.gradle.daemon = true