Search in sources :

Example 6 with ProjectConfigEntry

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));
            }
        }
    }
}
Also used : ConfigValue(com.google.gerrit.extensions.api.projects.ConfigValue) PluginConfig(com.google.gerrit.server.config.PluginConfig) ProjectConfigEntry(com.google.gerrit.server.config.ProjectConfigEntry) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) Map(java.util.Map) DynamicMap(com.google.gerrit.extensions.registration.DynamicMap)

Example 7 with ProjectConfigEntry

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));
            }
        }
    }
}
Also used : ConfigValue(com.google.gerrit.extensions.api.projects.ConfigValue) ProjectConfigEntry(com.google.gerrit.server.config.ProjectConfigEntry) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) Map(java.util.Map) DynamicMap(com.google.gerrit.extensions.registration.DynamicMap)

Example 8 with ProjectConfigEntry

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;
}
Also used : PluginConfig(com.google.gerrit.server.config.PluginConfig) ProjectConfigEntry(com.google.gerrit.server.config.ProjectConfigEntry) ProjectState(com.google.gerrit.server.project.ProjectState)

Example 9 with ProjectConfigEntry

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;
}
Also used : PluginConfig(com.google.gerrit.server.config.PluginConfig) ProjectConfigEntry(com.google.gerrit.server.config.ProjectConfigEntry) TreeMap(java.util.TreeMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) DynamicMap(com.google.gerrit.extensions.registration.DynamicMap) ConfigParameterInfo(com.google.gerrit.extensions.api.projects.ConfigInfo.ConfigParameterInfo)

Example 10 with ProjectConfigEntry

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();
    }
}
Also used : ProjectConfigEntry(com.google.gerrit.server.config.ProjectConfigEntry) Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) ConfigInfo(com.google.gerrit.extensions.api.projects.ConfigInfo) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Aggregations

ProjectConfigEntry (com.google.gerrit.server.config.ProjectConfigEntry)10 PluginConfig (com.google.gerrit.server.config.PluginConfig)7 DynamicMap (com.google.gerrit.extensions.registration.DynamicMap)4 Map (java.util.Map)4 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)3 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)2 Registration (com.google.gerrit.acceptance.ExtensionRegistry.Registration)2 ConfigInfo (com.google.gerrit.extensions.api.projects.ConfigInfo)2 ConfigValue (com.google.gerrit.extensions.api.projects.ConfigValue)2 LinkedHashMap (java.util.LinkedHashMap)2 TreeMap (java.util.TreeMap)2 Test (org.junit.Test)2 ConfigParameterInfo (com.google.gerrit.extensions.api.projects.ConfigInfo.ConfigParameterInfo)1 AuthException (com.google.gerrit.extensions.restapi.AuthException)1 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)1 RestApiException (com.google.gerrit.extensions.restapi.RestApiException)1 Change (com.google.gerrit.reviewdb.client.Change)1 Project (com.google.gerrit.reviewdb.client.Project)1 CommitValidationException (com.google.gerrit.server.git.validators.CommitValidationException)1 RefOperationValidationException (com.google.gerrit.server.git.validators.RefOperationValidationException)1