Search in sources :

Example 51 with EvalException

use of com.google.devtools.build.lib.syntax.EvalException in project copybara by google.

the class RegexTemplateTokens method buildBefore.

/**
 * Converts this sequence of tokens into a regex which can be used to search a string. It
 * automatically quotes literals and represents interpolations as named groups.
 *
 * <p>It also fills groupIndexes with all the interpolation locations.
 *
 * @param regexesByInterpolationName map from group name to the regex to interpolate when the
 * group is mentioned
 * @param repeatedGroups true if a regex group is allowed to be used multiple times
 */
private Pattern buildBefore(Map<String, Pattern> regexesByInterpolationName, boolean repeatedGroups) throws EvalException {
    int groupCount = 1;
    StringBuilder fullPattern = new StringBuilder();
    for (Token token : tokens) {
        switch(token.getType()) {
            case INTERPOLATION:
                Pattern subPattern = regexesByInterpolationName.get(token.getValue());
                if (subPattern == null) {
                    throw new EvalException(location, "Interpolation is used but not defined: " + token.getValue());
                }
                fullPattern.append(String.format("(%s)", subPattern.pattern()));
                if (groupIndexes.get(token.getValue()).size() > 0 && !repeatedGroups) {
                    throw new EvalException(location, "Regex group is used in template multiple times: " + token.getValue());
                }
                groupIndexes.put(token.getValue(), groupCount);
                groupCount += subPattern.groupCount() + 1;
                break;
            case LITERAL:
                fullPattern.append(Pattern.quote(token.getValue()));
                break;
            default:
                throw new IllegalStateException(token.getType().toString());
        }
    }
    return Pattern.compile(fullPattern.toString(), Pattern.MULTILINE);
}
Also used : Pattern(com.google.re2j.Pattern) Token(com.google.copybara.templatetoken.Token) EvalException(com.google.devtools.build.lib.syntax.EvalException)

Example 52 with EvalException

use of com.google.devtools.build.lib.syntax.EvalException in project copybara by google.

the class RestoreOriginalAuthor method transform.

@Override
public void transform(TransformWork work) throws IOException, ValidationException {
    Author author = null;
    // last author wins.
    for (Change<?> change : work.getChanges().getCurrent()) {
        ImmutableCollection<String> labelValue = change.getLabels().get(label);
        if (!labelValue.isEmpty()) {
            try {
                author = Author.parse(/*location=*/
                null, Iterables.getLast(labelValue));
            } catch (EvalException e) {
                // Don't fail the migration because the label is wrong since it is very
                // difficult for a user to recover from this.
                work.getConsole().warn("Cannot restore original author: " + e.getMessage());
            }
        }
        if (!searchAllChanges) {
            break;
        }
    }
    if (author != null) {
        work.setAuthor(author);
        work.removeLabel(label, /*wholeMessage=*/
        true);
    }
}
Also used : Author(com.google.copybara.authoring.Author) EvalException(com.google.devtools.build.lib.syntax.EvalException)

Example 53 with EvalException

use of com.google.devtools.build.lib.syntax.EvalException in project copybara by google.

the class GitHubEndPoint method createStatus.

