use of com.google.copybara.exception.ValidationException in project copybara by google.
the class MigrateCmd method loadConfig.
private Config loadConfig(Options options, ConfigLoader configLoader, String migrationName) throws IOException, ValidationException {
GeneralOptions generalOptions = options.get(GeneralOptions.class);
Console console = generalOptions.console();
Config config = configLoader.load(console);
console.progress("Validating configuration");
ValidationResult result = configValidator.validate(config, migrationName);
if (!result.hasErrors()) {
return config;
}
result.getErrors().forEach(console::error);
console.error("Configuration is invalid.");
throw new ValidationException("Error validating configuration: Configuration is invalid.");
}
use of com.google.copybara.exception.ValidationException in project copybara by google.
the class ValidateCmd method validate.
/**
* Validates that the configuration is correct and that there is a valid migration specified by
* {@code migrationName}.
*
* <p>Note that, besides validating the specific migration, all the configuration will be
* validated syntactically.
*
* Returns true iff this configuration is valid.
*/
private ValidationResult validate(Options options, ConfigLoader configLoader, String migrationName) throws IOException {
Console console = options.get(GeneralOptions.class).console();
ValidationResult.Builder resultBuilder = new ValidationResult.Builder();
try {
Config config = configLoader.load(console);
resultBuilder.append(configValidator.validate(config, migrationName));
} catch (ValidationException e) {
// The validate subcommand should not throw Validation exceptions but log a result
StringBuilder error = new StringBuilder(e.getMessage()).append("\n");
Throwable cause = e.getCause();
while (cause != null) {
error.append(" CAUSED BY: ").append(cause.getMessage()).append("\n");
cause = cause.getCause();
}
resultBuilder.error(error.toString());
}
return resultBuilder.build();
}
use of com.google.copybara.exception.ValidationException in project copybara by google.
the class GitHubEndPoint method getPullRequests.
@StarlarkMethod(name = "get_pull_requests", doc = "Get Pull Requests for a repo", parameters = { @Param(name = "head_prefix", allowedTypes = { @ParamType(type = String.class), @ParamType(type = NoneType.class) }, named = true, doc = "Only return PRs wher the branch name has head_prefix", defaultValue = "None"), @Param(name = "base_prefix", allowedTypes = { @ParamType(type = String.class), @ParamType(type = NoneType.class) }, named = true, doc = "Only return PRs where the destination branch name has base_prefix", defaultValue = "None"), @Param(name = "state", doc = "State of the Pull Request. Can be `\"OPEN\"`, `\"CLOSED\"` or `\"ALL\"`", defaultValue = "\"OPEN\"", named = true), @Param(name = "sort", doc = "Sort filter for retrieving the Pull Requests. Can be `\"CREATED\"`," + " `\"UPDATED\"` or `\"POPULARITY\"`", named = true, defaultValue = "\"CREATED\""), @Param(name = "direction", doc = "Direction of the filter. Can be `\"ASC\"` or `\"DESC\"`", defaultValue = "\"ASC\"", named = true) }, allowReturnNones = true)
@Nullable
public ImmutableList<PullRequest> getPullRequests(Object headPrefixParam, Object basePrefixParam, String state, String sort, String direction) throws EvalException, RepoException {
try {
String project = ghHost.getProjectNameFromUrl(url);
PullRequestListParams request = PullRequestListParams.DEFAULT;
String headPrefix = convertFromNoneable(headPrefixParam, null);
String basePrefix = convertFromNoneable(basePrefixParam, null);
if (!Strings.isNullOrEmpty(headPrefix)) {
checkCondition(SAFE_BRANCH_NAME_PREFIX.matches(headPrefix), "'%s' is not a valid head_prefix (%s is used for validation)", headPrefix, SAFE_BRANCH_NAME_PREFIX.pattern());
request = request.withHead(headPrefix);
}
if (!Strings.isNullOrEmpty(basePrefix)) {
checkCondition(SAFE_BRANCH_NAME_PREFIX.matches(basePrefix), "'%s' is not a valid base_prefix (%s is used for validation)", basePrefix, SAFE_BRANCH_NAME_PREFIX.pattern());
request = request.withHead(basePrefix);
}
return apiSupplier.load(console).getPullRequests(project, request.withState(stringToEnum("state", state, StateFilter.class)).withDirection(stringToEnum("direction", direction, DirectionFilter.class)).withSort(stringToEnum("sort", sort, SortFilter.class)));
} catch (GitHubApiException e) {
return returnNullOnNotFound(e);
} catch (ValidationException | RuntimeException e) {
throw Starlark.errorf("Error calling get_pull_requests: %s", e.getMessage());
}
}
use of com.google.copybara.exception.ValidationException in project copybara by google.
the class GitHubEndPoint method updateReference.
@StarlarkMethod(name = "update_reference", doc = "Update a reference to point to a new commit. Returns the info of the reference.", parameters = { @Param(name = "ref", named = true, doc = "The name of the reference."), @Param(name = "sha", doc = "The id for the commit" + " status.", named = true), @Param(name = "force", named = true, doc = "Indicates whether to force the update or to make sure the update is a" + " fast-forward update. Leaving this out or setting it to false will make" + " sure you're not overwriting work. Default: false") })
public Ref updateReference(String sha, String ref, boolean force) throws EvalException, RepoException {
try {
checkCondition(GitRevision.COMPLETE_SHA1_PATTERN.matcher(sha).matches(), "Not a valid complete SHA-1: %s", sha);
checkCondition(!Strings.isNullOrEmpty(ref), "ref cannot be empty");
if (!ref.startsWith("refs/")) {
// TODO(malcon): Remove this functionality and use a check once library migrated.
console.warnFmt("Non-complete ref passed to update_reference '%s'. Assuming refs/heads/%s", ref, ref);
ref = "refs/heads/" + ref;
}
String project = ghHost.getProjectNameFromUrl(url);
return apiSupplier.load(console).updateReference(project, ref, new UpdateReferenceRequest(sha, force));
} catch (ValidationException | RuntimeException e) {
throw Starlark.errorf("Error calling update_reference: %s", e.getMessage());
}
}
use of com.google.copybara.exception.ValidationException in project copybara by google.
the class GitHubPrOrigin method checkRequiredCheckRuns.
/**
* Check that the PR has a conclusion of "success" for each check_run whose name is in the list
* provided in the `required_check_runs` param
*/
private void checkRequiredCheckRuns(GitHubApi api, String project, PullRequest prData) throws ValidationException, RepoException {
Set<String> requiredCheckRuns = getRequiredCheckRuns();
if (forceImport() || requiredCheckRuns.isEmpty()) {
return;
}
try (ProfilerTask ignore = generalOptions.profiler().start("github_api_get_combined_status")) {
CheckRuns checkRuns = api.getCheckRuns(project, prData.getHead().getSha());
Set<String> requiredButNotPresent = Sets.newHashSet(requiredCheckRuns);
List<CheckRun> passedCheckRuns = checkRuns.getCheckRuns().stream().filter(e -> e.getConclusion().equals("success")).collect(Collectors.toList());
requiredButNotPresent.removeAll(Collections2.transform(passedCheckRuns, CheckRun::getName));
if (!requiredButNotPresent.isEmpty()) {
throw new EmptyChangeException(String.format("Cannot migrate http://github.com/%s/pull/%d because the following check runs " + "have not been passed: %s", project, prData.getNumber(), requiredButNotPresent));
}
}
}
Aggregations