Extract its path to provider
This allows to simplify the reading of Soy templates.
Change-Id: Ib48a7fd35d3864faaecb2076070f19d2421f8602
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/base/ItsHookModule.java b/src/main/java/com/googlesource/gerrit/plugins/its/base/ItsHookModule.java
index 5e994e0..691a037 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/its/base/ItsHookModule.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/its/base/ItsHookModule.java
@@ -20,8 +20,11 @@
import com.google.gerrit.extensions.registration.DynamicSet;
import com.google.gerrit.server.config.PluginConfigFactory;
import com.google.gerrit.server.config.ProjectConfigEntry;
+import com.google.gerrit.server.config.SitePaths;
import com.google.gerrit.server.events.EventListener;
import com.google.gerrit.server.git.validators.CommitValidationListener;
+import com.google.inject.Inject;
+import com.google.inject.Provides;
import com.googlesource.gerrit.plugins.its.base.its.ItsConfig;
import com.googlesource.gerrit.plugins.its.base.its.ItsHookEnabledConfigEntry;
import com.googlesource.gerrit.plugins.its.base.validation.ItsValidateComment;
@@ -34,9 +37,13 @@
import com.googlesource.gerrit.plugins.its.base.workflow.action.AddSoyComment;
import com.googlesource.gerrit.plugins.its.base.workflow.action.AddStandardComment;
import com.googlesource.gerrit.plugins.its.base.workflow.action.LogEvent;
+import java.nio.file.Path;
public class ItsHookModule extends FactoryModule {
+ /** Folder where rules configuration files are located */
+ private static final String ITS_FOLDER = "its";
+
private final String pluginName;
private final PluginConfigFactory pluginCfgFactory;
@@ -62,4 +69,11 @@
factory(AddStandardComment.Factory.class);
factory(LogEvent.Factory.class);
}
+
+ @Provides
+ @ItsPath
+ @Inject
+ Path itsPath(SitePaths sitePaths) {
+ return sitePaths.etc_dir.normalize().resolve(ITS_FOLDER);
+ }
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/base/ItsPath.java b/src/main/java/com/googlesource/gerrit/plugins/its/base/ItsPath.java
new file mode 100644
index 0000000..dfd5119
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/its/base/ItsPath.java
@@ -0,0 +1,24 @@
+// Copyright (C) 2018 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.plugins.its.base;
+
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import com.google.inject.BindingAnnotation;
+import java.lang.annotation.Retention;
+
+@Retention(RUNTIME)
+@BindingAnnotation
+public @interface ItsPath {}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/base/workflow/RuleBase.java b/src/main/java/com/googlesource/gerrit/plugins/its/base/workflow/RuleBase.java
index 748f4f6..8dd0016 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/its/base/workflow/RuleBase.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/its/base/workflow/RuleBase.java
@@ -16,8 +16,8 @@
import com.google.common.collect.Lists;
import com.google.gerrit.extensions.annotations.PluginName;
-import com.google.gerrit.server.config.SitePaths;
import com.google.inject.Inject;
+import com.googlesource.gerrit.plugins.its.base.ItsPath;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
@@ -55,12 +55,12 @@
@Inject
public RuleBase(
- SitePaths sitePaths,
+ @ItsPath Path itsPath,
Rule.Factory ruleFactory,
Condition.Factory conditionFactory,
ActionRequest.Factory actionRequestFactory,
@PluginName String pluginName) {
- this.itsPath = sitePaths.etc_dir.normalize().resolve("its");
+ this.itsPath = itsPath;
this.ruleFactory = ruleFactory;
this.conditionFactory = conditionFactory;
this.actionRequestFactory = actionRequestFactory;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/base/workflow/action/AddSoyComment.java b/src/main/java/com/googlesource/gerrit/plugins/its/base/workflow/action/AddSoyComment.java
index 2396234..0ddefd5 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/its/base/workflow/action/AddSoyComment.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/its/base/workflow/action/AddSoyComment.java
@@ -16,16 +16,15 @@
import com.google.common.base.Strings;
import com.google.common.io.CharStreams;
-import com.google.gerrit.server.config.SitePath;
import com.google.inject.Inject;
import com.google.inject.ProvisionException;
import com.google.template.soy.SoyFileSet;
import com.google.template.soy.data.SanitizedContent;
import com.google.template.soy.tofu.SoyTofu;
+import com.googlesource.gerrit.plugins.its.base.ItsPath;
import com.googlesource.gerrit.plugins.its.base.its.ItsFacade;
import com.googlesource.gerrit.plugins.its.base.workflow.ActionRequest;
import com.googlesource.gerrit.plugins.its.base.workflow.Property;
-import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
@@ -48,17 +47,13 @@
AddSoyComment create();
}
- /** Directory (relative to site) to search templates in */
- private static final String ITS_TEMPLATE_DIR =
- "etc" + File.separator + "its" + File.separator + "templates";
-
private final ItsFacade its;
- private final Path sitePath;
+ private final Path templateDir;
protected HashMap<String, Object> soyContext;
@Inject
- public AddSoyComment(@SitePath Path sitePath, ItsFacade its) {
- this.sitePath = sitePath;
+ public AddSoyComment(@ItsPath Path itsPath, ItsFacade its) {
+ this.templateDir = itsPath.resolve("templates");
this.its = its;
}
@@ -82,7 +77,6 @@
String template,
SanitizedContent.ContentKind kind,
Set<Property> properties) {
- Path templateDir = sitePath.resolve(ITS_TEMPLATE_DIR);
Path templatePath = templateDir.resolve(template + ".soy");
String content;
diff --git a/src/test/java/com/googlesource/gerrit/plugins/its/base/workflow/RuleBaseTest.java b/src/test/java/com/googlesource/gerrit/plugins/its/base/workflow/RuleBaseTest.java
index 076ce0a..2ba4d4a 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/its/base/workflow/RuleBaseTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/its/base/workflow/RuleBaseTest.java
@@ -18,9 +18,9 @@
import com.google.common.collect.Lists;
import com.google.gerrit.extensions.annotations.PluginName;
import com.google.gerrit.extensions.config.FactoryModule;
-import com.google.gerrit.server.config.SitePath;
import com.google.inject.Guice;
import com.google.inject.Injector;
+import com.googlesource.gerrit.plugins.its.base.ItsPath;
import com.googlesource.gerrit.plugins.its.base.testutil.LoggingMockingTestCase;
import java.io.IOException;
import java.nio.file.Files;
@@ -35,7 +35,7 @@
public class RuleBaseTest extends LoggingMockingTestCase {
private Injector injector;
- private Path sitePath;
+ private Path itsPath;
private Rule.Factory ruleFactory;
private Condition.Factory conditionFactory;
private ActionRequest.Factory actionRequestFactory;
@@ -294,7 +294,7 @@
}
private void injectRuleBase(String rules, RuleBaseKind ruleBaseKind) throws IOException {
- Path ruleBaseFile = sitePath.resolve("etc").resolve("its").resolve(ruleBaseKind.fileName);
+ Path ruleBaseFile = itsPath.resolve(ruleBaseKind.fileName);
Files.createDirectories(ruleBaseFile.getParent());
Files.write(ruleBaseFile, rules.getBytes());
}
@@ -309,8 +309,8 @@
@Override
public void tearDown() throws Exception {
if (cleanupSitePath) {
- if (Files.exists(sitePath)) {
- FileUtils.delete(sitePath.toFile(), FileUtils.RECURSIVE);
+ if (Files.exists(itsPath)) {
+ FileUtils.delete(itsPath.toFile(), FileUtils.RECURSIVE);
}
}
super.tearDown();
@@ -326,11 +326,11 @@
bind(String.class).annotatedWith(PluginName.class).toInstance("ItsTestName");
- sitePath = randomTargetPath();
- assertFalse("sitePath already (" + sitePath + ") already exists", Files.exists(sitePath));
+ itsPath = randomTargetPath().resolve("etc").resolve("its");
+ assertFalse("itsPath (" + itsPath + ") already exists", Files.exists(itsPath));
cleanupSitePath = true;
- bind(Path.class).annotatedWith(SitePath.class).toInstance(sitePath);
+ bind(Path.class).annotatedWith(ItsPath.class).toInstance(itsPath);
ruleFactory = createMock(Rule.Factory.class);
bind(Rule.Factory.class).toInstance(ruleFactory);