diff options
Diffstat (limited to 'java/com/google/gerrit/server/git/validators/CommentCumulativeSizeValidator.java')
-rw-r--r-- | java/com/google/gerrit/server/git/validators/CommentCumulativeSizeValidator.java | 29 |
1 files changed, 19 insertions, 10 deletions
diff --git a/java/com/google/gerrit/server/git/validators/CommentCumulativeSizeValidator.java b/java/com/google/gerrit/server/git/validators/CommentCumulativeSizeValidator.java index 6e640f328c..b887323682 100644 --- a/java/com/google/gerrit/server/git/validators/CommentCumulativeSizeValidator.java +++ b/java/com/google/gerrit/server/git/validators/CommentCumulativeSizeValidator.java @@ -23,7 +23,6 @@ import com.google.gerrit.extensions.validators.CommentForValidation; import com.google.gerrit.extensions.validators.CommentValidationContext; import com.google.gerrit.extensions.validators.CommentValidationFailure; import com.google.gerrit.extensions.validators.CommentValidator; -import com.google.gerrit.server.ChangeMessagesUtil; import com.google.gerrit.server.config.GerritServerConfig; import com.google.gerrit.server.notedb.ChangeNotes; import com.google.inject.Inject; @@ -32,7 +31,8 @@ import org.eclipse.jgit.lib.Config; /** * Limits the total size of all comments and change messages to prevent space/time complexity - * issues. Note that autogenerated change messages are not subject to validation. + * issues. Note that autogenerated change messages are not subject to validation. However, we still + * count autogenerated messages for the limit (which will be notified on a further comment). */ public class CommentCumulativeSizeValidator implements CommentValidator { public static final int DEFAULT_CUMULATIVE_COMMENT_SIZE_LIMIT = 3 << 20; @@ -60,17 +60,11 @@ public class CommentCumulativeSizeValidator implements CommentValidator { notes.getRobotComments().values().stream()) .mapToInt(Comment::getApproximateSize) .sum() - + notes.getChangeMessages().stream() - // Auto-generated change messages are not counted for the limit. This method is not - // called when those change messages are created, but we should also skip them when - // counting the size for unrelated messages. - .filter(cm -> !ChangeMessagesUtil.isAutogenerated(cm.getTag())) - .mapToInt(cm -> cm.getMessage().length()) - .sum(); + + notes.getChangeMessages().stream().mapToInt(cm -> cm.getMessage().length()).sum(); int newCumulativeSize = comments.stream().mapToInt(CommentForValidation::getApproximateSize).sum(); ImmutableList.Builder<CommentValidationFailure> failures = ImmutableList.builder(); - if (!comments.isEmpty() && existingCumulativeSize + newCumulativeSize > maxCumulativeSize) { + if (!comments.isEmpty() && !isEnoughSpace(notes, newCumulativeSize, maxCumulativeSize)) { // This warning really applies to the set of all comments, but we need to pick one to attach // the message to. CommentForValidation commentForFailureMessage = Iterables.getLast(comments); @@ -84,4 +78,19 @@ public class CommentCumulativeSizeValidator implements CommentValidator { } return failures.build(); } + + /** + * Returns {@code true} if there is available space and the new size that we wish to add is less + * than the maximum allowed size. {@code false} otherwise (if there is not enough space). + */ + public static boolean isEnoughSpace(ChangeNotes notes, int addedBytes, int maxCumulativeSize) { + int existingCumulativeSize = + Stream.concat( + notes.getHumanComments().values().stream(), + notes.getRobotComments().values().stream()) + .mapToInt(Comment::getApproximateSize) + .sum() + + notes.getChangeMessages().stream().mapToInt(cm -> cm.getMessage().length()).sum(); + return existingCumulativeSize + addedBytes < maxCumulativeSize; + } } |