Search in sources :

Example 1 with TransformationStatus

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

the class Sequence method transform.

@Override
public TransformationStatus transform(TransformWork work) throws IOException, ValidationException, RepoException {
    List<Transformation> transformationList = getTransformations();
    boolean someTransformWasSuccess = false;
    for (int i = 0; i < transformationList.size(); i++) {
        // Only check the cache in between consecutive Transforms
        if (i != 0) {
            work.validateTreeStateCache();
        }
        Transformation transformation = transformationList.get(i);
        work.getConsole().progress(getTransformMessage(transformation, i, transformationList.size()));
        TransformationStatus status = runOneTransform(work, transformation);
        if (status.isNoop()) {
            if (noopBehavior == NoopBehavior.FAIL_IF_ANY_NOOP) {
                status.throwException(work.getConsole(), workflowOptions.ignoreNoop);
            } else if (noopBehavior == NoopBehavior.NOOP_IF_ANY_NOOP) {
                if (workflowOptions.ignoreNoop) {
                    status.warn(work.getConsole());
                } else {
                    return status;
                }
            } else if (work.getConsole().isVerbose()) {
                status.warn(work.getConsole());
            }
        }
        someTransformWasSuccess |= status.isSuccess();
    }
    if (noopBehavior == NoopBehavior.NOOP_IF_ALL_NOOP && !someTransformWasSuccess) {
        return TransformationStatus.noop(String.format("%s was a no-op because all wrapped transforms were no-ops", this));
    }
    return TransformationStatus.success();
}
Also used : TransformationStatus(com.google.copybara.TransformationStatus) Transformation(com.google.copybara.Transformation) Transformations.toTransformation(com.google.copybara.transform.Transformations.toTransformation)

Example 2 with TransformationStatus

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

the class SkylarkTransformation method transform.

@Override
public TransformationStatus transform(TransformWork work) throws IOException, ValidationException, RepoException {
    SkylarkConsole skylarkConsole = new SkylarkConsole(work.getConsole());
    TransformWork skylarkWork = work.withConsole(skylarkConsole).withParams(params);
    TransformationStatus status = TransformationStatus.success();
    try (Mutability mu = Mutability.create("dynamic_transform")) {
        StarlarkThread thread = new StarlarkThread(mu, StarlarkSemantics.DEFAULT);
        thread.setPrintHandler(printHandler);
        Object result = Starlark.call(thread, function, ImmutableList.of(skylarkWork), /*kwargs=*/
        ImmutableMap.of());
        result = result == Starlark.NONE ? TransformationStatus.success() : result;
        checkCondition(result instanceof TransformationStatus, "Dynamic transforms functions should return nothing or objects of type %s, but '%s'" + " returned: %s", TransformationStatus.STARLARK_TYPE_NAME, describe(), result);
        status = (TransformationStatus) result;
    } catch (EvalException e) {
        if (e.getCause() instanceof EmptyChangeException) {
            throw ((EmptyChangeException) e.getCause());
        }
        if (e.getCause() instanceof RepoException) {
            throw new RepoException(String.format("Error while executing the skylark transformation %s: %s", describe(), e.getMessageWithStack()), e);
        }
        throw new ValidationException(String.format("Error while executing the skylark transformation %s: %s", describe(), e.getMessageWithStack()), e);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new RuntimeException("This should not happen.", e);
    } finally {
        work.updateFrom(skylarkWork);
    }
    checkCondition(skylarkConsole.getErrorCount() == 0, "%d error(s) while executing %s", skylarkConsole.getErrorCount(), describe());
    return status;
}
Also used : TransformationStatus(com.google.copybara.TransformationStatus) ValidationException(com.google.copybara.exception.ValidationException) NonReversibleValidationException(com.google.copybara.exception.NonReversibleValidationException) StarlarkThread(net.starlark.java.eval.StarlarkThread) TransformWork(com.google.copybara.TransformWork) Mutability(net.starlark.java.eval.Mutability) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) EvalException(net.starlark.java.eval.EvalException) RepoException(com.google.copybara.exception.RepoException)

Example 3 with TransformationStatus

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

the class TransformDebug method transform.

