Search in sources :

Example 1 with SkylarkConsole

use of com.google.copybara.transform.SkylarkConsole in project copybara by google.

the class Feedback method run.

@Override
public void run(Path workdir, @Nullable String sourceRef) throws RepoException, ValidationException {
    // TODO(danielromero): Handle correctly null sourceRefs
    SkylarkConsole console = new SkylarkConsole(generalOptions.console());
    Profiler profiler = generalOptions.profiler();
    try (ProfilerTask ignore = profiler.start("run/" + name)) {
        for (Action action : actions) {
            try (ProfilerTask ignore2 = profiler.start(action.getName())) {
                action.run(new FeedbackContext(origin, destination, sourceRef, console));
            }
        }
    }
    ValidationException.checkCondition(console.getErrorCount() == 0, "%d errors executing the feedback migration", console.getErrorCount());
}
Also used : SkylarkConsole(com.google.copybara.transform.SkylarkConsole) Profiler(com.google.copybara.profiler.Profiler) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask)

Example 2 with SkylarkConsole

use of com.google.copybara.transform.SkylarkConsole in project copybara by google.

the class Mirror method run.

@Override
public void run(Path workdir, ImmutableList<String> sourceRefs) throws RepoException, IOException, ValidationException {
    try (ProfilerTask ignore = generalOptions.profiler().start("run/" + name)) {
        GitRepository repo = gitOptions.cachedBareRepoForUrl(origin);
        if (Iterables.isEmpty(actions)) {
            defaultMirror(repo);
        } else {
            ImmutableList.Builder<ActionResult> allResultsBuilder = ImmutableList.builder();
            for (Action action : actions) {
                GitMirrorContext context = new GitMirrorContext(action, new SkylarkConsole(generalOptions.console()), sourceRefs, refspec, origin, destination, generalOptions.isForced(), repo, generalOptions.getDirFactory(), Dict.empty());
                try {
                    action.run(context);
                    ActionResult actionResult = context.getActionResult();
                    allResultsBuilder.add(actionResult);
                    // First error aborts the execution of the other actions unless --force is used
                    ValidationException.checkCondition(generalOptions.isForced() || actionResult.getResult() != Result.ERROR, "Feedback migration '%s' action '%s' returned error: %s. Aborting execution.", name, action.getName(), actionResult.getMsg());
                } catch (NonFastForwardRepositoryException e) {
                    allResultsBuilder.add(ActionResult.error(action.getName() + ": " + e.getMessage()));
                    if (!generalOptions.isForced()) {
                        throw e;
                    }
                    logger.atWarning().withCause(e).log();
                } finally {
                    generalOptions.eventMonitors().dispatchEvent(m -> m.onChangeMigrationFinished(new ChangeMigrationFinishedEvent(ImmutableList.copyOf(context.getNewDestinationEffects()), getOriginDescription(), getDestinationDescription())));
                }
            }
            ImmutableList<ActionResult> allResults = allResultsBuilder.build();
            if (allResults.stream().anyMatch(a -> a.getResult() == Result.ERROR)) {
                String errors = allResults.stream().filter(a -> a.getResult() == Result.ERROR).map(ActionResult::getMsg).collect(Collectors.joining("\n - "));
                throw new ValidationException("One or more errors happened during the migration:\n" + " - " + errors);
            }
            // This check also returns true if there are no actions
            if (allResults.stream().allMatch(a -> a.getResult() == Result.NO_OP)) {
                String detailedMessage = allResults.isEmpty() ? "actions field is empty" : allResults.stream().map(ActionResult::getMsg).collect(ImmutableList.toImmutableList()).toString();
                throw new EmptyChangeException(String.format("git.mirror migration '%s' was noop. Detailed messages: %s", name, detailedMessage));
            }
        }
    }
    // More fine grain events based on the references created/updated/deleted:
    ChangeMigrationFinishedEvent event = new ChangeMigrationFinishedEvent(ImmutableList.of(new DestinationEffect(generalOptions.dryRunMode ? DestinationEffect.Type.NOOP : DestinationEffect.Type.UPDATED, generalOptions.dryRunMode ? "Refspecs " + refspec + " can be mirrored" : "Refspecs " + refspec + " mirrored successfully", // TODO(danielromero): Populate OriginRef here
    ImmutableList.of(), new DestinationRef(getOriginDestinationRef(destination), "mirror", /*url=*/
    null))), getOriginDescription(), getDestinationDescription());
    generalOptions.eventMonitors().dispatchEvent(m -> m.onChangeMigrationFinished(event));
}
Also used : Action(com.google.copybara.action.Action) ValidationException(com.google.copybara.exception.ValidationException) ChangeMigrationFinishedEvent(com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent) ImmutableList(com.google.common.collect.ImmutableList) SkylarkConsole(com.google.copybara.transform.SkylarkConsole) ActionResult(com.google.copybara.action.ActionResult) DestinationRef(com.google.copybara.DestinationEffect.DestinationRef) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) DestinationEffect(com.google.copybara.DestinationEffect) EmptyChangeException(com.google.copybara.exception.EmptyChangeException)

