Limit the number of comments per change (to 5000 by default)

Also refactor the robot comment size check to be in line with the size
check for human comments.

Change-Id: If24adc10f5f16cd8894bf246078f0e6c992cdafa
This commit is contained in:
Joerg Zieren
2020-01-02 10:18:14 +01:00
parent b91db6bcdd
commit 7cac5d14a9
23 changed files with 555 additions and 266 deletions

View File

@@ -92,6 +92,7 @@ import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.extensions.restapi.UnprocessableEntityException;
import com.google.gerrit.extensions.validators.CommentForValidation;
import com.google.gerrit.extensions.validators.CommentForValidation.CommentSource;
import com.google.gerrit.extensions.validators.CommentForValidation.CommentType;
import com.google.gerrit.extensions.validators.CommentValidationContext;
import com.google.gerrit.extensions.validators.CommentValidationFailure;
@@ -126,6 +127,8 @@ import com.google.gerrit.server.git.MultiProgressMonitor.Task;
import com.google.gerrit.server.git.ReceivePackInitializer;
import com.google.gerrit.server.git.TagCache;
import com.google.gerrit.server.git.ValidationError;
import com.google.gerrit.server.git.validators.CommentCountValidator;
import com.google.gerrit.server.git.validators.CommentSizeValidator;
import com.google.gerrit.server.git.validators.CommitValidationMessage;
import com.google.gerrit.server.git.validators.RefOperationValidationException;
import com.google.gerrit.server.git.validators.RefOperationValidators;
@@ -1441,11 +1444,18 @@ class ReceiveCommits {
final ReceiveCommand cmd;
final LabelTypes labelTypes;
/**
* Result of running {@link CommentValidator}-s on drafts that are published with the commit
* (which happens iff {@code --publish-comments} is set). Remains {@code true} if none are
* installed.
* Draft comments are published with the commit iff {@code --publish-comments} is set. All
* drafts are withheld (overriding the option) if at least one of the following conditions are
* met:
*
* <ul>
* <li>Installed {@link CommentValidator} plugins reject one or more draft comments.
* <li>One or more comments exceed the maximum comment size (see {@link
* CommentSizeValidator}).
* <li>The maximum number of comments would be exceeded (see {@link CommentCountValidator}).
* </ul>
*/
private boolean commentsValid = true;
private boolean withholdComments = false;
BranchNameKey dest;
PermissionBackend.ForRef perm;
@@ -1642,18 +1652,19 @@ class ReceiveCommits {
.collect(toImmutableSet());
}
void setCommentsValid(boolean commentsValid) {
this.commentsValid = commentsValid;
void setWithholdComments(boolean withholdComments) {
this.withholdComments = withholdComments;
}
boolean shouldPublishComments() {
if (!commentsValid) {
if (withholdComments) {
// Validation messages of type WARNING have already been added, now withhold the comments.
return false;
}
if (publishComments) {
return true;
} else if (noPublishComments) {
}
if (noPublishComments) {
return false;
}
return defaultPublishComments;
@@ -2010,19 +2021,18 @@ class ReceiveCommits {
.map(
comment ->
CommentForValidation.create(
CommentSource.HUMAN,
comment.lineNbr > 0
? CommentType.INLINE_COMMENT
: CommentType.FILE_COMMENT,
comment.message))
comment.message,
comment.message.length()))
.collect(toImmutableList());
CommentValidationContext ctx =
CommentValidationContext.builder()
.changeId(change.getChangeId())
.project(change.getProject().get())
.build();
CommentValidationContext.create(change.getChangeId(), change.getProject().get());
ImmutableList<CommentValidationFailure> commentValidationFailures =
PublishCommentUtil.findInvalidComments(ctx, commentValidators, draftsForValidation);
magicBranch.setCommentsValid(commentValidationFailures.isEmpty());
magicBranch.setWithholdComments(!commentValidationFailures.isEmpty());
commentValidationFailures.forEach(
failure ->
addMessage(