Search in sources :

Example 96 with ValidationException

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.");
}
Also used : MigrationReference(com.google.copybara.Info.MigrationReference) ConfigValidator(com.google.copybara.config.ConfigValidator) Iterables(com.google.common.collect.Iterables) Migration(com.google.copybara.config.Migration) InfoFinishedEvent(com.google.copybara.monitor.EventMonitor.InfoFinishedEvent) RepoException(com.google.copybara.exception.RepoException) ValidationException(com.google.copybara.exception.ValidationException) MessageType(com.google.copybara.util.console.Message.MessageType) Console(com.google.copybara.util.console.Console) IOException(java.io.IOException) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Message(com.google.copybara.util.console.Message) Config(com.google.copybara.config.Config) Preconditions(com.google.common.base.Preconditions) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) ValidationException(com.google.copybara.exception.ValidationException) Message(com.google.copybara.util.console.Message) Config(com.google.copybara.config.Config) Console(com.google.copybara.util.console.Console)

Example 97 with ValidationException

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.*");
}
Also used : ValidationException(com.google.copybara.exception.ValidationException) Test(org.junit.Test)

Example 98 with ValidationException

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.");
                    }
                }
            }
        }
    }
}
Also used : Writer(com.google.copybara.Destination.Writer) ConfigFile(com.google.copybara.config.ConfigFile) MigrationReference(com.google.copybara.Info.MigrationReference) ValidationException.checkCondition(com.google.copybara.exception.ValidationException.checkCondition) RepoException(com.google.copybara.exception.RepoException) FinishHookContext(com.google.copybara.feedback.FinishHookContext) Callable(java.util.concurrent.Callable) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) ChangesResponse(com.google.copybara.Origin.Reader.ChangesResponse) TokenType(com.google.copybara.templatetoken.Token.TokenType) ImmutableList(com.google.common.collect.ImmutableList) Path(java.nio.file.Path) Profiler(com.google.copybara.profiler.Profiler) Nullable(javax.annotation.Nullable) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) CHANGE_REQUEST_FROM_SOT(com.google.copybara.WorkflowMode.CHANGE_REQUEST_FROM_SOT) Reader(com.google.copybara.Origin.Reader) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) Identity(com.google.copybara.util.Identity) ImmutableMap(com.google.common.collect.ImmutableMap) Migration(com.google.copybara.config.Migration) Token(com.google.copybara.templatetoken.Token) Action(com.google.copybara.action.Action) MoreObjects(com.google.common.base.MoreObjects) ValidationException(com.google.copybara.exception.ValidationException) SkylarkConsole(com.google.copybara.transform.SkylarkConsole) Set(java.util.Set) LazyResourceLoader.memoized(com.google.copybara.LazyResourceLoader.memoized) Console(com.google.copybara.util.console.Console) IOException(java.io.IOException) EventMonitors(com.google.copybara.monitor.EventMonitor.EventMonitors) DestinationStatus(com.google.copybara.Destination.DestinationStatus) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) CHANGE_REQUEST(com.google.copybara.WorkflowMode.CHANGE_REQUEST) Authoring(com.google.copybara.authoring.Authoring) Glob(com.google.copybara.util.Glob) List(java.util.List) ChangeMigrationFinishedEvent(com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent) Paths(java.nio.file.Paths) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) CommandLineException(com.google.copybara.exception.CommandLineException) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) ImmutableList(com.google.common.collect.ImmutableList) CommandLineException(com.google.copybara.exception.CommandLineException) Nullable(javax.annotation.Nullable)

Example 99 with ValidationException

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);
    }
}
Also used : ValidationException(com.google.copybara.exception.ValidationException) StarlarkThread(net.starlark.java.eval.StarlarkThread) Mutability(net.starlark.java.eval.Mutability) EvalException(net.starlark.java.eval.EvalException) RepoException(com.google.copybara.exception.RepoException)

Example 100 with ValidationException

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);
    }
}
Also used : BadExitStatusWithOutputException(com.google.copybara.util.BadExitStatusWithOutputException) ValidationException(com.google.copybara.exception.ValidationException) Command(com.google.copybara.shell.Command) CommandOutputWithStatus(com.google.copybara.util.CommandOutputWithStatus) Matcher(java.util.regex.Matcher) ImmutableList(com.google.common.collect.ImmutableList) CommandException(com.google.copybara.shell.CommandException)

Aggregations

ValidationException (com.google.copybara.exception.ValidationException)178 Test (org.junit.Test)125 Path (java.nio.file.Path)33 RepoException (com.google.copybara.exception.RepoException)29 NonReversibleValidationException (com.google.copybara.exception.NonReversibleValidationException)26 ImmutableList (com.google.common.collect.ImmutableList)21 IOException (java.io.IOException)19 Console (com.google.copybara.util.console.Console)16 EmptyChangeException (com.google.copybara.exception.EmptyChangeException)14 DummyRevision (com.google.copybara.testing.DummyRevision)14 Glob (com.google.copybara.util.Glob)14 ProfilerTask (com.google.copybara.profiler.Profiler.ProfilerTask)13 Nullable (javax.annotation.Nullable)13 Migration (com.google.copybara.config.Migration)11 TestingConsole (com.google.copybara.util.console.testing.TestingConsole)11 Iterables (com.google.common.collect.Iterables)10 Change (com.google.copybara.Change)10 CannotResolveRevisionException (com.google.copybara.exception.CannotResolveRevisionException)10 Collectors (java.util.stream.Collectors)10 WriterContext (com.google.copybara.WriterContext)9