Search in sources :

Example 1 with ChangeMigrationFinishedEvent

use of com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent 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 2 with ChangeMigrationFinishedEvent

use of com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent in project copybara by google.

the class FeedbackTest method runAndVerifyDestinationEffects.

private void runAndVerifyDestinationEffects(String actionsCode, ImmutableList<String> expectedErrors, String expectedSummary, String expectedOriginRef, String expectedDestRef, String expectedDestType, @Nullable String expectedDestUrl) throws IOException, ValidationException, RepoException {
    Feedback feedback = feedback(actionsCode, "test_action");
    feedback.run(workdir, ImmutableList.of());
    console.assertThat().equalsNext(MessageType.INFO, "Action 'test_action' returned success");
    assertThat(eventMonitor.changeMigrationStartedEventCount()).isEqualTo(1);
    assertThat(eventMonitor.changeMigrationFinishedEventCount()).isEqualTo(1);
    ChangeMigrationFinishedEvent event = Iterables.getOnlyElement(eventMonitor.changeMigrationFinishedEvents);
    DestinationEffect effect = Iterables.getOnlyElement(event.getDestinationEffects());
    assertThat(effect.getSummary()).isEqualTo(expectedSummary);
    assertThat(effect.getOriginRefs()).hasSize(1);
    assertThat(effect.getOriginRefs().get(0).getRef()).isEqualTo(expectedOriginRef);
    DestinationRef destinationRef = effect.getDestinationRef();
    assertThat(destinationRef.getId()).isEqualTo(expectedDestRef);
    assertThat(destinationRef.getType()).isEqualTo(expectedDestType);
    if (expectedDestUrl == null) {
        assertThat(destinationRef.getUrl()).isNull();
    } else {
        assertThat(destinationRef.getUrl()).isEqualTo(expectedDestUrl);
    }
    assertThat(effect.getErrors()).containsExactlyElementsIn(expectedErrors);
}
Also used : DestinationRef(com.google.copybara.DestinationEffect.DestinationRef) Feedback(com.google.copybara.feedback.Feedback) ChangeMigrationFinishedEvent(com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent)

Example 3 with ChangeMigrationFinishedEvent

use of com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent in project copybara by google.

the class WorkflowTest method verifyHookForException.

private <T extends Exception> void verifyHookForException(Class<T> expectedExceptionType, Type expectedEffectType, String expectedErrorMsg) throws IOException, ValidationException, RepoException {
    origin.singleFileChange(0, "one commit", "foo.txt", "1");
    String config = "" + "def test(ctx):\n" + "  origin_refs = [ctx.origin.new_origin_ref('1111')]\n" + "  dest_ref = ctx.destination.new_destination_ref(ref = '9999', type = 'some_type')\n" + "  ctx.record_effect('New effect', origin_refs, dest_ref)\n" + "\n" + "core.workflow(\n" + "  name = 'default',\n" + "  origin = testing.origin(),\n" + "  destination = testing.destination(),\n" + "  transformations = [],\n" + "  authoring = " + authoring + ",\n" + "  after_migration = [test]" + ")\n";
    try {
        loadConfig(config).getMigration("default").run(workdir, ImmutableList.of());
        fail();
    } catch (Exception expected) {
        if (!expectedExceptionType.isInstance(expected)) {
            throw expected;
        }
        assertThat(expected).hasMessageThat().contains(expectedErrorMsg);
    }
    assertThat(eventMonitor.changeMigrationFinishedEventCount()).isEqualTo(1);
    ChangeMigrationFinishedEvent event = Iterables.getOnlyElement(eventMonitor.changeMigrationFinishedEvents);
    assertThat(event.getDestinationEffects()).hasSize(2);
    DestinationEffect firstEffect = event.getDestinationEffects().get(0);
    assertThat(firstEffect.getType()).isEqualTo(expectedEffectType);
    assertThat(firstEffect.getSummary()).isEqualTo("Errors happened during the migration");
    assertThat(firstEffect.getErrors()).contains(expectedErrorMsg);
    // Effect from the hook is also created
    DestinationEffect secondEffect = event.getDestinationEffects().get(1);
    assertThat(secondEffect.getSummary()).isEqualTo("New effect");
    assertThat(secondEffect.getType()).isEqualTo(Type.UPDATED);
    assertThat(secondEffect.getOriginRefs().get(0).getRef()).isEqualTo("1111");
    assertThat(secondEffect.getDestinationRef().getId()).isEqualTo("9999");
}
Also used : ChangeMigrationFinishedEvent(com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent) ChangeRejectedException(com.google.copybara.exception.ChangeRejectedException) CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException) VoidOperationException(com.google.copybara.exception.VoidOperationException) NotADestinationFileException(com.google.copybara.exception.NotADestinationFileException) RepoException(com.google.copybara.exception.RepoException) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) ValidationException(com.google.copybara.exception.ValidationException) IOException(java.io.IOException)

