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);
}
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);
}
}
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);
}
}
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;
}
Aggregations