Skip to content

Commit

Permalink
Simplify and improve changelog YAML file validation (#89524)
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-vieira committed Aug 23, 2022
1 parent 74d694e commit 3a78f80
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 323 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,16 @@ public void validate(InputChanges inputChanges) throws IOException {
errors.values().stream().flatMap(Collection::stream).forEach(printWriter::println);
}
StringBuilder sb = new StringBuilder();
sb.append("Error validating JSON. See the report at: ");
sb.append("Verification failed. See the report at: ");
sb.append(getReport().toURI().toASCIIString());
sb.append(System.lineSeparator());
sb.append(
String.format("Verification failed: %d files contained %d violations", errors.keySet().size(), errors.values().size())
String.format(
"Error validating %s: %d files contained %d violations",
getFileType(),
errors.keySet().size(),
errors.values().size()
)
);
throw new JsonSchemaException(sb.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
import java.util.stream.Collectors;

/**
* This class models the contents of a changelog YAML file. We validate it using a
* JSON Schema, as well as some programmatic checks in {@link ValidateChangelogEntryTask}.
* This class models the contents of a changelog YAML file. We validate it using a JSON Schema.
* <ul>
* <li><code>buildSrc/src/main/resources/changelog-schema.json</code></li>
* <li><a href="https://json-schema.org/understanding-json-schema/">Understanding JSON Schema</a></li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.gradle.api.file.FileTree;
import org.gradle.api.file.ProjectLayout;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.api.tasks.util.PatternSet;

import java.io.File;
Expand Down Expand Up @@ -52,23 +51,15 @@ public void apply(Project project) {
.getAsFileTree()
.matching(new PatternSet().include("**/*.yml", "**/*.yaml"));

final Provider<ValidateYamlAgainstSchemaTask> validateChangelogsAgainstYamlTask = project.getTasks()
.register("validateChangelogsAgainstSchema", ValidateYamlAgainstSchemaTask.class, task -> {
final Provider<ValidateYamlAgainstSchemaTask> validateChangelogsTask = project.getTasks()
.register("validateChangelogs", ValidateYamlAgainstSchemaTask.class, task -> {
task.setGroup("Documentation");
task.setDescription("Validate that the changelog YAML files comply with the changelog schema");
task.setInputFiles(yamlFiles);
task.setJsonSchema(new File(project.getRootDir(), RESOURCES + "changelog-schema.json"));
task.setReport(new File(project.getBuildDir(), "reports/validateYaml.txt"));
});

final TaskProvider<ValidateChangelogEntryTask> validateChangelogsTask = project.getTasks()
.register("validateChangelogs", ValidateChangelogEntryTask.class, task -> {
task.setGroup("Documentation");
task.setDescription("Validate that all changelog YAML files are well-formed");
task.setChangelogs(yamlFiles);
task.dependsOn(validateChangelogsAgainstYamlTask);
});

final Function<Boolean, Action<GenerateReleaseNotesTask>> configureGenerateTask = shouldConfigureYamlFiles -> task -> {
task.setGroup("Documentation");
if (shouldConfigureYamlFiles) {
Expand Down

This file was deleted.

86 changes: 85 additions & 1 deletion build-tools-internal/src/main/resources/changelog-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,82 @@
"required": [
"type",
"summary"
]
],
"anyOf": [
{
"$comment": "PR number and area fields not required for known-issue type",
"if": {
"not": {
"properties": {
"type": {
"const": "known-issue"
}
}
}
},
"then": {
"required": [
"pr",
"area"
]
}
},
{
"$comment": "PR number and area fields not required for security type",
"if": {
"not": {
"properties": {
"type": {
"const": "security"
}
}
}
},
"then": {
"required": [
"pr",
"area"
]
}
}
],
"allOf": [
{
"if": {
"properties": {
"type": {
"const": "breaking"
}
}
},
"then": {
"required": ["breaking"]
}
},
{
"if": {
"properties": {
"type": {
"const": "breaking-java"
}
}
},
"then": {
"required": ["breaking"]
}
}
],
"if": {
"properties": {
"type": {
"const": "deprecation"
}
}
},
"then": {
"required": ["deprecation"]
},
"additionalProperties": false
},
"Highlight": {
"properties": {
Expand All @@ -133,13 +208,17 @@
},
"body": {
"type": "string",
"pattern": "(?s)^((?!```).)*$",
"minLength": 1
}
},
"required": [
"title",
"body"
],
"message": {
"pattern": "Changelog uses a triple-backtick (```) in a details section, but it must be formatted as a Asciidoc code block. For example:\n\n [source,yaml]\n ----\n {\n \"metrics.time\" : 10,\n \"metrics.time.min\" : 1,\n \"metrics.time.max\" : 500\n }\n ----\n"
},
"additionalProperties": false
},
"CompatibilityChange": {
Expand All @@ -153,10 +232,12 @@
},
"details": {
"type": "string",
"pattern": "(?s)^((?!```).)*$",
"minLength": 1
},
"impact": {
"type": "string",
"pattern": "(?s)^((?!```).)*$",
"minLength": 1
},
"notable": {
Expand All @@ -172,6 +253,9 @@
"details",
"impact"
],
"message": {
"pattern": "Changelog uses a triple-backtick (```) in a details section, but it must be formatted as a Asciidoc code block. For example:\n\n [source,yaml]\n ----\n {\n \"metrics.time\" : 10,\n \"metrics.time.min\" : 1,\n \"metrics.time.max\" : 500\n }\n ----\n"
},
"additionalProperties": false
},
"compatibilityChangeArea": {
Expand Down

0 comments on commit 3a78f80

Please sign in to comment.