Search in sources :

Example 21 with DestinationEffect

use of com.google.copybara.DestinationEffect 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)

Example 22 with DestinationEffect

use of com.google.copybara.DestinationEffect in project copybara by google.

the class GerritDestinationTest method testProcessPushOutput_new.

@Test
public void testProcessPushOutput_new() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GerritProcessPushOutput process = new GerritProcessPushOutput(LogConsole.readWriteConsole(System.in, new PrintStream(out), /*verbose=*/
    false));
    fakeOneCommitInDestination();
    ImmutableList<DestinationEffect> result = process.process(GERRIT_RESPONSE, /*newReview*/
    true, repo(), Changes.EMPTY.getCurrent());
    assertThat(out.toString()).contains("INFO: New Gerrit review created at https://some.url.google.com/1234");
    assertThat(result).hasSize(2);
    assertThat(result.get(0).getErrors()).isEmpty();
    assertThat(result.get(0).getType()).isEqualTo(Type.CREATED);
    assertThat(result.get(0).getDestinationRef().getType()).isEqualTo("commit");
    assertThat(result.get(0).getDestinationRef().getId()).matches("[0-9a-f]{40}");
    assertThat(result.get(1).getSummary()).contains("New Gerrit review created at https://some.url.google.com/1234");
    assertThat(result.get(1).getType()).isEqualTo(Type.CREATED);
    assertThat(result.get(1).getDestinationRef().getId()).isEqualTo("1234");
    assertThat(result.get(1).getDestinationRef().getType()).isEqualTo("gerrit_review");
    assertThat(result.get(1).getDestinationRef().getUrl()).isEqualTo("https://some.url.google.com/1234");
}
Also used : PrintStream(java.io.PrintStream) DestinationEffect(com.google.copybara.DestinationEffect) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GerritProcessPushOutput(com.google.copybara.git.GerritDestination.GerritProcessPushOutput) Test(org.junit.Test)

Example 23 with DestinationEffect

use of com.google.copybara.DestinationEffect in project copybara by google.

the class GerritDestinationTest method testProcessPushOutput_existing.

@Test
public void testProcessPushOutput_existing() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GerritProcessPushOutput process = new GerritProcessPushOutput(LogConsole.readWriteConsole(System.in, new PrintStream(out), /*verbose=*/
    false));
    fakeOneCommitInDestination();
    ImmutableList<DestinationEffect> result = process.process(GERRIT_RESPONSE, /*newReview*/
    false, repo(), Changes.EMPTY.getCurrent());
    assertThat(out.toString()).contains("INFO: Updated existing Gerrit review at https://some.url.google.com/1234");
    assertThat(result).hasSize(2);
    assertThat(result.get(0).getErrors()).isEmpty();
    assertThat(result.get(0).getType()).isEqualTo(Type.CREATED);
    assertThat(result.get(0).getDestinationRef().getType()).isEqualTo("commit");
    assertThat(result.get(0).getDestinationRef().getId()).matches("[0-9a-f]{40}");
    assertThat(result.get(1).getSummary()).contains("Updated existing Gerrit review at https://some.url.google.com/1234");
    assertThat(result.get(1).getType()).isEqualTo(Type.UPDATED);
    assertThat(result.get(1).getDestinationRef().getId()).isEqualTo("1234");
    assertThat(result.get(1).getDestinationRef().getType()).isEqualTo("gerrit_review");
    assertThat(result.get(1).getDestinationRef().getUrl()).isEqualTo("https://some.url.google.com/1234");
}
Also used : PrintStream(java.io.PrintStream) DestinationEffect(com.google.copybara.DestinationEffect) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GerritProcessPushOutput(com.google.copybara.git.GerritDestination.GerritProcessPushOutput) Test(org.junit.Test)

Example 24 with DestinationEffect

use of com.google.copybara.DestinationEffect in project copybara by google.

the class ActionContext method recordEffect.

@StarlarkMethod(name = "record_effect", doc = "Records an effect of the current action.", parameters = { @Param(name = "summary", doc = "The summary of this effect", named = true), @Param(name = "origin_refs", allowedTypes = { @ParamType(type = Sequence.class, generic1 = OriginRef.class) }, doc = "The origin refs", named = true), @Param(name = "destination_ref", doc = "The destination ref", named = true), @Param(name = "errors", allowedTypes = { @ParamType(type = Sequence.class, generic1 = String.class) }, defaultValue = "[]", doc = "An optional list of errors", named = true), @Param(name = "type", doc = "The type of migration effect:<br>" + "<ul>" + "<li><b>'CREATED'</b>: A new review or change was created.</li>" + "<li><b>'UPDATED'</b>: An existing review or change was updated.</li>" + "<li><b>'NOOP'</b>: The change was a noop.</li>" + "<li><b>'NOOP_AGAINST_PENDING_CHANGE'</b>: The change was a noop, relative" + "to an existing pending change.</li>" + "<li><b>'INSUFFICIENT_APPROVALS'</b>: The effect couldn't happen because " + "the change doesn't have enough approvals.</li>" + "<li><b>'ERROR'</b>: A user attributable error happened that prevented " + "the destination from creating/updating the change. " + "<li><b>'STARTED'</b>: The initial effect of a migration that depends on a " + "previous one. This allows to have 'dependant' migrations defined by users.\n" + "An example of this: a workflow migrates code from a Gerrit review to a " + "GitHub PR, and a feedback migration migrates the test results from a CI in " + "GitHub back to the Gerrit change.\n" + "This effect would be created on the former one.</li>" + "</ul>", defaultValue = "\"UPDATED\"", named = true) })
public void recordEffect(String summary, // <OriginRef>
Sequence<?> originRefs, DestinationRef destinationRef, // <String>
Sequence<?> errors, String typeStr) throws EvalException {
    DestinationEffect.Type type = SkylarkUtil.stringToEnum("type", typeStr, DestinationEffect.Type.class);
    newDestinationEffects.add(new DestinationEffect(type, summary, Sequence.cast(originRefs, OriginRef.class, "origin_refs"), destinationRef, Sequence.cast(errors, String.class, "errors")));
}
Also used : DestinationEffect(com.google.copybara.DestinationEffect) StarlarkMethod(net.starlark.java.annot.StarlarkMethod)

Example 25 with DestinationEffect

use of com.google.copybara.DestinationEffect 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

DestinationEffect (com.google.copybara.DestinationEffect)39 DummyRevision (com.google.copybara.testing.DummyRevision)26 Test (org.junit.Test)23 WriterContext (com.google.copybara.WriterContext)19 Author (com.google.copybara.authoring.Author)13 Glob (com.google.copybara.util.Glob)12 ImmutableList (com.google.common.collect.ImmutableList)11 Changes (com.google.copybara.Changes)10 ChangeMessage (com.google.copybara.ChangeMessage)8 Metadata (com.google.copybara.Metadata)7 MigrationInfo (com.google.copybara.MigrationInfo)7 TransformResult (com.google.copybara.TransformResult)7 TransformWork (com.google.copybara.TransformWork)7 CheckerException (com.google.copybara.checks.CheckerException)7 ValidationException (com.google.copybara.exception.ValidationException)7 GerritMessageInfo (com.google.copybara.git.GerritDestination.GerritMessageInfo)7 DummyChecker (com.google.copybara.testing.DummyChecker)7 FileSubjects.assertThatPath (com.google.copybara.testing.FileSubjects.assertThatPath)7 LowLevelHttpRequest (com.google.api.client.http.LowLevelHttpRequest)6 Joiner (com.google.common.base.Joiner)6