Add DisabledAuthors/EnabledAuthors config options
Added the `enabledAuthors` option, allowing for the specification of
users authorized to have their Patchsets and comments reviewed. By
default, all users are enabled, but this setting can be adjusted to
restrict reviews to specific usernames.
Additionally, the disabledAuthors option has been added, serving as a
counterpart to `enabledAuthors` with inverse functionality.
Jira-Id: IT-103
Change-Id: Ie0dcda50ea5f46e6000de1569a2c2fbdf76f4466
Signed-off-by: Patrizio <patrizio.gelosi@amarulasolutions.com>
diff --git a/README.md b/README.md
index 13da7ac..2482d0c 100644
--- a/README.md
+++ b/README.md
@@ -205,6 +205,9 @@
being submitted for review.
- `gptStreamOutput`: The default value is true. Whether the response is expected in stream output mode or not.
- `maxReviewLines`: The default value is 1000. This sets a limit on the number of lines of code included in the review.
+- `enabledAuthors`: By default, every user is enabled to have their Patchsets and comments reviewed. To limit review
+ capabilities to specific users, list their usernames in this setting, separated by commas.
+- `disabledAuthors`: Functions oppositely to enabledAuthors.
- `enabledFileExtensions`: This limits the reviewed files to the given types. Default file extensions are ".py, .java,
.js, .ts, .html, .css, .cs, .cpp, .c, .h, .php, .rb, .swift, .kt, .r, .jl, .go, .scala, .pl, .pm, .rs, .dart, .lua,
.sh, .vb, .bat".
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/GerritClientBase.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/GerritClientBase.java
index f6549ed..cdc7682 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/GerritClientBase.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/GerritClientBase.java
@@ -48,4 +48,12 @@
return response.body();
}
+ public boolean isDisabledAuthor(String authorUsername) {
+ List<String> enabledAuthors = config.getEnabledAuthors();
+ List<String> disabledAuthors = config.getDisabledAuthors();
+ return !enabledAuthors.contains(Configuration.ENABLED_AUTHORS_ALL)
+ && !enabledAuthors.contains(authorUsername)
+ || disabledAuthors.contains(authorUsername);
+ }
+
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/GerritClientComments.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/GerritClientComments.java
index 5ad8eaa..537488e 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/GerritClientComments.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/GerritClientComments.java
@@ -1,7 +1,6 @@
package com.googlesource.gerrit.plugins.chatgpt.client;
import com.google.common.net.HttpHeaders;
-import com.google.gerrit.server.data.AccountAttribute;
import com.google.gerrit.server.events.CommentAddedEvent;
import com.google.gerrit.server.events.Event;
import com.google.gson.Gson;
@@ -24,7 +23,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import java.util.function.Supplier;
import static java.net.HttpURLConnection.HTTP_OK;
@@ -181,14 +179,17 @@
public boolean retrieveLastComments(Event event, String fullChangeId) {
commentsStartTimestamp = event.eventCreatedOn;
CommentAddedEvent commentAddedEvent = (CommentAddedEvent) event;
- Supplier<AccountAttribute> author = commentAddedEvent.author;
- authorUsername = author.get().username;
+ authorUsername = commentAddedEvent.author.get().username;
log.debug("Comments start datetime: {}", commentsStartTimestamp);
log.debug("Author username: {} - ChatGPT username: {}", authorUsername, config.getGerritUserName());
if (authorUsername.equals(config.getGerritUserName())) {
log.debug("These are the Chatbot's own comments, do not process them.");
return false;
}
+ if (isDisabledAuthor(authorUsername)) {
+ log.info("Review of comments from author '{}' is disabled.", authorUsername);
+ return false;
+ }
addAllComments(fullChangeId);
return !commentProperties.isEmpty();
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/config/Configuration.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/config/Configuration.java
index 9ab25da..317a7f6 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/config/Configuration.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/config/Configuration.java
@@ -29,12 +29,15 @@
public static final String NOT_CONFIGURED_ERROR_MSG = "%s is not configured";
public static final String KEY_GPT_SYSTEM_PROMPT = "gptSystemPrompt";
public static final String KEY_GPT_USER_PROMPT = "gptUserPrompt";
+ public static final String ENABLED_AUTHORS_ALL = "ALL";
private static final String DEFAULT_GPT_TEMPERATURE = "1";
private static final boolean DEFAULT_REVIEW_PATCHSET = true;
private static final boolean DEFAULT_REVIEW_COMMIT_MESSAGES = false;
private static final boolean DEFAULT_FULL_FILE_REVIEW = true;
private static final boolean DEFAULT_STREAM_OUTPUT = true;
private static final boolean DEFAULT_GLOBAL_ENABLE = false;
+ private static final String DEFAULT_DISABLED_AUTHORS = "";
+ private static final String DEFAULT_ENABLED_AUTHORS = ENABLED_AUTHORS_ALL;
private static final String DEFAULT_ENABLED_PROJECTS = "";
private static final String DEFAULT_ENABLED_FILE_EXTENSIONS = String.join(",", new String[]{
".py",
@@ -80,6 +83,8 @@
private static final String KEY_FULL_FILE_REVIEW = "gptFullFileReview";
private static final String KEY_PROJECT_ENABLE = "isEnabled";
private static final String KEY_GLOBAL_ENABLE = "globalEnable";
+ private static final String KEY_DISABLED_AUTHORS = "disabledAuthors";
+ private static final String KEY_ENABLED_AUTHORS = "enabledAuthors";
private static final String KEY_ENABLED_PROJECTS = "enabledProjects";
private static final String KEY_MAX_REVIEW_LINES = "maxReviewLines";
private static final String KEY_MAX_REVIEW_FILE_SIZE = "maxReviewFileSize";
@@ -178,6 +183,14 @@
return globalConfig.getBoolean(KEY_GLOBAL_ENABLE, DEFAULT_GLOBAL_ENABLE);
}
+ public List<String> getDisabledAuthors() {
+ return splitConfig(globalConfig.getString(KEY_DISABLED_AUTHORS, DEFAULT_DISABLED_AUTHORS));
+ }
+
+ public List<String> getEnabledAuthors() {
+ return splitConfig(globalConfig.getString(KEY_ENABLED_AUTHORS, DEFAULT_ENABLED_AUTHORS));
+ }
+
public String getEnabledProjects() {
return globalConfig.getString(KEY_ENABLED_PROJECTS, DEFAULT_ENABLED_PROJECTS);
}
@@ -191,9 +204,7 @@
}
public List<String> getEnabledFileExtensions() {
- Pattern separator=Pattern.compile("\\s*,\\s*");
- String fileExtensions = globalConfig.getString(KEY_ENABLED_FILE_EXTENSIONS, DEFAULT_ENABLED_FILE_EXTENSIONS);
- return Arrays.asList(separator.split(fileExtensions));
+ return splitConfig(globalConfig.getString(KEY_ENABLED_FILE_EXTENSIONS, DEFAULT_ENABLED_FILE_EXTENSIONS));
}
private String getValidatedOrThrow(String key) {
@@ -233,6 +244,10 @@
return globalConfig.getBoolean(key, defaultValue);
}
+ private List<String> splitConfig(String value) {
+ Pattern separator=Pattern.compile("\\s*,\\s*");
+ return Arrays.asList(separator.split(value));
+ }
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/EventListenerHandler.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/EventListenerHandler.java
index 9fdda70..a6efb01 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/EventListenerHandler.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/EventListenerHandler.java
@@ -18,7 +18,6 @@
import java.util.List;
import java.util.Optional;
import java.util.concurrent.*;
-import java.util.function.Supplier;
import static com.google.gerrit.extensions.client.ChangeKind.REWORK;
@@ -68,12 +67,17 @@
return false;
}
try {
- Supplier<PatchSetAttribute> patchSetAttribute = patchSetEvent.patchSet;
- ChangeKind patchSetEventKind = patchSetAttribute.get().kind;
+ PatchSetAttribute patchSetAttribute = patchSetEvent.patchSet.get();
+ ChangeKind patchSetEventKind = patchSetAttribute.kind;
if (patchSetEventKind != REWORK) {
log.info("Change kind '{}' not processed", patchSetEventKind);
return false;
}
+ String authorUsername = patchSetAttribute.author.username;
+ if (gerritClient.isDisabledAuthor(authorUsername)) {
+ log.info("Review of Patchsets from author '{}' is disabled.", authorUsername);
+ return false;
+ }
}
catch (NullPointerException e) {
log.debug("PatchSet event properties not retrieved");
diff --git a/src/test/java/com/googlesource/gerrit/plugins/chatgpt/ChatGptReviewTest.java b/src/test/java/com/googlesource/gerrit/plugins/chatgpt/ChatGptReviewTest.java
index 4bb62fe..afe0d9c 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/chatgpt/ChatGptReviewTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/chatgpt/ChatGptReviewTest.java
@@ -191,6 +191,14 @@
REVIEW_TAG_COMMENTS;
}
+ private AccountAttribute createTestAccountAttribute() {
+ AccountAttribute accountAttribute = new AccountAttribute();
+ accountAttribute.name = GERRIT_ACCOUNT_NAME;
+ accountAttribute.username = GERRIT_USER_NAME;
+ accountAttribute.email = GERRIT_ACCOUNT_EMAIL;
+ return accountAttribute;
+ }
+
@Test
public void patchSetCreatedOrUpdated() throws InterruptedException, NoSuchProjectException, ExecutionException {
GerritClient gerritClient = new GerritClient();
@@ -207,6 +215,7 @@
event.patchSet = () -> {
PatchSetAttribute patchSetAttribute = new PatchSetAttribute();
patchSetAttribute.kind = REWORK;
+ patchSetAttribute.author = createTestAccountAttribute();
return patchSetAttribute;
};
EventListenerHandler eventListenerHandler = new EventListenerHandler(patchSetReviewer, gerritClient);
@@ -245,13 +254,7 @@
when(event.getBranchNameKey()).thenReturn(BRANCH_NAME);
when(event.getChangeKey()).thenReturn(CHANGE_ID);
when(event.getType()).thenReturn("comment-added");
- event.author = () -> {
- AccountAttribute accountAttribute = new AccountAttribute();
- accountAttribute.name = GERRIT_ACCOUNT_NAME;
- accountAttribute.username = GERRIT_USER_NAME;
- accountAttribute.email = GERRIT_ACCOUNT_EMAIL;
- return accountAttribute;
- };
+ event.author = this::createTestAccountAttribute;
event.eventCreatedOn = TEST_TIMESTAMP;
EventListenerHandler eventListenerHandler = new EventListenerHandler(patchSetReviewer, gerritClient);