use of com.google.gerrit.server.git.ValidationError in project gerrit by GerritCodeReview.
the class ReceiveCommits method validateConfigPush.
/**
* Validates a push to refs/meta/config, and reject the command if it fails.
*/
private void validateConfigPush(ReceiveCommand cmd) throws PermissionBackendException {
try (TraceTimer traceTimer = newTimer("validateConfigPush")) {
logger.atFine().log("Processing %s command", cmd.getRefName());
try {
permissions.check(ProjectPermission.WRITE_CONFIG);
} catch (AuthException e) {
reject(cmd, String.format("must be either project owner or have %s permission", ProjectPermission.WRITE_CONFIG.describeForException()));
return;
}
switch(cmd.getType()) {
case CREATE:
case UPDATE:
case UPDATE_NONFASTFORWARD:
try {
ProjectConfig cfg = projectConfigFactory.create(project.getNameKey());
cfg.load(project.getNameKey(), receivePack.getRevWalk(), cmd.getNewId());
if (!cfg.getValidationErrors().isEmpty()) {
addError("Invalid project configuration:");
for (ValidationError err : cfg.getValidationErrors()) {
addError(" " + err.getMessage());
}
reject(cmd, "invalid project configuration");
logger.atSevere().log("User %s tried to push invalid project configuration %s for %s", user.getLoggableName(), cmd.getNewId().name(), project.getName());
return;
}
Project.NameKey newParent = cfg.getProject().getParent(allProjectsName);
Project.NameKey oldParent = project.getParent(allProjectsName);
if (oldParent == null) {
// update of the 'All-Projects' project
if (newParent != null) {
reject(cmd, "invalid project configuration: root project cannot have parent");
return;
}
} else {
if (!oldParent.equals(newParent)) {
if (allowProjectOwnersToChangeParent) {
try {
permissionBackend.user(user).project(project.getNameKey()).check(ProjectPermission.WRITE_CONFIG);
} catch (AuthException e) {
reject(cmd, "invalid project configuration: only project owners can set parent");
return;
}
} else {
try {
permissionBackend.user(user).check(GlobalPermission.ADMINISTRATE_SERVER);
} catch (AuthException e) {
reject(cmd, "invalid project configuration: only Gerrit admin can set parent");
return;
}
}
}
if (!projectCache.get(newParent).isPresent()) {
reject(cmd, "invalid project configuration: parent does not exist");
return;
}
}
validatePluginConfig(cmd, cfg);
} catch (Exception e) {
reject(cmd, "invalid project configuration");
logger.atSevere().withCause(e).log("User %s tried to push invalid project configuration %s for %s", user.getLoggableName(), cmd.getNewId().name(), project.getName());
return;
}
break;
case DELETE:
break;
default:
reject(cmd, "prohibited by Gerrit: don't know how to handle config update of type " + cmd.getType());
}
}
}
use of com.google.gerrit.server.git.ValidationError in project gerrit by GerritCodeReview.
the class AccountValidator method loadAccount.
private Optional<Account> loadAccount(Account.Id accountId, Repository allUsersRepo, RevWalk rw, ObjectId commit, @Nullable ImmutableList.Builder<String> messages) throws IOException, ConfigInvalidException {
rw.reset();
AccountConfig accountConfig = new AccountConfig(accountId, allUsersName, allUsersRepo);
accountConfig.load(allUsersName, rw, commit);
if (messages != null) {
messages.addAll(accountConfig.getValidationErrors().stream().map(ValidationError::getMessage).collect(toSet()));
}
return accountConfig.getLoadedAccount();
}
Aggregations