@Override
public TransformationStatus transform(TransformWork work) throws IOException, ValidationException, RepoException {
    Console console = work.getConsole();
    boolean fileDebug = false;
    if (debugOptions.getDebugFileBreak() != null) {
        fileDebug = true;
    }
    boolean metadataDebug = false;
    if (debugOptions.debugMetadataBreak) {
        metadataDebug = true;
    }
    Pattern debugTransformBreak = debugOptions.getDebugTransformBreak();
    boolean transformMatch = false;
    if (debugTransformBreak != null && debugTransformBreak.matcher(this.delegate.describe()).find()) {
        transformMatch = true;
    }
    if (!fileDebug && !metadataDebug && !transformMatch) {
        // Nothing to debug!
        return delegate.transform(work);
    }
    TreeMap<String, byte[]> before = readState(work, fileDebug || transformMatch, work.getTreeState());
    TransformationStatus status = delegate.transform(work);
    work.validateTreeStateCache();
    TreeMap<String, byte[]> after = readState(work, fileDebug || transformMatch, work.getTreeState());
    MapDifference<String, byte[]> difference = Maps.difference(before, after, new Equivalence<byte[]>() {

        @Override
        protected boolean doEquivalent(byte[] one, byte[] other) {
            return Arrays.equals(one, other);
        }

        @Override
        protected int doHash(byte[] bytes) {
            return Arrays.hashCode(bytes);
        }
    });
    boolean stop = transformMatch;
    if (fileDebug) {
        PathMatcher debugFileBreak = debugOptions.getDebugFileBreak().relativeTo(Paths.get("/"));
        for (String path : Iterables.concat(difference.entriesOnlyOnLeft().keySet(), difference.entriesOnlyOnRight().keySet(), difference.entriesDiffering().keySet())) {
            if (path.equals(COPYBARA_METADATA_FAKE_FILE)) {
                continue;
            }
            if (debugFileBreak.matches(Paths.get("/" + path))) {
                stop = true;
                console.infoFmt("File '%s' change matched. Stopping", path);
                break;
            }
        }
    } else if (metadataDebug && !Arrays.equals(before.get(COPYBARA_METADATA_FAKE_FILE), after.get(COPYBARA_METADATA_FAKE_FILE))) {
        stop = true;
        console.infoFmt("Message, author and/or labels changed");
    }
    if (!stop) {
        return status;
    }
    if (!transformMatch) {
        // Stopped because of file/metadata change. Show the diff directly
        showDiff(console, difference);
    }
    while (true) {
        String answer = console.ask("Debugger stopped after '" + delegate.describe() + "' " + console.colorize(AnsiColor.PURPLE, delegate.location().toString()) + ".\n" + "      Current file state can be checked at " + work.getCheckoutDir() + "\n" + "Diff (d), Continue (c), Stop (s): ", "d", input -> ImmutableSet.of("d", "c", "s").contains(input));
        switch(answer) {
            case "d":
                {
                    showDiff(console, difference);
                    break;
                }
            case "c":
                return status;
            case "s":
                throw new ValidationException("Stopped by user");
        }
    }
}
Also used : Pattern(com.google.re2j.Pattern) TransformationStatus(com.google.copybara.TransformationStatus) PathMatcher(java.nio.file.PathMatcher) ValidationException(com.google.copybara.exception.ValidationException) NonReversibleValidationException(com.google.copybara.exception.NonReversibleValidationException) Console(com.google.copybara.util.console.Console)

Example 4 with TransformationStatus

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

the class BuildifierFormatTest method noop.

@Test
public void noop() throws ValidationException, IOException {
    BuildifierFormat b = skylark.eval("c", "c = format.buildifier(glob([\"BUILD\"]))\n");
    Files.createDirectories(checkoutDir.resolve("foo"));
    Files.write(checkoutDir.resolve("foo/BUILD"), NOT_FORMATTED.getBytes(UTF_8));
    TransformationStatus status = b.transform(TransformWorks.of(checkoutDir, "foo", console));
    assertThat(status.isNoop()).isTrue();
}
Also used : TransformationStatus(com.google.copybara.TransformationStatus) Test(org.junit.Test)

Example 5 with TransformationStatus

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

the class BuildozerModifyTest method testNoopIsWarningTarget.

@Test
public void testNoopIsWarningTarget() throws Exception {
    BuildozerModify modify = skylark.eval("m", "m = " + "buildozer.modify(\n" + "    target = 'foo:doesnt_exist',\n" + "    commands = [buildozer.cmd('remove deps')],\n" + ")");
    options.workflowOptions.ignoreNoop = true;
    Files.createDirectories(checkoutDir.resolve("foo"));
    Files.write(checkoutDir.resolve("foo/BUILD"), "".getBytes(UTF_8));
    TransformationStatus status = transform(modify);
    assertThat(status.isNoop()).isTrue();
    assertThat(status.getMessage()).matches(".*Buildozer could not find a target for foo:doesnt_exist:" + " rule 'doesnt_exist' not found.*");
}
Also used : TransformationStatus(com.google.copybara.TransformationStatus) Test(org.junit.Test)

Aggregations

TransformationStatus (com.google.copybara.TransformationStatus)27 Test (org.junit.Test)23 Transformation (com.google.copybara.Transformation)12 TransformWork (com.google.copybara.TransformWork)9 NonReversibleValidationException (com.google.copybara.exception.NonReversibleValidationException)2 ValidationException (com.google.copybara.exception.ValidationException)2 EmptyChangeException (com.google.copybara.exception.EmptyChangeException)1 RepoException (com.google.copybara.exception.RepoException)1 Transformations.toTransformation (com.google.copybara.transform.Transformations.toTransformation)1 Console (com.google.copybara.util.console.Console)1 Pattern (com.google.re2j.Pattern)1 PathMatcher (java.nio.file.PathMatcher)1 EvalException (net.starlark.java.eval.EvalException)1 Mutability (net.starlark.java.eval.Mutability)1 StarlarkThread (net.starlark.java.eval.StarlarkThread)1