Skip to content

Commit

Permalink
Add validation of config DSL.
Browse files Browse the repository at this point in the history
ketan committed Sep 14, 2018

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 9bae9ba commit 883daa0
Showing 43 changed files with 442 additions and 64 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# v0.4.0

## Improved

- Added validation of generated DSL.

# v0.3.0

## Changed
38 changes: 21 additions & 17 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -37,27 +37,31 @@ plugins {
apply plugin: "io.codearte.nexus-staging"

project.ext.versions = [
ant : '1.10.5',
assertj : '3.10.0',
commonsLang : '3.7',
gocdPluginApi : '18.6.0',
groovy : '2.4.15',
groovySandbox : '1.19',
gson : '2.8.5',
guava : '26.0-jre',
hamcrest : '1.3',
jcommander : '1.72',
jsonAssert : '1.30.0',
junit4 : '4.12',
junit5 : '5.2.0',
lombok : '1.18.2',
mockito : '2.21.0',
scriptSecurity: '1.41',
ant : '1.10.5',
assertj : '3.10.0',
commonsLang : '3.7',
gocdPluginApi : '18.6.0',
groovy : '2.4.15',
groovySandbox : '1.19',
gson : '2.8.5',
guava : '26.0-jre',
hamcrest : '1.3',
hibernateValidator: '6.0.13.Final',
javaxEL : '3.0.0',
javaxELImpl : '2.2',
jcommander : '1.72',
jsonAssert : '1.30.0',
junit4 : '4.12',
junit5 : '5.2.0',
lombok : '1.18.2',
mockito : '2.21.0',
scriptSecurity : '1.41',
validationApi : '2.0.1.Final',
]

allprojects {
group = 'com.github.ketan'
project.ext.pluginVersion = '0.3.0'
project.ext.pluginVersion = '0.4.0'
project.ext.gitRevision = Git.open(rootProject.projectDir).log().setMaxCount(1).call().first().name()
project.ext.distVersion = Git.open(rootProject.projectDir).log().call().size()
project.ext.fullVersion = project.distVersion ? "${project.pluginVersion}-${project.distVersion}" : project.pluginVersion
4 changes: 3 additions & 1 deletion cli/build.gradle
Original file line number Diff line number Diff line change
@@ -14,10 +14,12 @@
* limitations under the License.
*/


dependencies {
compile project(':dsl')
compile project(':sandbox')
compile group: 'com.beust', name: 'jcommander', version: project.versions.jcommander
compile group: 'org.codehaus.groovy', name: 'groovy-all', version: project.versions.groovy
compile group: 'org.hibernate.validator', name: 'hibernate-validator', version: project.versions.hibernateValidator
compile group: 'org.glassfish', name: 'javax.el', version: project.versions.javaxEL
compile group: 'org.glassfish.web', name: 'el-impl', version: project.versions.javaxELImpl
}
Original file line number Diff line number Diff line change
@@ -22,8 +22,14 @@
import com.beust.jcommander.JCommander;
import com.beust.jcommander.ParameterException;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.io.File;
import java.io.IOException;
import java.util.Set;
import java.util.function.Consumer;

public class Main {

@@ -45,10 +51,19 @@ private void run() {
for (File file : args.files) {
try {
System.out.print("Parsing file " + file + ".");
Object maybeConfig = getRunner().runScript(file.getPath());
java.lang.Object maybeConfig = getRunner().runScript(file.getPath());
if (maybeConfig instanceof GoCD) {
System.out.print(" Ok!");
GoCD configFromFile = (GoCD) maybeConfig;

validate(configFromFile, violations -> {
System.out.println("Found " + violations.size() + " validation errors!");
for (ConstraintViolation<Object> violation : violations) {
System.out.println(" - " + violation.getPropertyPath() + " " + violation.getMessage());
}
System.exit(1);
});

System.out.print(" Found environments: " + configFromFile.getEnvironments().getNames() + ".");
System.out.print(" Found pipelines: " + configFromFile.getPipelines().getNames() + ".");
System.out.println();
@@ -67,6 +82,15 @@ private void run() {
}
}

public static void validate(Object configFromFile, Consumer<Set<ConstraintViolation<Object>>> errorHandler) {
ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator validator = validatorFactory.getValidator();
Set<ConstraintViolation<Object>> violations = validator.validate(configFromFile);
if (!violations.isEmpty()) {
errorHandler.accept(violations);
}
}

private GroovyScriptRunner getRunner() throws IOException {
if (engine == null) {
engine = new GroovyScriptRunner(".", Pipeline.class.getPackage().getName());
7 changes: 7 additions & 0 deletions dsl/build.gradle
Original file line number Diff line number Diff line change
@@ -26,12 +26,15 @@ configurations {
dependencies {
doclet project(':doclet')
annotationProcessor group: 'org.projectlombok', name: 'lombok', version: project.versions.lombok
annotationProcessor group: 'org.hibernate.validator', name: 'hibernate-validator-annotation-processor', version: project.versions.hibernateValidator

compileOnly group: 'org.projectlombok', name: 'lombok', version: project.versions.lombok

compile group: 'org.codehaus.groovy', name: 'groovy-all', version: project.versions.groovy

compile group: 'com.google.code.gson', name: 'gson', version: project.versions.gson
compile group: 'org.apache.commons', name: 'commons-lang3', version: project.versions.commonsLang
compile group: 'javax.validation', name: 'validation-api', version: project.versions.validationApi

testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: project.versions.junit5
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: project.versions.junit5
@@ -40,6 +43,10 @@ dependencies {
testCompile project(':sandbox')
testCompile group: 'org.assertj', name: 'assertj-core', version: project.versions.assertj
testCompile group: 'net.javacrumbs.json-unit', name: 'json-unit-fluent', version: project.versions.jsonAssert

testCompile group: 'org.hibernate.validator', name: 'hibernate-validator', version: project.versions.hibernateValidator
testCompile group: 'org.glassfish', name: 'javax.el', version: project.versions.javaxEL
testCompile group: 'org.glassfish.web', name: 'el-impl', version: project.versions.javaxELImpl
}

extraArchive {
Original file line number Diff line number Diff line change
@@ -23,6 +23,8 @@
import lombok.Getter;
import lombok.Setter;

import javax.validation.constraints.NotEmpty;

@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
@@ -32,6 +34,7 @@ public abstract class AbstractArtifact<T extends AbstractArtifact> extends Node<
@SerializedName("type")
@Getter(value = AccessLevel.NONE)
@Setter(value = AccessLevel.NONE)
@NotEmpty
protected final String type;

AbstractArtifact(String type) {
Original file line number Diff line number Diff line change
@@ -16,13 +16,14 @@

package cd.go.contrib.plugins.configrepo.groovy.dsl;

import cd.go.contrib.plugins.configrepo.groovy.dsl.util.OneOfStrings;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

import javax.validation.constraints.NotEmpty;
import java.util.Arrays;
import java.util.List;

@@ -45,9 +46,10 @@ public class Approval extends Node<Approval> {
/**
* The type of the approval, either {@code 'manual'} or {@code 'success'}
*/
@Setter(AccessLevel.NONE)
@Expose
@SerializedName("type")
@NotEmpty
@OneOfStrings(value = {"success", "manual"})
private String type = "success";

/**
@@ -64,12 +66,4 @@ public class Approval extends Node<Approval> {
@SerializedName("users")
private List<String> users;

public void setType(String newValue) {
if (APPROVAL_TYPES.contains(newValue.toLowerCase())) {
this.type = newValue.toLowerCase();
} else {
throw new IllegalArgumentException("Illegal value for approval type: " + newValue);
}
}

}
Original file line number Diff line number Diff line change
@@ -26,6 +26,8 @@
import lombok.Getter;
import lombok.Setter;

import javax.validation.constraints.NotEmpty;

import static groovy.lang.Closure.DELEGATE_ONLY;

/**
@@ -51,6 +53,7 @@ public class BuiltInArtifact extends AbstractArtifact<BuiltInArtifact> {
*/
@Expose
@SerializedName("source")
@NotEmpty
private String source;

/**
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@
import lombok.Getter;
import lombok.Setter;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
@@ -34,6 +35,7 @@ abstract class CollectionNode<T extends Node> extends Node<T> {

@Getter(value = AccessLevel.NONE)
@Setter(value = AccessLevel.NONE)
@Valid
private final List<T> elements = new ArrayList<>();

public T create(Supplier<T> callable) {
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@
import lombok.Getter;
import lombok.Setter;

import javax.validation.Valid;
import java.util.List;

import static groovy.lang.Closure.DELEGATE_ONLY;
@@ -52,11 +53,11 @@ public class ConfigRepoMaterial extends Material<ConfigRepoMaterial> {
* <p>
* {@includeCode scm.filter.groovy }
*/

@Getter(value = AccessLevel.NONE)
@Setter(value = AccessLevel.NONE)
@Expose
@SerializedName("filter")
@Valid
private Filter filter;

ConfigRepoMaterial() {
Original file line number Diff line number Diff line change
@@ -26,6 +26,8 @@
import lombok.Getter;
import lombok.Setter;

import javax.validation.constraints.NotEmpty;

import static groovy.lang.Closure.DELEGATE_ONLY;

@Getter
@@ -35,10 +37,12 @@ public class DependencyMaterial extends Material<DependencyMaterial> {

@Expose
@SerializedName("pipeline")
@NotEmpty
private String pipeline;

@Expose
@SerializedName("stage")
@NotEmpty
private String stage;

DependencyMaterial() {
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@
import lombok.Getter;
import lombok.Setter;

import javax.validation.constraints.NotEmpty;
import java.util.LinkedList;
import java.util.List;

@@ -58,6 +59,7 @@ public class ExecTask extends Task<ExecTask> {
/**
* The command line to be executed.
*/
@NotEmpty
private List<String> commandLine = new LinkedList<>();

public ExecTask() {
@@ -72,6 +74,7 @@ public ExecTask(@DelegatesTo(value = ExecTask.class, strategy = DELEGATE_ONLY) @
@Override
public JsonObject toJson() {
JsonObject jsonObject = (JsonObject) super.toJson();

jsonObject.addProperty("command", commandLine.get(0));

if (commandLine.size() > 1) {
Original file line number Diff line number Diff line change
@@ -27,6 +27,8 @@
import lombok.Getter;
import lombok.Setter;

import javax.validation.constraints.NotEmpty;

import static groovy.lang.Closure.DELEGATE_ONLY;

@Getter
@@ -42,22 +44,27 @@ public class FetchArtifactTask extends Task<FetchArtifactTask> {

@Expose
@SerializedName("pipeline")
@NotEmpty
private String pipeline;

@Expose
@SerializedName("stage")
@NotEmpty
private String stage;

@Expose
@SerializedName("job")
@NotEmpty
private String job;

@Expose
@SerializedName("source")
@NotEmpty
private String source;

@Expose
@SerializedName("destination")
@NotEmpty
private String destination;

public FetchArtifactTask() {
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@
import lombok.Getter;
import lombok.Setter;

import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
@@ -37,6 +38,7 @@ public class Filter {

private final boolean isWhitelist;

@NotEmpty
private final List<String> items;

public Filter(List<String> items) {
Original file line number Diff line number Diff line change
@@ -26,6 +26,8 @@
import lombok.Getter;
import lombok.Setter;

import javax.validation.constraints.NotEmpty;

import static groovy.lang.Closure.DELEGATE_ONLY;

/**
@@ -46,6 +48,7 @@ public class GitMaterial extends ScmMaterial<GitMaterial> {
*/
@Expose
@SerializedName("url")
@NotEmpty
private String url;

/**
Loading

0 comments on commit 883daa0

Please sign in to comment.