@SkylarkCallable(name = "create_status", doc = "Create or update a status for a commit. Returns the status created.", parameters = { @Param(name = "sha", type = String.class, named = true, doc = "The SHA-1 for which we want to create or update the status"), @Param(name = "state", type = String.class, named = true, doc = "The state of the commit status: 'success', 'error', 'pending' or 'failure'"), @Param(name = "context", type = String.class, doc = "The context for the commit" + " status. Use a value like 'copybara/import_successful' or similar", named = true), @Param(name = "description", type = String.class, named = true, doc = "Description about what happened"), @Param(name = "target_url", type = String.class, named = true, doc = "Url with expanded information about the event", noneable = true, defaultValue = "None") })
public Status createStatus(String sha, String state, String context, String description, Object targetUrl) throws EvalException {
    try {
        checkCondition(State.VALID_VALUES.contains(state), "Invalid value for state. Valid values: %s", State.VALID_VALUES);
        checkCondition(GitRevision.COMPLETE_SHA1_PATTERN.matcher(sha).matches(), "Not a valid complete SHA-1: %s", sha);
        checkCondition(!Strings.isNullOrEmpty(description), "description cannot be empty");
        checkCondition(!Strings.isNullOrEmpty(context), "context cannot be empty");
        String project = GithubUtil.getProjectNameFromUrl(url);
        return githubOptions.getApi(project).createStatus(project, sha, new CreateStatusRequest(State.valueOf(state.toUpperCase()), convertFromNoneable(targetUrl, null), description, context));
    } catch (RepoException | ValidationException e) {
        throw new EvalException(/*location=*/
        null, e);
    }
}
Also used : CreateStatusRequest(com.google.copybara.git.github.api.CreateStatusRequest) ValidationException(com.google.copybara.exception.ValidationException) RepoException(com.google.copybara.exception.RepoException) EvalException(com.google.devtools.build.lib.syntax.EvalException) SkylarkCallable(com.google.devtools.build.lib.skylarkinterface.SkylarkCallable)

Example 54 with EvalException

use of com.google.devtools.build.lib.syntax.EvalException in project copybara by google.

the class Core method getChangeIdentity.

private static ImmutableList<Token> getChangeIdentity(Object changeIdentityObj, Location location) throws EvalException {
    String changeIdentity = SkylarkUtil.convertFromNoneable(changeIdentityObj, null);
    if (changeIdentity == null) {
        return ImmutableList.of();
    }
    ImmutableList<Token> result = new Parser(location).parse(changeIdentity);
    boolean configVarFound = false;
    for (Token token : result) {
        if (token.getType() != TokenType.INTERPOLATION) {
            continue;
        }
        if (token.getValue().equals(Workflow.COPYBARA_CONFIG_PATH_IDENTITY_VAR)) {
            configVarFound = true;
            continue;
        }
        if (token.getValue().equals(Workflow.COPYBARA_WORKFLOW_NAME_IDENTITY_VAR) || token.getValue().equals(Workflow.COPYBARA_REFERENCE_IDENTITY_VAR) || token.getValue().startsWith(Workflow.COPYBARA_REFERENCE_LABEL_VAR)) {
            continue;
        }
        throw new EvalException(location, String.format("Unrecognized variable: %s", token.getValue()));
    }
    if (!configVarFound) {
        throw new EvalException(location, String.format("${%s} variable is required", Workflow.COPYBARA_CONFIG_PATH_IDENTITY_VAR));
    }
    return result;
}
Also used : Token(com.google.copybara.templatetoken.Token) EvalException(com.google.devtools.build.lib.syntax.EvalException) Parser(com.google.copybara.templatetoken.Parser)

Aggregations

EvalException (com.google.devtools.build.lib.syntax.EvalException)54 IOException (java.io.IOException)15 ImmutableMap (com.google.common.collect.ImmutableMap)9 WorkspaceAttributeMapper (com.google.devtools.build.lib.rules.repository.WorkspaceAttributeMapper)9 ClassObject (com.google.devtools.build.lib.syntax.ClassObject)9 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)9 Label (com.google.devtools.build.lib.cmdline.Label)8 Path (com.google.devtools.build.lib.vfs.Path)8 Map (java.util.Map)8 SkylarkClassObject (com.google.devtools.build.lib.packages.SkylarkClassObject)7 Environment (com.google.devtools.build.lib.syntax.Environment)7 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)6 RepositoryFunctionException (com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException)6 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)6 SkyKey (com.google.devtools.build.skyframe.SkyKey)6 Nullable (javax.annotation.Nullable)6 FileValue (com.google.devtools.build.lib.skyframe.FileValue)5 ImmutableList (com.google.common.collect.ImmutableList)4 Artifact (com.google.devtools.build.lib.actions.Artifact)4 HashMap (java.util.HashMap)4