Search in sources :

Example 1 with ConfigValue

use of com.google.gerrit.extensions.api.projects.ConfigValue in project gerrit by GerritCodeReview.

the class CreateProjectCommand method parsePluginConfigValues.

@VisibleForTesting
Map<String, Map<String, ConfigValue>> parsePluginConfigValues(List<String> pluginConfigValues) throws UnloggedFailure {
    Map<String, Map<String, ConfigValue>> m = new HashMap<>();
    for (String pluginConfigValue : pluginConfigValues) {
        String[] s = pluginConfigValue.split("=");
        String[] s2 = s[0].split("\\.");
        if (s.length != 2 || s2.length != 2) {
            throw die("Invalid plugin config value '" + pluginConfigValue + "', expected format '<plugin-name>.<parameter-name>=<value>'" + " or '<plugin-name>.<parameter-name>=<value1,value2,...>'");
        }
        ConfigValue value = new ConfigValue();
        String v = s[1];
        if (v.contains(",")) {
            value.values = Lists.newArrayList(Splitter.on(",").split(v));
        } else {
            value.value = v;
        }
        String pluginName = s2[0];
        String paramName = s2[1];
        Map<String, ConfigValue> l = m.get(pluginName);
        if (l == null) {
            l = new HashMap<>();
            m.put(pluginName, l);
        }
        l.put(paramName, value);
    }
    return m;
}
Also used : ConfigValue(com.google.gerrit.extensions.api.projects.ConfigValue) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with ConfigValue

use of com.google.gerrit.extensions.api.projects.ConfigValue 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 3 with ConfigValue

use of com.google.gerrit.extensions.api.projects.ConfigValue in project gerrit by GerritCodeReview.

the class ProjectConfigParamParserTest method parseMultipleValue.

@Test
public void parseMultipleValue() throws Exception {
    String in = "a.b=c,d,e";
    Map<String, Map<String, ConfigValue>> r = cmd.parsePluginConfigValues(Collections.singletonList(in));
    ConfigValue configValue = r.get("a").get("b");
    assertThat(configValue.values).containsExactly("c", "d", "e").inOrder();
    assertThat(configValue.value).isNull();
}
Also used : ConfigValue(com.google.gerrit.extensions.api.projects.ConfigValue) Map(java.util.Map) Test(org.junit.Test)

Example 4 with ConfigValue

use of com.google.gerrit.extensions.api.projects.ConfigValue in project gerrit by GerritCodeReview.

the class ProjectConfigParamParserTest method parseSingleValue.

@Test
public void parseSingleValue() throws Exception {
    String in = "a.b=c";
    Map<String, Map<String, ConfigValue>> r = cmd.parsePluginConfigValues(Collections.singletonList(in));
    ConfigValue configValue = r.get("a").get("b");
    assertThat(configValue.value).isEqualTo("c");
    assertThat(configValue.values).isNull();
}
Also used : ConfigValue(com.google.gerrit.extensions.api.projects.ConfigValue) Map(java.util.Map) Test(org.junit.Test)

Aggregations

ConfigValue (com.google.gerrit.extensions.api.projects.ConfigValue)4 Map (java.util.Map)4 Test (org.junit.Test)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 DynamicMap (com.google.gerrit.extensions.registration.DynamicMap)1 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)1 PluginConfig (com.google.gerrit.server.config.PluginConfig)1 ProjectConfigEntry (com.google.gerrit.server.config.ProjectConfigEntry)1 HashMap (java.util.HashMap)1