Search in sources :

Example 21 with StarlarkMethod

use of net.starlark.java.annot.StarlarkMethod in project copybara by google.

the class Core method transform.

@SuppressWarnings("unused")
@StarlarkMethod(name = "transform", doc = "Groups some transformations in a transformation that can contain a particular," + " manually-specified, reversal, where the forward version and reversed version" + " of the transform are represented as lists of transforms. The is useful if a" + " transformation does not automatically reverse, or if the automatic reversal" + " does not work for some reason." + "<br>" + "If reversal is not provided, the transform will try to compute the reverse of" + " the transformations list.", parameters = { @Param(name = "transformations", allowedTypes = { @ParamType(type = net.starlark.java.eval.Sequence.class, generic1 = Transformation.class) }, named = true, doc = "The list of transformations to run as a result of running this transformation."), @Param(name = "reversal", allowedTypes = { @ParamType(type = net.starlark.java.eval.Sequence.class, generic1 = Transformation.class), @ParamType(type = NoneType.class) }, doc = "The list of transformations to run as a result of running this" + " transformation in reverse.", named = true, positional = false, defaultValue = "None"), @Param(name = "ignore_noop", allowedTypes = { @ParamType(type = Boolean.class), @ParamType(type = NoneType.class) }, doc = "WARNING: Deprecated. Use `noop_behavior` instead.\nIn case a noop error happens in" + " the group of transformations (Both forward and reverse), it will be" + " ignored, but the rest of the transformations in the group will still be" + " executed. If ignore_noop is not set, we will apply the closest parent's" + " ignore_noop.", named = true, positional = false, defaultValue = "None"), @Param(name = "noop_behavior", allowedTypes = { @ParamType(type = String.class), @ParamType(type = NoneType.class) }, doc = "How to handle no-op transformations:<br><ul> <li><b>'IGNORE_NOOP'</b>: Any no-ops" + " among the wrapped transformations are ignored.</li>" + " <li><b>'NOOP_IF_ANY_NOOP'</b>: Throws an exception as soon as a single" + " wrapped transformation is a no-op.</li> <li><b>'NOOP_IF_ALL_NOOP'</b>:" + " Ignores no-ops from the wrapped transformations unless they all no-op, in" + " which case an exception is thrown.</li></ul>", named = true, positional = false, defaultValue = "None") })
@DocDefault(field = "reversal", value = "The reverse of 'transformations'")
@DocDefault(field = "noop_behavior", value = "NOOP_IF_ANY_NOOP")
public Transformation transform(// <Transformation>
net.starlark.java.eval.Sequence<?> transformations, Object reversal, Object ignoreNoop, Object noopBehaviorString) throws EvalException, ValidationException {
    checkCondition(Starlark.isNullOrNone(ignoreNoop) || Starlark.isNullOrNone(noopBehaviorString), "The deprecated param 'ignore_noop' cannot be set simultaneously with 'noop_behavior'." + " Prefer using 'noop_behavior'.");
    Sequence.NoopBehavior noopBehavior = stringToEnum("noop_behavior", convertFromNoneable(noopBehaviorString, "NOOP_IF_ANY_NOOP"), Sequence.NoopBehavior.class);
    if (Boolean.TRUE.equals(ignoreNoop)) {
        noopBehavior = Sequence.NoopBehavior.IGNORE_NOOP;
    } else if (Boolean.FALSE.equals(ignoreNoop)) {
        noopBehavior = Sequence.NoopBehavior.FAIL_IF_ANY_NOOP;
    }
    Sequence forward = Sequence.fromConfig(generalOptions.profiler(), workflowOptions, transformations, "transformations", printHandler, debugOptions::transformWrapper, noopBehavior);
    net.starlark.java.eval.Sequence<Transformation> reverseList = convertFromNoneable(reversal, null);
    if (reverseList == null) {
        try {
            reverseList = StarlarkList.immutableCopyOf(ImmutableList.of(forward.reverse()));
        } catch (NonReversibleValidationException e) {
            throw Starlark.errorf("transformations are not automatically reversible." + " Use 'reversal' field to explicitly configure the reversal of the transform");
        }
    }
    Sequence reverse = Sequence.fromConfig(generalOptions.profiler(), workflowOptions, reverseList, "reversal", printHandler, debugOptions::transformWrapper, noopBehavior);
    return new ExplicitReversal(forward, reverse);
}
Also used : NonReversibleValidationException(com.google.copybara.exception.NonReversibleValidationException) SkylarkTransformation(com.google.copybara.transform.SkylarkTransformation) Transformations.toTransformation(com.google.copybara.transform.Transformations.toTransformation) Sequence(com.google.copybara.transform.Sequence) ExplicitReversal(com.google.copybara.transform.ExplicitReversal) StarlarkMethod(net.starlark.java.annot.StarlarkMethod) DocDefault(com.google.copybara.doc.annotations.DocDefault)

Example 22 with StarlarkMethod

use of net.starlark.java.annot.StarlarkMethod in project copybara by google.

the class TransformWork method createSymlink.

