Merge "Support to inherit project level plugin configuration"

This commit is contained in:
Edwin Kempin
2013-09-25 08:00:05 +00:00
committed by Gerrit Code Review
4 changed files with 60 additions and 3 deletions

View File

@@ -401,6 +401,20 @@ is no need for a plugin to parse the `project.config` file on its own:
.getBoolean("enabled", false); .getBoolean("enabled", false);
---- ----
It is also possible to get missing configuration parameters inherited
from the parent projects:
[source,java]
----
@Inject
private com.google.gerrit.server.config.PluginConfigFactory cfg;
...
boolean enabled = cfg.getWithInheritance(project, "helloworld")
.getBoolean("enabled", false);
----
Project owners can edit the project configuration by fetching the Project owners can edit the project configuration by fetching the
`refs/meta/config` branch, editing the `project.config` file and `refs/meta/config` branch, editing the `project.config` file and
pushing the commit back. pushing the commit back.

View File

@@ -15,18 +15,53 @@
package com.google.gerrit.server.config; package com.google.gerrit.server.config;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.common.collect.Iterables;
import com.google.gerrit.server.git.ProjectConfig;
import com.google.gerrit.server.project.ProjectState;
import org.eclipse.jgit.lib.Config; import org.eclipse.jgit.lib.Config;
import java.util.Arrays;
import java.util.Set;
public class PluginConfig { public class PluginConfig {
private static final String PLUGIN = "plugin"; private static final String PLUGIN = "plugin";
private final String pluginName; private final String pluginName;
private final Config cfg; private Config cfg;
private final ProjectConfig projectConfig;
public PluginConfig(String pluginName, Config cfg) { public PluginConfig(String pluginName, Config cfg) {
this(pluginName, cfg, null);
}
public PluginConfig(String pluginName, Config cfg, ProjectConfig projectConfig) {
this.pluginName = pluginName; this.pluginName = pluginName;
this.cfg = cfg; this.cfg = cfg;
this.projectConfig = projectConfig;
}
PluginConfig withInheritance(ProjectState.Factory projectStateFactory) {
if (projectConfig == null) {
return this;
}
ProjectState state = projectStateFactory.create(projectConfig);
ProjectState parent = Iterables.getFirst(state.parents(), null);
if (parent != null) {
PluginConfig parentPluginConfig =
parent.getConfig().getPluginConfig(pluginName)
.withInheritance(projectStateFactory);
Set<String> allNames = cfg.getNames(PLUGIN, pluginName);
cfg = new Config(cfg);
for (String name : parentPluginConfig.cfg.getNames(PLUGIN, pluginName)) {
if (!allNames.contains(name)) {
cfg.setStringList(PLUGIN, pluginName, name, Arrays
.asList(parentPluginConfig.cfg.getStringList(PLUGIN, pluginName, name)));
}
}
}
return this;
} }
public String getString(String name) { public String getString(String name) {

View File

@@ -27,11 +27,14 @@ import org.eclipse.jgit.lib.Config;
public class PluginConfigFactory { public class PluginConfigFactory {
private final Config cfg; private final Config cfg;
private final ProjectCache projectCache; private final ProjectCache projectCache;
private final ProjectState.Factory projectStateFactory;
@Inject @Inject
PluginConfigFactory(@GerritServerConfig Config cfg, ProjectCache projectCache) { PluginConfigFactory(@GerritServerConfig Config cfg,
ProjectCache projectCache, ProjectState.Factory projectStateFactory) {
this.cfg = cfg; this.cfg = cfg;
this.projectCache = projectCache; this.projectCache = projectCache;
this.projectStateFactory = projectStateFactory;
} }
public PluginConfig get(String pluginName) { public PluginConfig get(String pluginName) {
@@ -46,4 +49,9 @@ public class PluginConfigFactory {
} }
return projectState.getConfig().getPluginConfig(pluginName); return projectState.getConfig().getPluginConfig(pluginName);
} }
public PluginConfig getWithInheritance(Project.NameKey projectName,
String pluginName) throws NoSuchProjectException {
return get(projectName, pluginName).withInheritance(projectStateFactory);
}
} }

View File

@@ -707,7 +707,7 @@ public class ProjectConfig extends VersionedMetaData {
pluginConfig = new Config(); pluginConfig = new Config();
pluginConfigs.put(pluginName, pluginConfig); pluginConfigs.put(pluginName, pluginConfig);
} }
return new PluginConfig(pluginName, pluginConfig); return new PluginConfig(pluginName, pluginConfig, this);
} }
private Map<String, GroupReference> readGroupList() throws IOException { private Map<String, GroupReference> readGroupList() throws IOException {