Example 4 with ChangeMigrationFinishedEvent

use of com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent 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();
}
Also used : DestinationRef(com.google.copybara.DestinationEffect.DestinationRef) InfoFinishedEvent(com.google.copybara.monitor.EventMonitor.InfoFinishedEvent) DestinationEffect(com.google.copybara.DestinationEffect) ChangeMigrationFinishedEvent(com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent) ChangeMigrationStartedEvent(com.google.copybara.monitor.EventMonitor.ChangeMigrationStartedEvent) DummyRevision(com.google.copybara.testing.DummyRevision) MigrationStartedEvent(com.google.copybara.monitor.EventMonitor.MigrationStartedEvent) ChangeMigrationStartedEvent(com.google.copybara.monitor.EventMonitor.ChangeMigrationStartedEvent) MigrationFinishedEvent(com.google.copybara.monitor.EventMonitor.MigrationFinishedEvent) ChangeMigrationFinishedEvent(com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent) OriginRef(com.google.copybara.DestinationEffect.OriginRef) Test(org.junit.Test)

Example 5 with ChangeMigrationFinishedEvent

use of com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent in project copybara by google.

the class Mirror method run.

@Override
public void run(Path workdir, @Nullable String sourceRef) throws RepoException, IOException, ValidationException {
    mirrorOptions.mirror(origin, destination, refspec, prune);
    // More fine grain events based on the references created/updated/deleted:
    generalOptions.eventMonitor().onChangeMigrationFinished(new ChangeMigrationFinishedEvent(ImmutableList.of(new DestinationEffect(Type.UPDATED, "Refspecs " + refspec + " mirrored successfully", // references
    ImmutableList.of(), new DestinationRef(getOriginDestinationRef(destination), "mirror", /*url=*/
    null), ImmutableList.of()))));
}
Also used : DestinationRef(com.google.copybara.DestinationEffect.DestinationRef) ChangeMigrationFinishedEvent(com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent) DestinationEffect(com.google.copybara.DestinationEffect)

Aggregations

ChangeMigrationFinishedEvent (com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent)8 DestinationEffect (com.google.copybara.DestinationEffect)4 DestinationRef (com.google.copybara.DestinationEffect.DestinationRef)4 EmptyChangeException (com.google.copybara.exception.EmptyChangeException)4 ValidationException (com.google.copybara.exception.ValidationException)3 ChangeMigrationStartedEvent (com.google.copybara.monitor.EventMonitor.ChangeMigrationStartedEvent)3 ProfilerTask (com.google.copybara.profiler.Profiler.ProfilerTask)3 SkylarkConsole (com.google.copybara.transform.SkylarkConsole)3 ImmutableList (com.google.common.collect.ImmutableList)2 Action (com.google.copybara.action.Action)2 ActionResult (com.google.copybara.action.ActionResult)2 RepoException (com.google.copybara.exception.RepoException)2 IOException (java.io.IOException)2 Test (org.junit.Test)2 OriginRef (com.google.copybara.DestinationEffect.OriginRef)1 CannotResolveRevisionException (com.google.copybara.exception.CannotResolveRevisionException)1 ChangeRejectedException (com.google.copybara.exception.ChangeRejectedException)1 NotADestinationFileException (com.google.copybara.exception.NotADestinationFileException)1 VoidOperationException (com.google.copybara.exception.VoidOperationException)1 Action (com.google.copybara.feedback.Action)1