use of com.google.copybara.monitor.EventMonitor.ChangeMigrationStartedEvent in project copybara by google.
the class ConsoleEventMonitorTest method testEventMonitorPrintsToConsole.
@Test
public void testEventMonitorPrintsToConsole() {
eventMonitor.onMigrationStarted(new MigrationStartedEvent());
eventMonitor.onMigrationFinished(new MigrationFinishedEvent(ExitCode.SUCCESS));
eventMonitor.onChangeMigrationStarted(new ChangeMigrationStartedEvent());
DestinationEffect destinationEffect = new DestinationEffect(Type.CREATED, "Created revision 1234", ImmutableList.of(new OriginRef("ABCD")), new DestinationRef("1234", "commit", /*url=*/
null));
eventMonitor.onChangeMigrationFinished(new ChangeMigrationFinishedEvent(ImmutableList.of(destinationEffect), ImmutableMultimap.of(), ImmutableMultimap.of()));
MigrationReference<DummyRevision> workflow = MigrationReference.create("workflow", new DummyRevision("1111"), ImmutableList.of(newChange("2222"), newChange("3333")));
Info<?> info = Info.create(ImmutableMultimap.of("origin", "foo"), ImmutableMultimap.of("dest", "bar"), ImmutableList.of(workflow));
eventMonitor.onInfoFinished(new InfoFinishedEvent(info, ImmutableMap.of("foo_a", "foo_b")));
console.assertThat().equalsNext(VERBOSE, "onMigrationStarted(): MigrationStartedEvent").equalsNext(VERBOSE, "onMigrationFinished(): " + "MigrationFinishedEvent{exitCode=SUCCESS, profiler=null}").equalsNext(VERBOSE, "onChangeMigrationStarted(): ChangeMigrationStartedEvent{}").matchesNext(VERBOSE, "onChangeMigrationFinished[(][)]: ChangeMigrationFinishedEvent[{].*[}]").matchesNext(VERBOSE, "onInfoFinished[(][)]: InfoFinishedEvent[{].*(origin).*" + "(foo).*(dest).*(bar).*(foo_a).*(foo_b).*[}]").containsNoMoreMessages();
}
use of com.google.copybara.monitor.EventMonitor.ChangeMigrationStartedEvent 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);
}
}
}
}
}
}
use of com.google.copybara.monitor.EventMonitor.ChangeMigrationStartedEvent 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));
}
}
Aggregations