Example 3 with SkylarkConsole

use of com.google.copybara.transform.SkylarkConsole in project copybara by google.

the class WorkflowRunHelper method migrate.

/**
 * Performs a full migration, including checking out files from the origin, deleting excluded
 * files, transforming the code, and writing to the destination. This writes to the destination
 * exactly once.
 *
 * @param rev revision to the version which will be written to the destination
 * @param lastRev last revision that was migrated
 * @param processConsole console to use to print progress messages
 * @param metadata metadata of the change to be migrated
 * @param changes changes included in this migration
 * @param destinationBaseline it not null, use this baseline in the destination
 * @param changeIdentityRevision the revision to be used for computing the change identity
 */
ImmutableList<DestinationEffect> migrate(O rev, @Nullable O lastRev, Console processConsole, Metadata metadata, Changes changes, @Nullable String destinationBaseline, @Nullable O changeIdentityRevision) throws IOException, RepoException, ValidationException {
    ImmutableList<DestinationEffect> effects = ImmutableList.of();
    boolean callPerMigrationHook = true;
    try {
        eventMonitor().onChangeMigrationStarted(new ChangeMigrationStartedEvent());
        effects = doMigrate(rev, lastRev, processConsole, metadata, changes, destinationBaseline, changeIdentityRevision);
        return effects;
    } catch (EmptyChangeException empty) {
        effects = ImmutableList.of(new DestinationEffect(Type.NOOP, empty.getMessage(), changes.getCurrent(), /*destinationRef=*/
        null, ImmutableList.of()));
        throw empty;
    } catch (ValidationException | IOException | RepoException | RuntimeException e) {
        effects = ImmutableList.of(new DestinationEffect(Type.ERROR, "Errors happened during the migration", changes.getCurrent(), /*destinationRef=*/
        null, ImmutableList.of(e.getMessage() != null ? e.getMessage() : e.toString())));
        callPerMigrationHook = e instanceof ValidationException;
        throw e;
    } finally {
        eventMonitor().onChangeMigrationFinished(new ChangeMigrationFinishedEvent(effects));
        if (callPerMigrationHook) {
            FinishHookContext finishHookContext = new FinishHookContext(getOriginReader().getFeedbackEndPoint(), getDestinationWriter().getFeedbackEndPoint(), effects, resolvedRef, new SkylarkConsole(getConsole()));
            try (ProfilerTask ignored = profiler().start("finish_hooks")) {
                for (Action action : workflow.getAfterMigrationActions()) {
                    try (ProfilerTask ignored2 = profiler().start(action.getName())) {
                        logger.log(Level.INFO, "Running after migration hook: " + action.getName());
                        action.run(finishHookContext);
                    }
                }
            }
        }
    }
}
Also used : Action(com.google.copybara.feedback.Action) ValidationException(com.google.copybara.exception.ValidationException) ChangeMigrationFinishedEvent(com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent) FinishHookContext(com.google.copybara.feedback.FinishHookContext) IOException(java.io.IOException) RepoException(com.google.copybara.exception.RepoException) SkylarkConsole(com.google.copybara.transform.SkylarkConsole) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) ChangeMigrationStartedEvent(com.google.copybara.monitor.EventMonitor.ChangeMigrationStartedEvent) EmptyChangeException(com.google.copybara.exception.EmptyChangeException)

Example 4 with SkylarkConsole

use of com.google.copybara.transform.SkylarkConsole in project copybara by google.

the class Workflow method runHooks.

