LCOV - code coverage report
Current view: top level - server/git/validators - CommentSizeValidator.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 16 20 80.0 %
Date: 2022-11-19 15:00:39 Functions: 5 5 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2020 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.server.git.validators;
      16             : 
      17             : import com.google.common.collect.ImmutableList;
      18             : import com.google.gerrit.extensions.validators.CommentForValidation;
      19             : import com.google.gerrit.extensions.validators.CommentValidationContext;
      20             : import com.google.gerrit.extensions.validators.CommentValidationFailure;
      21             : import com.google.gerrit.extensions.validators.CommentValidator;
      22             : import com.google.gerrit.server.config.GerritServerConfig;
      23             : import com.google.inject.Inject;
      24             : import org.eclipse.jgit.lib.Config;
      25             : 
      26             : /** Limits the size of comments to prevent space/time complexity issues. */
      27             : public class CommentSizeValidator implements CommentValidator {
      28             :   private final int commentSizeLimit;
      29             :   private final int robotCommentSizeLimit;
      30             : 
      31             :   @Inject
      32          65 :   CommentSizeValidator(@GerritServerConfig Config serverConfig) {
      33          65 :     commentSizeLimit = serverConfig.getInt("change", "commentSizeLimit", 16 << 10);
      34          65 :     robotCommentSizeLimit = serverConfig.getInt("change", "robotCommentSizeLimit", 1 << 20);
      35          65 :   }
      36             : 
      37             :   @Override
      38             :   public ImmutableList<CommentValidationFailure> validateComments(
      39             :       CommentValidationContext ctx, ImmutableList<CommentForValidation> comments) {
      40          65 :     return comments.stream()
      41          65 :         .filter(this::exceedsSizeLimit)
      42          65 :         .map(c -> c.failValidation(buildErrorMessage(c)))
      43          65 :         .collect(ImmutableList.toImmutableList());
      44             :   }
      45             : 
      46             :   private boolean exceedsSizeLimit(CommentForValidation comment) {
      47          65 :     switch (comment.getSource()) {
      48             :       case HUMAN:
      49          65 :         return comment.getApproximateSize() > commentSizeLimit;
      50             :       case ROBOT:
      51           7 :         return robotCommentSizeLimit > 0 && comment.getApproximateSize() > robotCommentSizeLimit;
      52             :     }
      53           0 :     throw new RuntimeException(
      54           0 :         "Unknown comment source (should not have compiled): " + comment.getSource());
      55             :   }
      56             : 
      57             :   private String buildErrorMessage(CommentForValidation comment) {
      58           2 :     switch (comment.getSource()) {
      59             :       case HUMAN:
      60           1 :         return String.format(
      61           1 :             "Comment size exceeds limit (%d > %d)", comment.getApproximateSize(), commentSizeLimit);
      62             : 
      63             :       case ROBOT:
      64           1 :         return String.format(
      65             :             "Size %d (bytes) of robot comment is greater than limit %d (bytes)",
      66           1 :             comment.getApproximateSize(), robotCommentSizeLimit);
      67             :     }
      68           0 :     throw new RuntimeException(
      69           0 :         "Unknown comment source (should not have compiled): " + comment.getSource());
      70             :   }
      71             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750