use of com.google.gerrit.server.project.ProjectConfig in project gerrit by GerritCodeReview.
the class SetParent method apply.
public String apply(ProjectResource rsrc, ParentInput input, boolean checkIfAdmin) throws AuthException, ResourceConflictException, ResourceNotFoundException, UnprocessableEntityException, IOException, PermissionBackendException, BadRequestException {
IdentifiedUser user = rsrc.getUser().asIdentifiedUser();
String parentName = MoreObjects.firstNonNull(Strings.emptyToNull(input.parent), allProjects.get());
validateParentUpdate(rsrc.getProjectState().getNameKey(), user, parentName, checkIfAdmin);
try (MetaDataUpdate md = updateFactory.get().create(rsrc.getNameKey())) {
ProjectConfig config = projectConfigFactory.read(md);
config.updateProject(p -> p.setParent(parentName));
String msg = Strings.emptyToNull(input.commitMessage);
if (msg == null) {
msg = String.format("Changed parent to %s.\n", parentName);
} else if (!msg.endsWith("\n")) {
msg += "\n";
}
md.setAuthor(user);
md.setMessage(msg);
config.commit(md);
cache.evictAndReindex(rsrc.getProjectState().getProject());
Project.NameKey parent = config.getProject().getParent(allProjects);
requireNonNull(parent);
return parent.get();
} catch (RepositoryNotFoundException notFound) {
throw new ResourceNotFoundException(rsrc.getName(), notFound);
} catch (ConfigInvalidException e) {
throw new ResourceConflictException(String.format("invalid project.config: %s", e.getMessage()));
}
}
use of com.google.gerrit.server.project.ProjectConfig in project gerrit by GerritCodeReview.
the class GrantRevertPermission method execute.
public void execute(Project.NameKey projectName) throws IOException, ConfigInvalidException {
GroupReference registeredUsers = systemGroupBackend.getGroup(REGISTERED_USERS);
try (Repository repo = repoManager.openRepository(projectName)) {
MetaDataUpdate md = new MetaDataUpdate(GitReferenceUpdated.DISABLED, projectName, repo);
ProjectConfig projectConfig = projectConfigFactory.read(md);
AtomicBoolean shouldExit = new AtomicBoolean(false);
projectConfig.upsertAccessSection(AccessSection.HEADS, heads -> {
Permission permissionOnRefsHeads = heads.build().getPermission(Permission.REVERT);
if (permissionOnRefsHeads != null) {
if (permissionOnRefsHeads.getRule(registeredUsers) == null || permissionOnRefsHeads.getRules().size() > 1) {
// If admins already changed the permission, don't do anything.
shouldExit.set(true);
return;
}
// permission already exists in refs/heads/*, delete it for Registered Users.
remove(projectConfig, heads, Permission.REVERT, registeredUsers);
}
});
if (shouldExit.get()) {
return;
}
projectConfig.upsertAccessSection(AccessSection.ALL, all -> {
Permission permissionOnRefsStar = all.build().getPermission(Permission.REVERT);
if (permissionOnRefsStar != null && permissionOnRefsStar.getRule(registeredUsers) != null) {
// permission already exists in refs/*, don't do anything.
return;
}
// If the permission doesn't exist of refs/* for Registered Users, grant it.
grant(projectConfig, all, Permission.REVERT, registeredUsers);
});
md.getCommitBuilder().setAuthor(serverUser);
md.getCommitBuilder().setCommitter(serverUser);
md.setMessage("Add revert permission for all registered users\n");
projectConfig.commit(md);
}
}
use of com.google.gerrit.server.project.ProjectConfig in project gerrit by GerritCodeReview.
the class QueryChangesIT method skipVisibility_noReadPermission.
@Test
@SuppressWarnings("unchecked")
public void skipVisibility_noReadPermission() throws Exception {
createChange().getChangeId();
requestScopeOperations.setApiUser(admin.id());
QueryChanges queryChanges = queryChangesProvider.get();
queryChanges.addQuery("is:open repo:" + project.get());
List<List<ChangeInfo>> result = (List<List<ChangeInfo>>) queryChanges.apply(TopLevelResource.INSTANCE).value();
assertThat(result).hasSize(1);
try (ProjectConfigUpdate u = updateProject(allProjects)) {
ProjectConfig cfg = u.getConfig();
removeAllBranchPermissions(cfg, Permission.READ);
u.save();
}
queryChanges = queryChangesProvider.get();
queryChanges.addQuery("is:open repo:" + project.get());
List<List<ChangeInfo>> result2 = (List<List<ChangeInfo>>) queryChanges.apply(TopLevelResource.INSTANCE).value();
assertThat(result2).hasSize(0);
queryChanges = queryChangesProvider.get();
queryChanges.addQuery("is:open repo:" + project.get());
queryChanges.skipVisibility(true);
List<List<ChangeInfo>> result3 = (List<List<ChangeInfo>>) queryChanges.apply(TopLevelResource.INSTANCE).value();
assertThat(result3).hasSize(1);
}
use of com.google.gerrit.server.project.ProjectConfig in project gerrit by GerritCodeReview.
the class PluginConfig method withInheritance.
PluginConfig withInheritance(ProjectState.Factory projectStateFactory) {
checkState(projectConfig().isPresent(), "no project config provided");
ProjectState state = projectStateFactory.create(projectConfig().get());
ProjectState parent = Iterables.getFirst(state.parents(), null);
if (parent == null) {
return this;
}
Map<AccountGroup.UUID, GroupReference> groupReferences = new HashMap<>();
groupReferences.putAll(groupReferences());
PluginConfig parentPluginConfig = parent.getPluginConfig(pluginName()).withInheritance(projectStateFactory);
Set<String> allNames = cfg().getNames(PLUGIN, pluginName());
Config newCfg = copyConfig(cfg());
for (String name : parentPluginConfig.cfg().getNames(PLUGIN, pluginName())) {
if (!allNames.contains(name)) {
List<String> values = Arrays.asList(parentPluginConfig.cfg().getStringList(PLUGIN, pluginName(), name));
for (String value : values) {
Optional<GroupReference> groupRef = parentPluginConfig.projectConfig().get().getGroupByName(GroupReference.extractGroupName(value));
if (groupRef.isPresent()) {
groupReferences.putIfAbsent(groupRef.get().getUUID(), groupRef.get());
}
}
newCfg.setStringList(PLUGIN, pluginName(), name, values);
}
}
return new AutoValue_PluginConfig(pluginName(), newCfg, projectConfig(), ImmutableMap.copyOf(groupReferences));
}
use of com.google.gerrit.server.project.ProjectConfig in project gerrit by GerritCodeReview.
the class PostLabels method apply.
@Override
public Response<?> apply(ProjectResource rsrc, BatchLabelInput input) throws AuthException, UnprocessableEntityException, PermissionBackendException, IOException, ConfigInvalidException, BadRequestException, ResourceConflictException {
if (!user.get().isIdentifiedUser()) {
throw new AuthException("Authentication required");
}
permissionBackend.currentUser().project(rsrc.getNameKey()).check(ProjectPermission.WRITE_CONFIG);
if (input == null) {
input = new BatchLabelInput();
}
try (MetaDataUpdate md = updateFactory.create(rsrc.getNameKey())) {
boolean dirty = false;
ProjectConfig config = projectConfigFactory.read(md);
if (input.delete != null && !input.delete.isEmpty()) {
for (String labelName : input.delete) {
if (!deleteLabel.deleteLabel(config, labelName.trim())) {
throw new UnprocessableEntityException(String.format("label %s not found", labelName));
}
}
dirty = true;
}
if (input.create != null && !input.create.isEmpty()) {
for (LabelDefinitionInput labelInput : input.create) {
if (labelInput.name == null || labelInput.name.trim().isEmpty()) {
throw new BadRequestException("label name is required for new label");
}
if (labelInput.commitMessage != null) {
throw new BadRequestException("commit message on label definition input not supported");
}
createLabel.createLabel(config, labelInput.name.trim(), labelInput);
}
dirty = true;
}
if (input.update != null && !input.update.isEmpty()) {
for (Map.Entry<String, LabelDefinitionInput> e : input.update.entrySet()) {
LabelType labelType = config.getLabelSections().get(e.getKey().trim());
if (labelType == null) {
throw new UnprocessableEntityException(String.format("label %s not found", e.getKey()));
}
if (e.getValue().commitMessage != null) {
throw new BadRequestException("commit message on label definition input not supported");
}
setLabel.updateLabel(config, labelType, e.getValue());
}
dirty = true;
}
if (input.commitMessage != null) {
md.setMessage(Strings.emptyToNull(input.commitMessage.trim()));
} else {
md.setMessage("Update labels");
}
if (dirty) {
config.commit(md);
projectCache.evictAndReindex(rsrc.getProjectState().getProject());
}
}
return Response.ok("");
}
Aggregations