ImmutableList<DestinationEffect> runHooks(ImmutableList<DestinationEffect> effects, ImmutableList<Action> actions, LazyResourceLoader<Endpoint> originEndpoint, LazyResourceLoader<Endpoint> destinationEndpoint, Revision resolvedRef) throws ValidationException, RepoException {
    SkylarkConsole console = new SkylarkConsole(getConsole());
    List<DestinationEffect> hookDestinationEffects = new ArrayList<>();
    for (Action action : actions) {
        try (ProfilerTask ignored2 = profiler().start(action.getName())) {
            logger.log(Level.INFO, "Running after migration hook: " + action.getName());
            FinishHookContext context = new FinishHookContext(action, originEndpoint, destinationEndpoint, ImmutableList.copyOf(effects), generalOptions.labels, resolvedRef, console);
            action.run(context);
            hookDestinationEffects.addAll(context.getNewDestinationEffects());
        }
    }
    return ImmutableList.<DestinationEffect>builder().addAll(effects).addAll(hookDestinationEffects).build();
}
Also used : SkylarkConsole(com.google.copybara.transform.SkylarkConsole) Action(com.google.copybara.action.Action) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) ArrayList(java.util.ArrayList) FinishHookContext(com.google.copybara.feedback.FinishHookContext)

Example 5 with SkylarkConsole

use of com.google.copybara.transform.SkylarkConsole in project copybara by google.

the class Feedback method run.

@Override
public void run(Path workdir, ImmutableList<String> sourceRefs) throws RepoException, ValidationException {
    ImmutableList.Builder<ActionResult> allResultsBuilder = ImmutableList.builder();
    String suffix = Joiner.on('_').join(sourceRefs).replaceAll("([/ ])", "_");
    String root = "run/" + name + "/" + suffix.substring(0, Math.min(suffix.length(), 20));
    try (ProfilerTask ignore = profiler().start(root)) {
        for (Action action : actions) {
            ArrayList<DestinationEffect> effects = new ArrayList<>();
            try (ProfilerTask ignore2 = profiler().start(action.getName())) {
                SkylarkConsole console = new SkylarkConsole(generalOptions.console());
                eventMonitors().dispatchEvent(m -> m.onChangeMigrationStarted(new ChangeMigrationStartedEvent()));
                FeedbackMigrationContext context = new FeedbackMigrationContext(this, action, generalOptions.cliLabels(), sourceRefs, console);
                action.run(context);
                effects.addAll(context.getNewDestinationEffects());
                ActionResult actionResult = context.getActionResult();
                allResultsBuilder.add(actionResult);
                // First error aborts the execution of the other actions
                ValidationException.checkCondition(actionResult.getResult() != Result.ERROR, "Feedback migration '%s' action '%s' returned error: %s. Aborting execution.", name, action.getName(), actionResult.getMsg());
            } finally {
                eventMonitors().dispatchEvent(m -> m.onChangeMigrationFinished(new ChangeMigrationFinishedEvent(ImmutableList.copyOf(effects), getOriginDescription(), getDestinationDescription())));
            }
        }
    }
    ImmutableList<ActionResult> allResults = allResultsBuilder.build();
    // This check also returns true if there are no actions
    if (allResults.stream().allMatch(a -> a.getResult() == Result.NO_OP)) {
        String detailedMessage = allResults.isEmpty() ? "actions field is empty" : allResults.stream().map(ActionResult::getMsg).collect(ImmutableList.toImmutableList()).toString();
        throw new EmptyChangeException(String.format("Feedback migration '%s' was noop. Detailed messages: %s", name, detailedMessage));
    }
}
Also used : Action(com.google.copybara.action.Action) ChangeMigrationFinishedEvent(com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) SkylarkConsole(com.google.copybara.transform.SkylarkConsole) ActionResult(com.google.copybara.action.ActionResult) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) DestinationEffect(com.google.copybara.DestinationEffect) ChangeMigrationStartedEvent(com.google.copybara.monitor.EventMonitor.ChangeMigrationStartedEvent) EmptyChangeException(com.google.copybara.exception.EmptyChangeException)

Aggregations

ProfilerTask (com.google.copybara.profiler.Profiler.ProfilerTask)5 SkylarkConsole (com.google.copybara.transform.SkylarkConsole)5 Action (com.google.copybara.action.Action)3 EmptyChangeException (com.google.copybara.exception.EmptyChangeException)3 ChangeMigrationFinishedEvent (com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent)3 ImmutableList (com.google.common.collect.ImmutableList)2 DestinationEffect (com.google.copybara.DestinationEffect)2 ActionResult (com.google.copybara.action.ActionResult)2 ValidationException (com.google.copybara.exception.ValidationException)2 FinishHookContext (com.google.copybara.feedback.FinishHookContext)2 ChangeMigrationStartedEvent (com.google.copybara.monitor.EventMonitor.ChangeMigrationStartedEvent)2 ArrayList (java.util.ArrayList)2 DestinationRef (com.google.copybara.DestinationEffect.DestinationRef)1 RepoException (com.google.copybara.exception.RepoException)1 Action (com.google.copybara.feedback.Action)1 Profiler (com.google.copybara.profiler.Profiler)1 IOException (java.io.IOException)1