@StarlarkMethod(name = "create_symlink", doc = "Create a symlink", parameters = { @Param(name = "link", doc = "The link path"), @Param(name = "target", doc = "The target path") })
public void createSymlink(CheckoutPath link, CheckoutPath target) throws EvalException {
    try {
        Path linkFullPath = asCheckoutPath(link);
        // Verify target is inside checkout dir
        asCheckoutPath(target);
        if (Files.exists(linkFullPath)) {
            throw Starlark.errorf("'%s' already exist%s", link.getPath(), Files.isDirectory(linkFullPath) ? " and is a directory" : Files.isSymbolicLink(linkFullPath) ? " and is a symlink" : Files.isRegularFile(linkFullPath) ? " and is a regular file" : // Shouldn't happen:
            " and we don't know what kind of file is");
        }
        Path relativized = link.getPath().getParent() == null ? target.getPath() : link.getPath().getParent().relativize(target.getPath());
        Files.createDirectories(linkFullPath.getParent());
        // Shouldn't happen.
        Verify.verify(linkFullPath.getParent().resolve(relativized).normalize().startsWith(checkoutDir), "%s path escapes the checkout dir", relativized);
        Files.createSymbolicLink(linkFullPath, relativized);
    } catch (IOException e) {
        String msg = "Cannot create symlink: " + e.getMessage();
        logger.atSevere().withCause(e).log("%s", msg);
        throw Starlark.errorf("%s", msg);
    }
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) StarlarkMethod(net.starlark.java.annot.StarlarkMethod)

Example 23 with StarlarkMethod

use of net.starlark.java.annot.StarlarkMethod in project copybara by google.

the class TransformWork method writePath.

@StarlarkMethod(name = "write_path", doc = "Write an arbitrary string to a path (UTF-8 will be used)", parameters = { @Param(name = "path", doc = "The Path to write to"), @Param(name = "content", doc = "The content of the file") })
public void writePath(CheckoutPath path, String content) throws IOException, EvalException {
    Path fullPath = asCheckoutPath(path);
    if (fullPath.getParent() != null) {
        Files.createDirectories(fullPath.getParent());
    }
    Files.write(fullPath, content.getBytes(UTF_8));
}
Also used : Path(java.nio.file.Path) StarlarkMethod(net.starlark.java.annot.StarlarkMethod)

Example 24 with StarlarkMethod

use of net.starlark.java.annot.StarlarkMethod 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 StarlarkMethod

use of net.starlark.java.annot.StarlarkMethod in project copybara by google.

the class GerritEndpoint method getChange.

@StarlarkMethod(name = "get_change", doc = "Retrieve a Gerrit change.", parameters = { @Param(name = "id", named = true, doc = "The change id or change number."), @Param(name = "include_results", named = true, allowedTypes = { @ParamType(type = Sequence.class, generic1 = String.class) }, doc = "" + "What to include in the response. See " + "https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html" + "#query-options", positional = false, defaultValue = "['LABELS']") })
public ChangeInfo getChange(String id, Sequence<?> includeResults) throws EvalException {
    try {
        ChangeInfo changeInfo = doGetChange(id, getIncludeResults(includeResults));
        ValidationException.checkCondition(!changeInfo.isMoreChanges(), "Pagination is not supported yet.");
        return changeInfo;
    } catch (RepoException | ValidationException | RuntimeException e) {
        throw new EvalException("Error getting change: " + e.getMessage(), e);
    }
}
Also used : ValidationException(com.google.copybara.exception.ValidationException) ChangeInfo(com.google.copybara.git.gerritapi.ChangeInfo) RepoException(com.google.copybara.exception.RepoException) EvalException(net.starlark.java.eval.EvalException) StarlarkMethod(net.starlark.java.annot.StarlarkMethod)

Aggregations

StarlarkMethod (net.starlark.java.annot.StarlarkMethod)37 UsesFlags (com.google.copybara.doc.annotations.UsesFlags)17 DocDefault (com.google.copybara.doc.annotations.DocDefault)9 Checker (com.google.copybara.checks.Checker)8 Example (com.google.copybara.doc.annotations.Example)8 GeneralOptions (com.google.copybara.GeneralOptions)7 ValidationException (com.google.copybara.exception.ValidationException)7 Pattern (com.google.re2j.Pattern)6 SubmoduleStrategy (com.google.copybara.git.GitOrigin.SubmoduleStrategy)4 PatchTransformation (com.google.copybara.transform.patch.PatchTransformation)4 ImmutableList (com.google.common.collect.ImmutableList)3 LabelsAwareModule (com.google.copybara.config.LabelsAwareModule)3 SkylarkTransformation (com.google.copybara.transform.SkylarkTransformation)3 Transformations.toTransformation (com.google.copybara.transform.Transformations.toTransformation)3 IOException (java.io.IOException)3 Module (net.starlark.java.eval.Module)3 Action (com.google.copybara.action.Action)2 StarlarkAction (com.google.copybara.action.StarlarkAction)2 RepoException (com.google.copybara.exception.RepoException)2 FolderModule (com.google.copybara.folder.FolderModule)2