use of com.google.copybara.exception.ValidationException in project copybara by google.
the class Copybara method loadConfig.
protected Config loadConfig(Options options, ConfigLoader<?> configLoader, String migrationName) throws IOException, ValidationException {
GeneralOptions generalOptions = options.get(GeneralOptions.class);
Console console = generalOptions.console();
Config config = configLoader.loadConfig(options, console);
console.progress("Validating configuration");
List<Message> validationMessages = validateConfig(config, migrationName);
List<Message> errors = validationMessages.stream().filter(message -> message.getType() == MessageType.ERROR).collect(Collectors.toList());
if (errors.isEmpty()) {
return config;
}
errors.forEach(error -> error.printTo(console));
console.error("Configuration is invalid.");
throw new ValidationException("Error validating configuration: Configuration is invalid.");
}
use of com.google.copybara.exception.ValidationException in project copybara by google.
the class WorkflowTest method testIterativeValidationException.
@Test
public void testIterativeValidationException() throws Exception {
assertThat(checkIterativeModeWithError(new ValidationException("Your change is wrong!"))).hasMessage("Your change is wrong!");
console().assertThat().onceInLog(MessageType.ERROR, "Migration of origin revision '2' failed with error: Your change is wrong.*");
}
use of com.google.copybara.exception.ValidationException in project copybara by google.
the class Workflow method run.
@Override
public void run(Path workdir, ImmutableList<String> sourceRefs) throws RepoException, IOException, ValidationException {
if (sourceRefs.size() > 1) {
throw new CommandLineException(String.format("Workflow does not support multiple source_ref arguments yet: %s", ImmutableList.copyOf(sourceRefs)));
}
@Nullable String sourceRef = sourceRefs.size() == 1 ? sourceRefs.get(0) : null;
validateFlags();
try (ProfilerTask ignore = profiler().start("run/" + name)) {
console.progress("Getting last revision: " + "Resolving " + ((sourceRef == null) ? "origin reference" : sourceRef));
O resolvedRef = generalOptions.repoTask("origin.resolve_source_ref", () -> origin.resolve(sourceRef));
logger.log(Level.INFO, String.format("Running Copybara for workflow '%s' and ref '%s': %s", name, resolvedRef.asString(), this.toString()));
logger.log(Level.INFO, String.format("Using working directory : %s", workdir));
ImmutableList.Builder<DestinationEffect> allEffects = ImmutableList.builder();
WorkflowRunHelper<O, D> helper = newRunHelper(workdir, resolvedRef, sourceRef, event -> {
allEffects.addAll(event.getDestinationEffects());
eventMonitors().dispatchEvent(m -> m.onChangeMigrationFinished(event));
});
try (ProfilerTask ignored = profiler().start(mode.toString().toLowerCase())) {
mode.run(helper);
} finally {
if (!getGeneralOptions().dryRunMode) {
try (ProfilerTask ignored = profiler().start("after_all_migration")) {
ImmutableList<DestinationEffect> effects = allEffects.build();
ImmutableList<DestinationEffect> resultEffects = runHooks(effects, getAfterAllMigrationActions(), // Only do this once for all the actions
memoized(c -> helper.getOriginReader().getFeedbackEndPoint(c)), // Only do this once for all the actions
memoized(c -> helper.getDestinationWriter().getFeedbackEndPoint(c)), resolvedRef);
if (effects.size() != resultEffects.size()) {
console.warn("Effects where created in after_all_migrations, but they are ignored.");
}
}
}
}
}
}
use of com.google.copybara.exception.ValidationException in project copybara by google.
the class StarlarkAction method run.
@Override
public <T extends SkylarkContext<T>> void run(ActionContext<T> context) throws ValidationException, RepoException {
SkylarkContext<T> actionContext = context.withParams(params);
try (Mutability mu = Mutability.create("dynamic_action")) {
StarlarkThread thread = new StarlarkThread(mu, StarlarkSemantics.DEFAULT);
thread.setPrintHandler(printHandler);
Object result = Starlark.call(thread, function, ImmutableList.of(actionContext), /*kwargs=*/
ImmutableMap.of());
context.onFinish(result, actionContext);
} catch (EvalException e) {
Throwable cause = e.getCause();
String error = String.format("Error while executing the skylark transformation %s: %s.", function.getName(), e.getMessageWithStack());
if (cause instanceof RepoException) {
throw new RepoException(error, cause);
}
throw new ValidationException(error, cause);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("This should not happen.", e);
}
}
use of com.google.copybara.exception.ValidationException in project copybara by google.
the class BuildozerOptions method run.
/**
* Runs buildozer with the given commands.
*/
void run(Console console, Path checkoutDir, Iterable<BuildozerCommand> commands) throws ValidationException, TargetNotFoundException {
List<String> args = Lists.newArrayList(buildozerBin, "-buildifier=" + buildifierOptions.buildifierBin);
// We only use -k in keep going mode because it shows less errors (http://b/69386431)
if (workflowOptions.ignoreNoop) {
args.add("-k");
}
args.add("-f");
args.add("-");
try {
Command cmd = new Command(args.toArray(new String[0]), /*environmentVariables*/
null, checkoutDir.toFile());
CommandOutputWithStatus output = generalOptions.newCommandRunner(cmd).withVerbose(generalOptions.isVerbose()).withInput(Joiner.on('\n').join(commands).getBytes(UTF_8)).execute();
if (!output.getStdout().isEmpty()) {
logger.atInfo().log("buildozer stdout: %s", output.getStdout());
}
if (!output.getStderr().isEmpty()) {
logger.atInfo().log("buildozer stderr: %s", output.getStderr());
}
} catch (BadExitStatusWithOutputException e) {
// Don't print the output for common/known errors.
if (generalOptions.isVerbose()) {
logError(console, e.getOutput());
}
if (e.getResult().getTerminationStatus().getExitCode() == 3) {
// :%java_library
throw new TargetNotFoundException(commandsMessage("Buildozer could not find a target for", commands));
}
if (e.getResult().getTerminationStatus().getExitCode() == 2) {
ImmutableList<String> errors = Splitter.on('\n').splitToList(e.getOutput().getStderr()).stream().filter(s -> !(s.isEmpty() || s.startsWith("fixed "))).collect(ImmutableList.toImmutableList());
ImmutableList.Builder<String> notFoundMsg = ImmutableList.builder();
boolean allNotFound = true;
for (String error : errors) {
Matcher matcher = targetNotFound.matcher(error);
if (matcher.matches()) {
notFoundMsg.add(String.format("Buildozer could not find a target for %s", matcher.group(1)));
} else if (error.contains("no such file or directory") || error.contains("not a directory")) {
notFoundMsg.add("Buildozer could not find build file: " + error);
} else {
allNotFound = false;
}
}
if (allNotFound) {
throw new TargetNotFoundException(Joiner.on("\n").join(notFoundMsg.build()));
}
}
// Otherwise we have already printed above.
if (!generalOptions.isVerbose()) {
logError(console, e.getOutput());
}
throw new ValidationException(String.format("%s\nCommand stderr:%s", commandsMessage("Failed to execute buildozer with args", commands), e.getOutput().getStderr()), e);
} catch (CommandException e) {
String message = String.format("Error '%s' running buildozer command: %s", e.getMessage(), e.getCommand().toDebugString());
console.error(message);
throw new ValidationException(message, e);
}
}
Aggregations