Merge "ChangeQueryProcessor: Add DynamicBean getter and setter"

This commit is contained in:
Martin Fick
2018-10-12 00:38:54 +00:00
committed by Gerrit Code Review
2 changed files with 51 additions and 5 deletions

View File

@@ -808,10 +808,12 @@ Plugins can provide additional attributes to be returned in Gerrit queries by
implementing the ChangeAttributeFactory interface and registering it to the
ChangeQueryProcessor.ChangeAttributeFactory class in the plugin module's
'configure()' method. The new attribute(s) will be output under a "plugin"
attribute in the change query output.
attribute in the change query output. This can be further controlled with an
option registered in the Http and Ssh modules' 'configure*()' methods.
The example below shows a plugin that adds two attributes ('exampleName' and
'changeValue'), to the change query output.
'changeValue'), to the change query output, when the query command is provided
the --myplugin-name--all option.
[source, java]
----
@@ -824,7 +826,31 @@ public class Module extends AbstractModule {
}
}
public class MyQueryOptions implements DynamicBean {
@Option(name = "--all", usage = "Include plugin output")
public boolean all = false;
}
public static class HttpModule extends HttpPluginModule {
@Override
protected void configureServlets() {
bind(DynamicBean.class)
.annotatedWith(Exports.named(QueryChanges.class))
.to(MyQueryOptions.class);
}
}
public static class SshModule extends PluginCommandModule {
@Override
protected void configureCommands() {
bind(DynamicBean.class)
.annotatedWith(Exports.named(Query.class))
.to(MyQueryOptions.class);
}
}
public class AttributeFactory implements ChangeAttributeFactory {
protected MyQueryOptions options;
public class PluginAttribute extends PluginDefinedInfo {
public String exampleName;
@@ -838,7 +864,13 @@ public class AttributeFactory implements ChangeAttributeFactory {
@Override
public PluginDefinedInfo create(ChangeData c, ChangeQueryProcessor qp, String plugin) {
return new PluginAttribute(c);
if (options == null) {
options = (MyQueryOptions) qp.getDynamicBean(plugin);
}
if (options.all) {
return new PluginAttribute(c);
}
return null;
}
}
----
@@ -846,7 +878,7 @@ public class AttributeFactory implements ChangeAttributeFactory {
Example
----
ssh -p 29418 localhost gerrit query "change:1" --format json
ssh -p 29418 localhost gerrit query --myplugin-name--all "change:1" --format json
Output:

View File

@@ -28,6 +28,8 @@ import com.google.gerrit.metrics.MetricMaker;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.AnonymousUser;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.DynamicOptions;
import com.google.gerrit.server.DynamicOptions.DynamicBean;
import com.google.gerrit.server.account.AccountLimits;
import com.google.gerrit.server.index.change.ChangeIndexCollection;
import com.google.gerrit.server.index.change.ChangeIndexRewriter;
@@ -39,7 +41,9 @@ import com.google.gerrit.server.project.ProjectCache;
import com.google.inject.Inject;
import com.google.inject.Provider;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
@@ -49,7 +53,7 @@ import java.util.Set;
* holding on to a single instance.
*/
public class ChangeQueryProcessor extends QueryProcessor<ChangeData>
implements PluginDefinedAttributesFactory {
implements DynamicOptions.BeanReceiver, PluginDefinedAttributesFactory {
/**
* Register a ChangeAttributeFactory in a config Module like this:
*
@@ -67,6 +71,7 @@ public class ChangeQueryProcessor extends QueryProcessor<ChangeData>
private final PermissionBackend permissionBackend;
private final ProjectCache projectCache;
private final Provider<AnonymousUser> anonymousUserProvider;
private final Map<String, DynamicBean> dynamicBeans = new HashMap<>();
static {
// It is assumed that basic rewrites do not touch visibleto predicates.
@@ -118,6 +123,15 @@ public class ChangeQueryProcessor extends QueryProcessor<ChangeData>
return IndexedChangeQuery.createOptions(indexConfig, start, limit, requestedFields);
}
@Override
public void setDynamicBean(String plugin, DynamicBean dynamicBean) {
dynamicBeans.put(plugin, dynamicBean);
}
public DynamicBean getDynamicBean(String plugin) {
return dynamicBeans.get(plugin);
}
@Override
public List<PluginDefinedInfo> create(ChangeData cd) {
List<PluginDefinedInfo> plugins = new ArrayList<>(attributeFactories.plugins().size());