SubmoduleOp: Move subscription graph instantiation to its own factory

Subscription graph has a factory, but there is an extra layer of
indirection to take config into account. This indirection is inside
submoduleOp. SubmoduleOp needs the subscription graph but shouldn't
control its instantiation.

Move this to an extra Factory, so clients can instantiate their
subscription graph without depending on submoduleOp.

In guice, bind the unnannotated Subscription.Factory to the configured
factory and annotate the "vanilla" version.

Change-Id: Ie1ab3bfaebbe0480a5e4f93bbb5796b5e6a8e740
This commit is contained in:
Ivan Frade
2020-09-17 15:52:30 -07:00
parent f2a511895a
commit 8c05f63809
5 changed files with 92 additions and 18 deletions

View File

@@ -187,9 +187,11 @@ import com.google.gerrit.server.rules.PrologModule;
import com.google.gerrit.server.rules.RulesCache;
import com.google.gerrit.server.rules.SubmitRule;
import com.google.gerrit.server.ssh.SshAddressesModule;
import com.google.gerrit.server.submit.ConfiguredSubscriptionGraphFactory;
import com.google.gerrit.server.submit.GitModules;
import com.google.gerrit.server.submit.MergeSuperSetComputation;
import com.google.gerrit.server.submit.SubmitStrategy;
import com.google.gerrit.server.submit.SubscriptionGraph;
import com.google.gerrit.server.tools.ToolsCatalog;
import com.google.gerrit.server.update.BatchUpdate;
import com.google.gerrit.server.util.IdGenerator;
@@ -453,6 +455,7 @@ public class GerritGlobalModule extends FactoryModule {
factory(VersionedAuthorizedKeys.Factory.class);
bind(AccountManager.class);
bind(SubscriptionGraph.Factory.class).to(ConfiguredSubscriptionGraphFactory.class);
bind(new TypeLiteral<List<CommentLinkInfo>>() {}).toProvider(CommentLinkProvider.class);
DynamicSet.bind(binder(), GerritConfigListener.class).to(CommentLinkProvider.class);

View File

@@ -0,0 +1,54 @@
// Copyright (C) 2020 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.submit;
import com.google.common.collect.ImmutableSet;
import com.google.common.flogger.FluentLogger;
import com.google.gerrit.entities.BranchNameKey;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.inject.Inject;
import java.util.Set;
import org.eclipse.jgit.lib.Config;
/**
* Wrap a (@link {@link SubscriptionGraph.Factory} to honor the gerrit configuration.
*
* <p>If superproject subscriptions are disabled in the conf, return an empty graph.
*/
public class ConfiguredSubscriptionGraphFactory implements SubscriptionGraph.Factory {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private final SubscriptionGraph.Factory subscriptionGraphFactory;
private final Config cfg;
@Inject
ConfiguredSubscriptionGraphFactory(
@VanillaSubscriptionGraph SubscriptionGraph.Factory subscriptionGraphFactory,
@GerritServerConfig Config cfg) {
this.subscriptionGraphFactory = subscriptionGraphFactory;
this.cfg = cfg;
}
@Override
public SubscriptionGraph compute(Set<BranchNameKey> updatedBranches, MergeOpRepoManager orm)
throws SubmoduleConflictException {
if (cfg.getBoolean("submodule", "enableSuperProjectSubscriptions", true)) {
return subscriptionGraphFactory.compute(updatedBranches, orm);
} else {
logger.atFine().log("Updating superprojects disabled");
return SubscriptionGraph.createEmptyGraph(ImmutableSet.copyOf(updatedBranches));
}
}
}

View File

@@ -15,13 +15,11 @@
package com.google.gerrit.server.submit;
import com.google.common.collect.ImmutableSet;
import com.google.common.flogger.FluentLogger;
import com.google.gerrit.common.UsedAt;
import com.google.gerrit.entities.BranchNameKey;
import com.google.gerrit.entities.Project;
import com.google.gerrit.exceptions.StorageException;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.gerrit.server.project.NoSuchProjectException;
import com.google.gerrit.server.submit.MergeOpRepoManager.OpenRepo;
import com.google.gerrit.server.update.BatchUpdate;
@@ -32,38 +30,28 @@ import com.google.inject.Singleton;
import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.Set;
import org.eclipse.jgit.lib.Config;
public class SubmoduleOp {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
@Singleton
public static class Factory {
private final SubscriptionGraph.Factory subscriptionGraphFactory;
private final Config cfg;
private final SubmoduleCommits.Factory submoduleCommitsFactory;
@Inject
Factory(
SubscriptionGraph.Factory subscriptionGraphFactory,
SubmoduleCommits.Factory submoduleCommitsFactory,
@GerritServerConfig Config cfg) {
SubmoduleCommits.Factory submoduleCommitsFactory) {
this.subscriptionGraphFactory = subscriptionGraphFactory;
this.submoduleCommitsFactory = submoduleCommitsFactory;
this.cfg = cfg;
}
public SubmoduleOp create(Set<BranchNameKey> updatedBranches, MergeOpRepoManager orm)
throws SubmoduleConflictException {
SubscriptionGraph subscriptionGraph;
if (cfg.getBoolean("submodule", "enableSuperProjectSubscriptions", true)) {
subscriptionGraph = subscriptionGraphFactory.compute(updatedBranches, orm);
} else {
logger.atFine().log("Updating superprojects disabled");
subscriptionGraph =
SubscriptionGraph.createEmptyGraph(ImmutableSet.copyOf(updatedBranches));
}
return new SubmoduleOp(orm, subscriptionGraph, submoduleCommitsFactory.create(orm));
return new SubmoduleOp(
orm,
subscriptionGraphFactory.compute(updatedBranches, orm),
submoduleCommitsFactory.create(orm));
}
}

View File

@@ -159,7 +159,7 @@ public class SubscriptionGraph {
public static class Module extends AbstractModule {
@Override
protected void configure() {
bind(Factory.class).to(DefaultFactory.class);
bind(Factory.class).annotatedWith(VanillaSubscriptionGraph.class).to(DefaultFactory.class);
}
}

View File

@@ -0,0 +1,29 @@
// Copyright (C) 2020 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.submit;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import com.google.inject.BindingAnnotation;
import java.lang.annotation.Retention;
/**
* Marker on a {@link SubscriptionGraph.Factory} without gerrit configuration.
*
* <p>See {@link ConfiguredSubscriptionGraphFactory}.
*/
@Retention(RUNTIME)
@BindingAnnotation
public @interface VanillaSubscriptionGraph {}