use of com.google.gerrit.server.project.ProjectState in project gerrit by GerritCodeReview.
the class GarbageCollectionCommand method runGC.
private void runGC() {
List<Project.NameKey> projectNames;
if (all) {
projectNames = Lists.newArrayList(projectCache.all());
} else {
projectNames = projects.stream().map(ProjectState::getNameKey).collect(toList());
}
GarbageCollectionResult result = garbageCollectionFactory.create().run(projectNames, aggressive, showProgress ? stdout : null);
if (result.hasErrors()) {
for (GcError e : result.getErrors()) {
String msg;
switch(e.getType()) {
case REPOSITORY_NOT_FOUND:
msg = "error: project \"" + e.getProjectName() + "\" not found";
break;
case GC_ALREADY_SCHEDULED:
msg = "error: garbage collection for project \"" + e.getProjectName() + "\" was already scheduled";
break;
case GC_FAILED:
msg = "error: garbage collection for project \"" + e.getProjectName() + "\" failed";
break;
default:
msg = "error: garbage collection for project \"" + e.getProjectName() + "\" failed: " + e.getType();
}
stdout.print(msg + "\n");
}
}
}
use of com.google.gerrit.server.project.ProjectState in project gerrit by GerritCodeReview.
the class SetParentCommand method run.
@Override
protected void run() throws Failure {
enableGracefulStop();
if (oldParent == null && children.isEmpty()) {
throw die("child projects have to be specified as " + "arguments or the --children-of option has to be set");
}
if (oldParent == null && !excludedChildren.isEmpty()) {
throw die("--exclude can only be used together with --children-of");
}
final StringBuilder err = new StringBuilder();
if (newParent != null) {
newParentKey = newParent.getProject().getNameKey();
}
final List<Project.NameKey> childProjects = children.stream().map(ProjectState::getNameKey).collect(toList());
if (oldParent != null) {
try {
childProjects.addAll(getChildrenForReparenting(oldParent));
} catch (PermissionBackendException e) {
throw new Failure(1, "permissions unavailable", e);
} catch (Exception e) {
throw new Failure(1, "failure in request", e);
}
}
for (Project.NameKey nameKey : childProjects) {
final String name = nameKey.get();
ProjectState project = projectCache.get(nameKey).orElseThrow(illegalState(nameKey));
try {
setParent.apply(new ProjectResource(project, user), parentInput(newParentKey.get()));
} catch (AuthException e) {
err.append("error: insuffient access rights to change parent of '").append(name).append("'\n");
} catch (ResourceConflictException | ResourceNotFoundException | BadRequestException e) {
err.append("error: ").append(e.getMessage()).append("'\n");
} catch (UnprocessableEntityException | IOException e) {
throw new Failure(1, "failure in request", e);
} catch (PermissionBackendException e) {
throw new Failure(1, "permissions unavailable", e);
}
}
if (err.length() > 0) {
while (err.charAt(err.length() - 1) == '\n') {
err.setLength(err.length() - 1);
}
throw die(err.toString());
}
}
use of com.google.gerrit.server.project.ProjectState in project gerrit by GerritCodeReview.
the class SetParent method validateParentUpdate.
public void validateParentUpdate(Project.NameKey project, IdentifiedUser user, String newParent, boolean checkIfAdmin) throws AuthException, ResourceConflictException, UnprocessableEntityException, PermissionBackendException, BadRequestException {
if (checkIfAdmin) {
if (allowProjectOwnersToChangeParent) {
permissionBackend.user(user).project(project).check(ProjectPermission.WRITE_CONFIG);
} else {
permissionBackend.user(user).check(GlobalPermission.ADMINISTRATE_SERVER);
}
}
if (project.equals(allUsers) && !allProjects.get().equals(newParent)) {
throw new BadRequestException(String.format("%s must inherit from %s", allUsers.get(), allProjects.get()));
}
if (project.equals(allProjects)) {
throw new ResourceConflictException("cannot set parent of " + allProjects.get());
}
if (allUsers.get().equals(newParent)) {
throw new ResourceConflictException(String.format("Cannot inherit from '%s' project", allUsers.get()));
}
newParent = Strings.emptyToNull(newParent);
if (newParent != null) {
Project.NameKey newParentNameKey = Project.nameKey(newParent);
ProjectState parent = cache.get(newParentNameKey).orElseThrow(() -> new UnprocessableEntityException("parent project " + newParentNameKey + " not found"));
if (parent.getName().equals(project.get())) {
throw new ResourceConflictException("cannot set parent to self");
}
if (Iterables.tryFind(parent.tree(), p -> p.getNameKey().equals(project)).isPresent()) {
throw new ResourceConflictException("cycle exists between " + project.get() + " and " + parent.getName());
}
}
}
use of com.google.gerrit.server.project.ProjectState in project gerrit by GerritCodeReview.
the class PutConfig method apply.
public ConfigInfo apply(ProjectState projectState, ConfigInput input) throws ResourceNotFoundException, BadRequestException, ResourceConflictException {
Project.NameKey projectName = projectState.getNameKey();
if (input == null) {
throw new BadRequestException("config is required");
}
try (MetaDataUpdate md = metaDataUpdateFactory.get().create(projectName)) {
ProjectConfig projectConfig = projectConfigFactory.read(md);
projectConfig.updateProject(p -> {
p.setDescription(Strings.emptyToNull(input.description));
for (BooleanProjectConfig cfg : BooleanProjectConfig.values()) {
InheritableBoolean val = BooleanProjectConfigTransformations.get(cfg, input);
if (val != null) {
p.setBooleanConfig(cfg, val);
}
}
if (input.maxObjectSizeLimit != null) {
p.setMaxObjectSizeLimit(input.maxObjectSizeLimit);
}
if (input.submitType != null) {
p.setSubmitType(input.submitType);
}
if (input.state != null) {
p.setState(input.state);
}
});
if (input.pluginConfigValues != null) {
setPluginConfigValues(projectState, projectConfig, input.pluginConfigValues);
}
if (input.commentLinks != null) {
updateCommentLinks(projectConfig, input.commentLinks);
}
md.setMessage("Modified project settings\n");
try {
projectConfig.commit(md);
projectCache.evictAndReindex(projectConfig.getProject());
md.getRepository().setGitwebDescription(projectConfig.getProject().getDescription());
} catch (IOException e) {
if (e.getCause() instanceof ConfigInvalidException) {
throw new ResourceConflictException("Cannot update " + projectName + ": " + e.getCause().getMessage());
}
logger.atWarning().withCause(e).log("Failed to update config of project %s.", projectName);
throw new ResourceConflictException("Cannot update " + projectName);
}
ProjectState state = projectStateFactory.create(projectConfigFactory.read(md).getCacheable());
return ConfigInfoCreator.constructInfo(serverEnableSignedPush, state, user.get(), pluginConfigEntries, cfgFactory, allProjects, uiActions, views);
} catch (RepositoryNotFoundException notFound) {
throw new ResourceNotFoundException(projectName.get(), notFound);
} catch (ConfigInvalidException err) {
throw new ResourceConflictException("Cannot read project " + projectName, err);
} catch (IOException err) {
throw new ResourceConflictException("Cannot update project " + projectName, err);
}
}
use of com.google.gerrit.server.project.ProjectState in project gerrit by GerritCodeReview.
the class PrologRuleEvaluator method runSubmitFilters.
private Term runSubmitFilters(Term results, PrologEnvironment env, String filterRuleLocatorName, String filterRuleWrapperName) throws RuleEvalException {
PrologEnvironment childEnv = env;
ChangeData cd = env.get(StoredValues.CHANGE_DATA);
ProjectState projectState = env.get(StoredValues.PROJECT_STATE);
for (ProjectState parentState : projectState.parents()) {
PrologEnvironment parentEnv;
try {
parentEnv = envFactory.create(rulesCache.loadMachine(parentState.getNameKey(), parentState.getConfig().getRulesId().orElse(null)));
} catch (CompileException err) {
throw new RuleEvalException("Cannot consult rules.pl for " + parentState.getName(), err);
}
parentEnv.copyStoredValues(childEnv);
Term filterRule = parentEnv.once("gerrit", filterRuleLocatorName, new VariableTerm());
try {
Term[] template = parentEnv.once("gerrit", filterRuleWrapperName, filterRule, results, new VariableTerm());
results = template[2];
} catch (ReductionLimitException err) {
throw new RuleEvalException(String.format("%s on change %d of %s", err.getMessage(), cd.getId().get(), parentState.getName()));
} catch (RuntimeException err) {
throw new RuleEvalException(String.format("Exception calling %s on change %d of %s", filterRule, cd.getId().get(), parentState.getName()), err);
}
childEnv = parentEnv;
}
return results;
}
Aggregations