Introduce `reset` option to `configure` command
The `reset` option has been added to the `configure` command to revert
modified configuration settings to their default values. Usage is
outlined as follows:
- `/configure --reset` resets all modified settings to their default
values.
- `/configure --reset --<CONFIG_KEY_1> [...--<CONFIG_KEY_N>]` resets
only the specified key(s) to their default values.
Change-Id: Iea1a98c27258585e0628afb7c746e373155ca22a
Signed-off-by: Patrizio <patrizio.gelosi@amarulasolutions.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/config/DynamicConfiguration.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/config/DynamicConfiguration.java
index c0ea2bf..7e8dee9 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/config/DynamicConfiguration.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/config/DynamicConfiguration.java
@@ -25,10 +25,22 @@
dynamicConfig.put(key, value);
}
- public void updateConfiguration() {
- if (dynamicConfig != null && !dynamicConfig.isEmpty()) {
+ public void updateConfiguration(boolean modifiedDynamicConfig, boolean shouldResetDynamicConfig) {
+ if (dynamicConfig == null || dynamicConfig.isEmpty()) return;
+ if (shouldResetDynamicConfig && !modifiedDynamicConfig) {
+ pluginDataHandler.removeValue(KEY_DYNAMIC_CONFIG);
+ }
+ else {
+ if (shouldResetDynamicConfig) {
+ resetDynamicConfig();
+ }
log.info("Updating dynamic configuration with {}", dynamicConfig);
pluginDataHandler.setJsonValue(KEY_DYNAMIC_CONFIG, dynamicConfig);
}
}
+
+ private void resetDynamicConfig() {
+ // The keys with empty values are simply removed
+ dynamicConfig.entrySet().removeIf(entry -> entry.getValue() == null || entry.getValue().isEmpty());
+ }
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/commands/ClientCommands.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/commands/ClientCommands.java
index ebd2200..5489ff7 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/commands/ClientCommands.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/commands/ClientCommands.java
@@ -30,6 +30,9 @@
FILTER,
DEBUG
}
+ private enum CONFIGURE_OPTION_SET {
+ RESET
+ }
private static final Map<String, COMMAND_SET> COMMAND_MAP = Map.of(
"review", COMMAND_SET.REVIEW,
@@ -48,6 +51,9 @@
private static final List<COMMAND_SET> HISTORY_COMMANDS = new ArrayList<>(List.of(
COMMAND_SET.DIRECTIVE
));
+ private static final Map<String, CONFIGURE_OPTION_SET> CONFIGURE_OPTION_MAP = Map.of(
+ "reset", CONFIGURE_OPTION_SET.RESET
+ );
// Option values can be either a sequence of chars enclosed in double quotes or a sequence of non-space chars.
private static final String OPTION_VALUES = "\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\"|\\S+";
private static final Pattern COMMAND_PATTERN = Pattern.compile("/(" + String.join("|",
@@ -60,6 +66,8 @@
private DynamicConfiguration dynamicConfiguration;
private boolean containingHistoryCommand;
+ private boolean modifiedDynamicConfig;
+ private boolean shouldResetDynamicConfig;
public ClientCommands(
Configuration config,
@@ -76,6 +84,8 @@
dynamicConfiguration = new DynamicConfiguration(pluginDataHandlerProvider);
}
containingHistoryCommand = false;
+ modifiedDynamicConfig = false;
+ shouldResetDynamicConfig = false;
}
public boolean parseCommands(String comment, boolean isNotHistory) {
@@ -118,7 +128,7 @@
if (config.getEnableMessageDebugging()) {
changeSetData.setHideChatGptReview(true);
changeSetData.setForceDisplaySystemMessage(true);
- dynamicConfiguration.updateConfiguration();
+ dynamicConfiguration.updateConfiguration(modifiedDynamicConfig, shouldResetDynamicConfig);
}
else {
changeSetData.setReviewSystemMessage(localizer.getText(
@@ -173,8 +183,15 @@
break;
}
} else if (command == COMMAND_SET.CONFIGURE && config.getEnableMessageDebugging()) {
- log.debug("Updating configuration setting '{}' to '{}'", optionKey, optionValue);
- dynamicConfiguration.setConfig(optionKey, optionValue);
+ if (CONFIGURE_OPTION_MAP.get(optionKey) == CONFIGURE_OPTION_SET.RESET) {
+ shouldResetDynamicConfig = true;
+ log.debug("Resetting configuration settings");
+ }
+ else {
+ modifiedDynamicConfig = true;
+ log.debug("Updating configuration setting '{}' to '{}'", optionKey, optionValue);
+ dynamicConfiguration.setConfig(optionKey, optionValue);
+ }
}
}
}