DRY out the logic to read the owners plugin config
The logic for accessing the global and project-specific
plugin config was previously spread across the code.
Introduce a new common class PluginSettings to centralise
the logic to detect the plugin name and parse its config
settings.
Change-Id: I85e342ad4032ca22dd35e5c8e97e2f687273c174
diff --git a/owners-autoassign/src/main/java/com/googlesource/gerrit/owners/common/AutoassignConfig.java b/owners-autoassign/src/main/java/com/googlesource/gerrit/owners/common/AutoassignConfig.java
index 546e0aa..48ec686 100644
--- a/owners-autoassign/src/main/java/com/googlesource/gerrit/owners/common/AutoassignConfig.java
+++ b/owners-autoassign/src/main/java/com/googlesource/gerrit/owners/common/AutoassignConfig.java
@@ -20,36 +20,31 @@
import static com.googlesource.gerrit.owners.common.AutoassignConfigModule.PROJECT_CONFIG_AUTOASSIGN_WIP_CHANGES;
import com.google.gerrit.entities.Project;
-import com.google.gerrit.extensions.annotations.PluginName;
import com.google.gerrit.extensions.client.ReviewerState;
-import com.google.gerrit.server.config.PluginConfig;
-import com.google.gerrit.server.config.PluginConfigFactory;
import com.google.gerrit.server.project.NoSuchProjectException;
import com.google.inject.Inject;
import com.google.inject.Singleton;
@Singleton
public class AutoassignConfig {
-
- private final PluginConfigFactory cfgFactory;
- private final String pluginName;
+ private final PluginSettings config;
@Inject
- AutoassignConfig(@PluginName String pluginName, PluginConfigFactory cfgFactory) {
- this.pluginName = pluginName;
- this.cfgFactory = cfgFactory;
+ AutoassignConfig(PluginSettings config) {
+ this.config = config;
}
public boolean autoAssignWip(Project.NameKey projectKey) throws NoSuchProjectException {
- return cfg(projectKey).getEnum(PROJECT_CONFIG_AUTOASSIGN_WIP_CHANGES, TRUE).equals(TRUE);
+ return config
+ .projectSpecificConfig(projectKey)
+ .getEnum(PROJECT_CONFIG_AUTOASSIGN_WIP_CHANGES, TRUE)
+ .equals(TRUE);
}
public ReviewerState autoassignedReviewerState(Project.NameKey projectKey)
throws NoSuchProjectException {
- return cfg(projectKey).getEnum(PROJECT_CONFIG_AUTOASSIGN_FIELD, ReviewerState.REVIEWER);
- }
-
- private PluginConfig cfg(Project.NameKey projectKey) throws NoSuchProjectException {
- return cfgFactory.getFromProjectConfigWithInheritance(projectKey, pluginName);
+ return config
+ .projectSpecificConfig(projectKey)
+ .getEnum(PROJECT_CONFIG_AUTOASSIGN_FIELD, ReviewerState.REVIEWER);
}
}
diff --git a/owners-common/src/main/java/com/googlesource/gerrit/owners/common/PluginSettings.java b/owners-common/src/main/java/com/googlesource/gerrit/owners/common/PluginSettings.java
new file mode 100644
index 0000000..02f98ad
--- /dev/null
+++ b/owners-common/src/main/java/com/googlesource/gerrit/owners/common/PluginSettings.java
@@ -0,0 +1,66 @@
+// Copyright (C) 2022 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.googlesource.gerrit.owners.common;
+
+import com.google.common.collect.ImmutableSet;
+import com.google.gerrit.entities.Project;
+import com.google.gerrit.extensions.annotations.PluginName;
+import com.google.gerrit.server.config.PluginConfig;
+import com.google.gerrit.server.config.PluginConfigFactory;
+import com.google.gerrit.server.project.NoSuchProjectException;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+import org.eclipse.jgit.lib.Config;
+
+/** Global owners plugin's settings defined globally or on a per-project basis. */
+@Singleton
+public class PluginSettings {
+ private final ImmutableSet<String> disabledBranchesPatterns;
+ private final PluginConfigFactory configFactory;
+ private final String ownersPluginName;
+ private final Config globalPluginConfig;
+
+ @Inject
+ public PluginSettings(PluginConfigFactory configFactory, @PluginName String ownersPluginName) {
+ this.configFactory = configFactory;
+ this.ownersPluginName = ownersPluginName;
+
+ this.globalPluginConfig = configFactory.getGlobalPluginConfig(ownersPluginName);
+ disabledBranchesPatterns =
+ ImmutableSet.copyOf(globalPluginConfig.getStringList("owners", "disable", "branch"));
+ }
+
+ /**
+ * Branches that should be ignored for the OWNERS processing.
+ *
+ * @return set of branches regex
+ */
+ public ImmutableSet<String> disabledBranchPatterns() {
+ return disabledBranchesPatterns;
+ }
+
+ /**
+ * Project-specific config of the owners plugin.
+ *
+ * @param projectKey project name
+ * @return project-specific plugin config
+ * @throws NoSuchProjectException if the project cannot be found
+ */
+ public PluginConfig projectSpecificConfig(Project.NameKey projectKey)
+ throws NoSuchProjectException {
+ return configFactory.getFromProjectConfigWithInheritance(projectKey, ownersPluginName);
+ }
+}
diff --git a/owners/src/main/java/com/googlesource/gerrit/owners/OwnerPredicateProvider.java b/owners/src/main/java/com/googlesource/gerrit/owners/OwnerPredicateProvider.java
index 4225631..b96ab5b 100644
--- a/owners/src/main/java/com/googlesource/gerrit/owners/OwnerPredicateProvider.java
+++ b/owners/src/main/java/com/googlesource/gerrit/owners/OwnerPredicateProvider.java
@@ -18,21 +18,17 @@
import com.google.common.collect.ImmutableSet;
import com.google.gerrit.extensions.annotations.Listen;
-import com.google.gerrit.extensions.annotations.PluginName;
-import com.google.gerrit.server.config.PluginConfigFactory;
import com.google.gerrit.server.rules.PredicateProvider;
import com.google.inject.Inject;
import com.googlesource.gerrit.owners.common.Accounts;
-import org.eclipse.jgit.lib.Config;
+import com.googlesource.gerrit.owners.common.PluginSettings;
/** Gerrit OWNERS Prolog Predicate Provider. */
@Listen
public class OwnerPredicateProvider implements PredicateProvider {
@Inject
- public OwnerPredicateProvider(
- Accounts accounts, PluginConfigFactory configFactory, @PluginName String pluginName) {
- Config config = configFactory.getGlobalPluginConfig(pluginName);
- OwnersStoredValues.initialize(accounts, config.getStringList("owners", "disable", "branch"));
+ public OwnerPredicateProvider(Accounts accounts, PluginSettings config) {
+ OwnersStoredValues.initialize(accounts, config.disabledBranchPatterns());
}
@Override
diff --git a/owners/src/main/java/com/googlesource/gerrit/owners/OwnersStoredValues.java b/owners/src/main/java/com/googlesource/gerrit/owners/OwnersStoredValues.java
index 997bc06..e749cf2 100644
--- a/owners/src/main/java/com/googlesource/gerrit/owners/OwnersStoredValues.java
+++ b/owners/src/main/java/com/googlesource/gerrit/owners/OwnersStoredValues.java
@@ -16,6 +16,7 @@
package com.googlesource.gerrit.owners;
+import com.google.common.collect.ImmutableSet;
import com.google.gerrit.server.patch.PatchList;
import com.google.gerrit.server.rules.StoredValue;
import com.google.gerrit.server.rules.StoredValues;
@@ -33,7 +34,8 @@
public static StoredValue<PathOwners> PATH_OWNERS;
- public static synchronized void initialize(Accounts accounts, String[] disablePatterns) {
+ public static synchronized void initialize(
+ Accounts accounts, ImmutableSet<String> disablePatterns) {
if (PATH_OWNERS != null) {
return;
}