Validation of Changed Max Path Length Configuration
Maximum path length must be parseable as an integer. If it is not,
a commit validation exception is thrown. This can be used on other
integer type fields as well.
Change-Id: Ieebdd1d3408c17fbe09e3e177c92abe6e82bb479
diff --git a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/PluginConfigValidator.java b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/PluginConfigValidator.java
index d3d6404..430e92b 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/PluginConfigValidator.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/PluginConfigValidator.java
@@ -17,6 +17,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.flogger.FluentLogger;
+import com.google.common.primitives.Ints;
import com.google.gerrit.entities.RefNames;
import com.google.gerrit.extensions.annotations.PluginName;
import com.google.gerrit.extensions.registration.DynamicSet;
@@ -134,7 +135,8 @@
validateRegex(fileName, cfg, ChangeEmailValidator.KEY_ALLOWED_AUTHOR_EMAIL_PATTERN));
validationMessages.addAll(
validateRegex(fileName, cfg, ChangeEmailValidator.KEY_ALLOWED_COMMITTER_EMAIL_PATTERN));
- // This is where we can add the validators for the other modules in the plugin.
+ validationMessages.addAll(
+ validateInteger(fileName, cfg, MaxPathLengthValidator.KEY_MAX_PATH_LENGTH));
return validationMessages.build();
}
@@ -166,4 +168,28 @@
}
return validationMessages.build();
}
+
+ /**
+ * Validates an integer-only field
+ *
+ * @param fileName the name of the config file
+ * @param cfg the project.config to validate
+ * @return list of messages with validation issues, empty list if there are no issues
+ */
+ @VisibleForTesting
+ public ImmutableList<CommitValidationMessage> validateInteger(
+ String fileName, Config cfg, String validatorKey) {
+
+ String value = cfg.getString("plugin", pluginName, validatorKey);
+
+ if (Ints.tryParse(value) == null) {
+ return ImmutableList.of(
+ new CommitValidationMessage(
+ String.format(
+ "The value '%s' configured in %s (parameter %s.%s) is invalid.",
+ value, fileName, pluginName, validatorKey),
+ ValidationMessage.Type.ERROR));
+ }
+ return ImmutableList.of();
+ }
}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/PluginConfigValidatorTest.java b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/PluginConfigValidatorTest.java
index 864c1b4..685abb8 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/PluginConfigValidatorTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/PluginConfigValidatorTest.java
@@ -17,6 +17,7 @@
import static com.google.common.truth.Truth.assertThat;
import static com.googlesource.gerrit.plugins.uploadvalidator.ChangeEmailValidator.KEY_ALLOWED_AUTHOR_EMAIL_PATTERN;
import static com.googlesource.gerrit.plugins.uploadvalidator.ChangeEmailValidator.KEY_ALLOWED_COMMITTER_EMAIL_PATTERN;
+import static com.googlesource.gerrit.plugins.uploadvalidator.MaxPathLengthValidator.KEY_MAX_PATH_LENGTH;
import static com.google.gerrit.server.project.ProjectConfig.PROJECT_CONFIG;
import com.google.common.collect.ImmutableList;
@@ -29,6 +30,8 @@
private static final String PLUGIN_NAME = "uploadvalidator";
private static final String ILLEGAL_REGEX = "*";
private static final String LEGAL_REGEX = ".*";
+ private static final String LEGAL_PATH_LENGTH = "100";
+ private static final String ILLEGAL_PATH_LENGTH = "10xi";
private PluginConfigValidator configValidator;
private Config cfg;
@@ -70,4 +73,21 @@
configValidator.validateRegex(PROJECT_CONFIG, cfg, KEY_ALLOWED_COMMITTER_EMAIL_PATTERN);
assertThat(messages).isNotEmpty();
}
-}
+
+ @Test
+ public void hasLegalMaxPathLength_noMessages() throws Exception {
+ cfg.setString("plugin", PLUGIN_NAME, KEY_MAX_PATH_LENGTH, LEGAL_PATH_LENGTH);
+ ImmutableList<CommitValidationMessage> messages =
+ configValidator.validateInteger(PROJECT_CONFIG, cfg, KEY_MAX_PATH_LENGTH);
+ assertThat(messages).isEmpty();
+ }
+
+ @Test
+ public void hasIllegalMaxPathLength_messages() throws Exception {
+ cfg.setString("plugin", PLUGIN_NAME, KEY_MAX_PATH_LENGTH, ILLEGAL_PATH_LENGTH);
+
+ ImmutableList<CommitValidationMessage> messages =
+ configValidator.validateInteger(PROJECT_CONFIG, cfg, KEY_MAX_PATH_LENGTH);
+ assertThat(messages).isNotEmpty();
+ }
+}
\ No newline at end of file