Skip to content

Commit

Permalink
Add severity option for validators (#131)
Browse files Browse the repository at this point in the history
Adds a severity option to the build and validate tasks to allow setting a minimum validation event severity
  • Loading branch information
hpmellema committed Mar 18, 2024
1 parent 6f1967d commit 8a4d35b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ Provider<FileCollection> getCliExecutionClasspath() {
@Optional
public abstract Property<ShowStacktrace> getShowStackTrace();

/**
* Set the minimum reported validation severity.
*
* <p>This value should be one of: NOTE, WARNING [default], DANGER, ERROR.
*
* @return minimum validator severity
*/
@Input
@Optional
public abstract Property<String> getSeverity();

@Internal
WorkerExecutor getExecutor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.gradle.api.tasks.TaskAction;
import software.amazon.smithy.cli.BuildParameterBuilder;
import software.amazon.smithy.gradle.SmithyUtils;
import software.amazon.smithy.model.validation.Severity;

/**
* Executes the Smithy CLI {@code build} command.
Expand All @@ -39,6 +40,7 @@ public SmithyBuildTask(ObjectFactory objectFactory) {
super(objectFactory);

getSourceProjection().convention("source");
getSeverity().convention(Severity.WARNING.toString());
getOutputDir().convention(SmithyUtils.getProjectionOutputDirProperty(getProject()));
}

Expand Down Expand Up @@ -149,6 +151,11 @@ public void execute() {
// Add extra configuration options for build command
List<String> extraArgs = new ArrayList<>();
configureLoggingOptions(extraArgs);

// Add validator severity option if it exists
extraArgs.add("--severity");
extraArgs.add(getSeverity().get());

builder.addExtraArgs(extraArgs.toArray(new String[0]));

BuildParameterBuilder.Result result = builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package software.amazon.smithy.gradle.tasks;

import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import org.gradle.api.file.FileCollection;
import org.gradle.api.model.ObjectFactory;
Expand All @@ -15,7 +17,7 @@
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.TaskAction;
import software.amazon.smithy.utils.ListUtils;
import software.amazon.smithy.model.validation.Severity;


/**
Expand All @@ -35,6 +37,7 @@ public SmithyValidateTask(ObjectFactory objectFactory) {
super(objectFactory);
getAllowUnknownTraits().convention(false);
getDisableModelDiscovery().convention(false);
getSeverity().convention(Severity.ERROR.toString());
setDescription(DESCRIPTION);
}

Expand Down Expand Up @@ -67,6 +70,17 @@ public SmithyValidateTask(ObjectFactory objectFactory) {
@Optional
public abstract Property<Boolean> getDisableModelDiscovery();

/**
* Set the minimum reported validation severity.
*
* <p>This value should be one of: NOTE, WARNING, DANGER, ERROR [default].
*
* @return minimum validator severity
*/
@Input
@Optional
public abstract Property<String> getSeverity();

/**
* Gets the classpath to use when executing the Smithy CLI.
*
Expand All @@ -86,8 +100,13 @@ Provider<FileCollection> getCliExecutionClasspath() {
public void execute() {
writeHeading("Running smithy validate");

// Add validator severity settings
List<String> extraArgs = new ArrayList<>();
extraArgs.add("--severity");
extraArgs.add(getSeverity().get());

// Set models to an empty collection so source models are not included in validation path.
executeCliProcess("validate", ListUtils.of(),
executeCliProcess("validate", extraArgs,
getJarToValidate().get(),
getDisableModelDiscovery().get()
);
Expand Down

0 comments on commit 8a4d35b

Please sign in to comment.