Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New release notes generator tasks #68463

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ plugins {
id 'lifecycle-base'
id 'elasticsearch.docker-support'
id 'elasticsearch.global-build-info'
id 'elasticsearch.release-tools'
id "com.diffplug.spotless" version "5.9.0" apply false
}

Expand Down
1 change: 1 addition & 0 deletions buildSrc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ dependencies {
testFixturesApi gradleTestKit()
testImplementation 'com.github.tomakehurst:wiremock-jre8-standalone:2.23.2'
testImplementation 'org.mockito:mockito-core:1.9.5'
testImplementation "org.hamcrest:hamcrest:${props.getProperty('hamcrest')}"
integTestImplementation('org.spockframework:spock-core:1.3-groovy-2.5') {
exclude module: "groovy"
}
Expand Down
40 changes: 30 additions & 10 deletions buildSrc/src/main/java/org/elasticsearch/gradle/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
*/
package org.elasticsearch.gradle;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -19,6 +22,7 @@ public final class Version implements Comparable<Version> {
private final int minor;
private final int revision;
private final int id;
private final List<String> labels;

/**
* Specifies how a version string should be parsed.
Expand All @@ -36,11 +40,15 @@ public enum Mode {
RELAXED
}

private static final Pattern pattern = Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)(-alpha\\d+|-beta\\d+|-rc\\d+)?(-SNAPSHOT)?");
private static final Pattern pattern = Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)((?:-alpha\\d+|-beta\\d+|-rc\\d+)?(?:-SNAPSHOT)?)?");

private static final Pattern relaxedPattern = Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)(-[a-zA-Z0-9_]+)*?");
private static final Pattern relaxedPattern = Pattern.compile("v?(\\d+)\\.(\\d+)\\.(\\d+)((?:-[a-zA-Z0-9_]+)*)?");

public Version(int major, int minor, int revision) {
this(major, minor, revision, List.of());
}

public Version(int major, int minor, int revision, List<String> labels) {
Objects.requireNonNull(major, "major version can't be null");
Objects.requireNonNull(minor, "minor version can't be null");
Objects.requireNonNull(revision, "revision version can't be null");
Expand All @@ -50,13 +58,8 @@ public Version(int major, int minor, int revision) {

// currently snapshot is not taken into account
this.id = major * 10000000 + minor * 100000 + revision * 1000;
}

private static int parseSuffixNumber(String substring) {
if (substring.isEmpty()) {
throw new IllegalArgumentException("Invalid suffix, must contain a number e.x. alpha2");
}
return Integer.parseInt(substring);
this.labels = new ArrayList<>(labels);
}

public static Version fromString(final String s) {
Expand All @@ -73,12 +76,25 @@ public static Version fromString(final String s, final Mode mode) {
throw new IllegalArgumentException("Invalid version format: '" + s + "'. Should be " + expected);
}

return new Version(Integer.parseInt(matcher.group(1)), parseSuffixNumber(matcher.group(2)), parseSuffixNumber(matcher.group(3)));
String labelString = matcher.group(4);
List<String> labels;
if (labelString == null || labelString.isEmpty()) {
labels = List.of();
} else {
labels = Arrays.asList(labelString.substring(1).split("-"));
}

return new Version(
Integer.parseInt(matcher.group(1)),
Integer.parseInt(matcher.group(2)),
Integer.parseInt(matcher.group(3)),
labels
);
}

@Override
public String toString() {
return String.valueOf(getMajor()) + "." + String.valueOf(getMinor()) + "." + String.valueOf(getRevision());
return getMajor() + "." + getMinor() + "." + getRevision();
}

public boolean before(Version compareTo) {
Expand Down Expand Up @@ -146,6 +162,10 @@ protected int getId() {
return id;
}

public List<String> getLabels() {
return labels;
}

@Override
public int compareTo(Version other) {
return Integer.compare(getId(), other.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
import org.gradle.api.file.FileCollection;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskAction;
import org.gradle.work.ChangeType;
import org.gradle.work.FileChange;
import org.gradle.work.Incremental;
import org.gradle.work.InputChanges;

Expand All @@ -40,8 +42,6 @@
* Incremental task to validate a set of JSON files against against a schema.
*/
public class ValidateJsonAgainstSchemaTask extends DefaultTask {

private final ObjectMapper mapper = new ObjectMapper();
private File jsonSchema;
private File report;
private FileCollection inputFiles;
Expand Down Expand Up @@ -74,28 +74,37 @@ public File getReport() {
return this.report;
}

@Internal
protected ObjectMapper getMapper() {
return new ObjectMapper();
}

@Internal
protected String getFileType() {
return "JSON";
}

@TaskAction
public void validate(InputChanges inputChanges) throws IOException {
File jsonSchemaOnDisk = getJsonSchema();
getLogger().debug("JSON schema : [{}]", jsonSchemaOnDisk.getAbsolutePath());
SchemaValidatorsConfig config = new SchemaValidatorsConfig();
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
JsonSchema jsonSchema = factory.getSchema(mapper.readTree(jsonSchemaOnDisk), config);
Map<File, Set<String>> errors = new LinkedHashMap<>();
final File jsonSchemaOnDisk = getJsonSchema();
final JsonSchema jsonSchema = buildSchemaObject(jsonSchemaOnDisk);

final Map<File, Set<String>> errors = new LinkedHashMap<>();
final ObjectMapper mapper = this.getMapper();

// incrementally evaluate input files
// validate all files and hold on to errors for a complete report if there are failures
StreamSupport.stream(inputChanges.getFileChanges(getInputFiles()).spliterator(), false)
.filter(f -> f.getChangeType() != ChangeType.REMOVED)
.forEach(fileChange -> {
File file = fileChange.getFile();
if (file.isDirectory() == false) {
// validate all files and hold on to errors for a complete report if there are failures
getLogger().debug("Validating JSON [{}]", file.getName());
try {
Set<ValidationMessage> validationMessages = jsonSchema.validate(mapper.readTree(file));
maybeLogAndCollectError(validationMessages, errors, file);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
.map(FileChange::getFile)
.filter(file -> file.isDirectory() == false)
.forEach(file -> {
getLogger().debug("Validating {} [{}]", getFileType(), file.getName());
try {
Set<ValidationMessage> validationMessages = jsonSchema.validate(mapper.readTree(file));
maybeLogAndCollectError(validationMessages, errors, file);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
if (errors.isEmpty()) {
Expand All @@ -119,9 +128,18 @@ public void validate(InputChanges inputChanges) throws IOException {
}
}

private JsonSchema buildSchemaObject(File jsonSchemaOnDisk) throws IOException {
final ObjectMapper jsonMapper = new ObjectMapper();
getLogger().debug("JSON schema : [{}]", jsonSchemaOnDisk.getAbsolutePath());
final SchemaValidatorsConfig config = new SchemaValidatorsConfig();
final JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
return factory.getSchema(jsonMapper.readTree(jsonSchemaOnDisk), config);
}

private void maybeLogAndCollectError(Set<ValidationMessage> messages, Map<File, Set<String>> errors, File file) {
final String fileType = getFileType();
for (ValidationMessage message : messages) {
getLogger().error("[validate JSON][ERROR][{}][{}]", file.getName(), message.toString());
getLogger().error("[validate {}][ERROR][{}][{}]", fileType, file.getName(), message.toString());
errors.computeIfAbsent(file, k -> new LinkedHashSet<>())
.add(String.format("%s: %s", file.getAbsolutePath(), message.toString()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.gradle.internal.precommit;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

/**
* Incremental task to validate a set of YAML files against against a schema.
*/
public class ValidateYamlAgainstSchemaTask extends ValidateJsonAgainstSchemaTask {
@Override
protected String getFileType() {
return "YAML";
}

protected ObjectMapper getMapper() {
return new ObjectMapper(new YAMLFactory());
}
}
Loading