use of com.google.gerrit.server.config.ProjectConfigEntry in project gerrit by GerritCodeReview.
the class PutConfig method setPluginConfigValues.
private void setPluginConfigValues(ProjectState projectState, ProjectConfig projectConfig, Map<String, Map<String, ConfigValue>> pluginConfigValues) throws BadRequestException {
for (Entry<String, Map<String, ConfigValue>> e : pluginConfigValues.entrySet()) {
String pluginName = e.getKey();
PluginConfig cfg = projectConfig.getPluginConfig(pluginName);
for (Entry<String, ConfigValue> v : e.getValue().entrySet()) {
ProjectConfigEntry projectConfigEntry = pluginConfigEntries.get(pluginName, v.getKey());
if (projectConfigEntry != null) {
if (!isValidParameterName(v.getKey())) {
log.warn(String.format("Parameter name '%s' must match '^[a-zA-Z0-9]+[a-zA-Z0-9-]*$'", v.getKey()));
continue;
}
String oldValue = cfg.getString(v.getKey());
String value = v.getValue().value;
if (projectConfigEntry.getType() == ProjectConfigEntryType.ARRAY) {
List<String> l = Arrays.asList(cfg.getStringList(v.getKey()));
oldValue = Joiner.on("\n").join(l);
value = Joiner.on("\n").join(v.getValue().values);
}
if (Strings.emptyToNull(value) != null) {
if (!value.equals(oldValue)) {
validateProjectConfigEntryIsEditable(projectConfigEntry, projectState, v.getKey(), pluginName);
v.setValue(projectConfigEntry.preUpdate(v.getValue()));
value = v.getValue().value;
try {
switch(projectConfigEntry.getType()) {
case BOOLEAN:
boolean newBooleanValue = Boolean.parseBoolean(value);
cfg.setBoolean(v.getKey(), newBooleanValue);
break;
case INT:
int newIntValue = Integer.parseInt(value);
cfg.setInt(v.getKey(), newIntValue);
break;
case LONG:
long newLongValue = Long.parseLong(value);
cfg.setLong(v.getKey(), newLongValue);
break;
case LIST:
if (!projectConfigEntry.getPermittedValues().contains(value)) {
throw new BadRequestException(String.format("The value '%s' is not permitted for parameter '%s' of plugin '" + pluginName + "'", value, v.getKey()));
}
//$FALL-THROUGH$
case STRING:
cfg.setString(v.getKey(), value);
break;
case ARRAY:
cfg.setStringList(v.getKey(), v.getValue().values);
break;
default:
log.warn(String.format("The type '%s' of parameter '%s' is not supported.", projectConfigEntry.getType().name(), v.getKey()));
}
} catch (NumberFormatException ex) {
throw new BadRequestException(String.format("The value '%s' of config parameter '%s' of plugin '%s' is invalid: %s", v.getValue(), v.getKey(), pluginName, ex.getMessage()));
}
}
} else {
if (oldValue != null) {
validateProjectConfigEntryIsEditable(projectConfigEntry, projectState, v.getKey(), pluginName);
cfg.unset(v.getKey());
}
}
} else {
throw new BadRequestException(String.format("The config parameter '%s' of plugin '%s' does not exist.", v.getKey(), pluginName));
}
}
}
}
use of com.google.gerrit.server.config.ProjectConfigEntry in project gerrit by GerritCodeReview.
the class PutConfig method setPluginConfigValues.
private void setPluginConfigValues(ProjectState projectState, ProjectConfig projectConfig, Map<String, Map<String, ConfigValue>> pluginConfigValues) throws BadRequestException {
for (Map.Entry<String, Map<String, ConfigValue>> e : pluginConfigValues.entrySet()) {
String pluginName = e.getKey();
for (Map.Entry<String, ConfigValue> v : e.getValue().entrySet()) {
ProjectConfigEntry projectConfigEntry = pluginConfigEntries.get(pluginName, v.getKey());
if (projectConfigEntry != null) {
if (!PARAMETER_NAME_PATTERN.matcher(v.getKey()).matches()) {
// TODO check why we have this restriction
logger.atWarning().log("Parameter name '%s' must match '%s'", v.getKey(), PARAMETER_NAME_PATTERN.pattern());
continue;
}
String oldValue = projectConfig.getPluginConfig(pluginName).getString(v.getKey());
String value = v.getValue().value;
if (projectConfigEntry.getType() == ProjectConfigEntryType.ARRAY) {
List<String> l = Arrays.asList(projectConfig.getPluginConfig(pluginName).getStringList(v.getKey()));
oldValue = Joiner.on("\n").join(l);
value = Joiner.on("\n").join(v.getValue().values);
}
if (Strings.emptyToNull(value) != null) {
if (!value.equals(oldValue)) {
validateProjectConfigEntryIsEditable(projectConfigEntry, projectState, v.getKey(), pluginName);
v.setValue(projectConfigEntry.preUpdate(v.getValue()));
value = v.getValue().value;
try {
switch(projectConfigEntry.getType()) {
case BOOLEAN:
boolean newBooleanValue = Boolean.parseBoolean(value);
projectConfig.updatePluginConfig(pluginName, cfg -> cfg.setBoolean(v.getKey(), newBooleanValue));
break;
case INT:
int newIntValue = Integer.parseInt(value);
projectConfig.updatePluginConfig(pluginName, cfg -> cfg.setInt(v.getKey(), newIntValue));
break;
case LONG:
long newLongValue = Long.parseLong(value);
projectConfig.updatePluginConfig(pluginName, cfg -> cfg.setLong(v.getKey(), newLongValue));
break;
case LIST:
if (!projectConfigEntry.getPermittedValues().contains(value)) {
throw new BadRequestException(String.format("The value '%s' is not permitted for parameter '%s' of plugin '" + pluginName + "'", value, v.getKey()));
}
// $FALL-THROUGH$
case STRING:
String valueToSet = value;
projectConfig.updatePluginConfig(pluginName, cfg -> cfg.setString(v.getKey(), valueToSet));
break;
case ARRAY:
projectConfig.updatePluginConfig(pluginName, cfg -> cfg.setStringList(v.getKey(), v.getValue().values));
break;
default:
logger.atWarning().log("The type '%s' of parameter '%s' is not supported.", projectConfigEntry.getType().name(), v.getKey());
}
} catch (NumberFormatException ex) {
throw new BadRequestException(String.format("The value '%s' of config parameter '%s' of plugin '%s' is invalid: %s", v.getValue(), v.getKey(), pluginName, ex.getMessage()));
}
}
} else {
if (oldValue != null) {
validateProjectConfigEntryIsEditable(projectConfigEntry, projectState, v.getKey(), pluginName);
projectConfig.updatePluginConfig(pluginName, cfg -> cfg.unset(v.getKey()));
}
}
} else {
throw new BadRequestException(String.format("The config parameter '%s' of plugin '%s' does not exist.", v.getKey(), pluginName));
}
}
}
}
use of com.google.gerrit.server.config.ProjectConfigEntry in project gerrit by GerritCodeReview.
the class ConfigInfoCreator method getInheritedValue.
private static String getInheritedValue(ProjectState project, PluginConfigFactory cfgFactory, Extension<ProjectConfigEntry> e) {
ProjectConfigEntry configEntry = e.getProvider().get();
ProjectState parent = Iterables.getFirst(project.parents(), null);
String inheritedValue = configEntry.getDefaultValue();
if (parent != null) {
PluginConfig parentCfgWithInheritance = cfgFactory.getFromProjectConfigWithInheritance(parent, e.getPluginName());
inheritedValue = parentCfgWithInheritance.getString(e.getExportName(), configEntry.getDefaultValue());
}
return inheritedValue;
}
use of com.google.gerrit.server.config.ProjectConfigEntry in project gerrit by GerritCodeReview.
the class ConfigInfoCreator method getPluginConfig.
private static Map<String, Map<String, ConfigParameterInfo>> getPluginConfig(ProjectState project, DynamicMap<ProjectConfigEntry> pluginConfigEntries, PluginConfigFactory cfgFactory, AllProjectsName allProjects) {
TreeMap<String, Map<String, ConfigParameterInfo>> pluginConfig = new TreeMap<>();
for (Extension<ProjectConfigEntry> e : pluginConfigEntries) {
ProjectConfigEntry configEntry = e.getProvider().get();
PluginConfig cfg = cfgFactory.getFromProjectConfig(project, e.getPluginName());
String configuredValue = cfg.getString(e.getExportName());
ConfigParameterInfo p = new ConfigParameterInfo();
p.displayName = configEntry.getDisplayName();
p.description = configEntry.getDescription();
p.warning = configEntry.getWarning(project);
p.type = configEntry.getType();
p.permittedValues = configEntry.getPermittedValues();
p.editable = configEntry.isEditable(project) ? true : null;
if (configEntry.isInheritable() && !allProjects.equals(project.getNameKey())) {
PluginConfig cfgWithInheritance = cfgFactory.getFromProjectConfigWithInheritance(project, e.getPluginName());
p.inheritable = true;
p.value = configEntry.onRead(project, cfgWithInheritance.getString(e.getExportName(), configEntry.getDefaultValue()));
p.configuredValue = configuredValue;
p.inheritedValue = getInheritedValue(project, cfgFactory, e);
} else {
if (configEntry.getType() == ProjectConfigEntryType.ARRAY) {
p.values = configEntry.onRead(project, Arrays.asList(cfg.getStringList(e.getExportName())));
} else {
p.value = configEntry.onRead(project, configuredValue != null ? configuredValue : configEntry.getDefaultValue());
}
}
Map<String, ConfigParameterInfo> pc = pluginConfig.get(e.getPluginName());
if (pc == null) {
pc = new TreeMap<>();
pluginConfig.put(e.getPluginName(), pc);
}
pc.put(e.getExportName(), p);
}
return !pluginConfig.isEmpty() ? pluginConfig : null;
}
use of com.google.gerrit.server.config.ProjectConfigEntry in project gerrit by GerritCodeReview.
the class ProjectIT method pluginConfigsReturnedWhenRefsMetaConfigReadable.
@Test
public void pluginConfigsReturnedWhenRefsMetaConfigReadable() throws Exception {
ProjectConfigEntry entry = new ProjectConfigEntry("enabled", "true");
try (Registration ignored = extensionRegistry.newRegistration().add(entry, "test-config-entry")) {
// The admin can see refs/meta/config and hence has the READ_CONFIG permission.
requestScopeOperations.setApiUser(admin.id());
ConfigInfo configInfo = getConfig();
assertThat(configInfo.pluginConfig).isNotNull();
assertThat(configInfo.pluginConfig).isNotEmpty();
}
}
Aggregations