LCOV - code coverage report
Current view: top level - server/git - DefaultChangeReportFormatter.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 33 34 97.1 %
Date: 2022-11-19 15:00:39 Functions: 6 6 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2017 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;
      16             : 
      17             : import static com.google.common.base.Preconditions.checkState;
      18             : 
      19             : import com.google.gerrit.entities.Change;
      20             : import com.google.gerrit.extensions.registration.DynamicItem;
      21             : import com.google.gerrit.server.config.UrlFormatter;
      22             : import com.google.inject.Inject;
      23             : import java.util.Optional;
      24             : 
      25             : /** Default formatter for change descriptions for use in git command-line progress. */
      26             : public class DefaultChangeReportFormatter implements ChangeReportFormatter {
      27             :   private static final int SUBJECT_MAX_LENGTH = 80;
      28             :   private static final String SUBJECT_CROP_APPENDIX = "...";
      29             :   private static final int SUBJECT_CROP_RANGE = 10;
      30             :   private static final String NEW_CHANGE_INDICATOR = " [NEW]";
      31             : 
      32             :   protected final DynamicItem<UrlFormatter> urlFormatter;
      33             : 
      34             :   @Inject
      35          97 :   public DefaultChangeReportFormatter(DynamicItem<UrlFormatter> urlFormatter) {
      36          97 :     this.urlFormatter = urlFormatter;
      37          97 :   }
      38             : 
      39             :   @Override
      40             :   public String newChange(ChangeReportFormatter.Input input) {
      41          88 :     return formatChangeUrl(input) + NEW_CHANGE_INDICATOR;
      42             :   }
      43             : 
      44             :   @Override
      45             :   public String changeUpdated(ChangeReportFormatter.Input input) {
      46          37 :     return formatChangeUrl(input);
      47             :   }
      48             : 
      49             :   @Override
      50             :   public String changeClosed(ChangeReportFormatter.Input input) {
      51           3 :     Change c = input.change();
      52           3 :     return String.format(
      53             :         "change %s closed",
      54             :         urlFormatter
      55           3 :             .get()
      56           3 :             .getChangeViewUrl(c.getProject(), c.getId())
      57           3 :             .orElse(c.getId().toString()));
      58             :   }
      59             : 
      60             :   protected String cropSubject(String subject) {
      61          88 :     if (subject.length() > SUBJECT_MAX_LENGTH) {
      62           2 :       int maxLength = SUBJECT_MAX_LENGTH - SUBJECT_CROP_APPENDIX.length();
      63           2 :       for (int cropPosition = maxLength;
      64           2 :           cropPosition > maxLength - SUBJECT_CROP_RANGE;
      65           2 :           cropPosition--) {
      66           2 :         if (Character.isWhitespace(subject.charAt(cropPosition - 1))) {
      67           2 :           return subject.substring(0, cropPosition) + SUBJECT_CROP_APPENDIX;
      68             :         }
      69             :       }
      70           0 :       return subject.substring(0, maxLength) + SUBJECT_CROP_APPENDIX;
      71             :     }
      72          88 :     return subject;
      73             :   }
      74             : 
      75             :   protected String formatChangeUrl(Input input) {
      76          88 :     Change c = input.change();
      77          88 :     Optional<String> changeUrl = urlFormatter.get().getChangeViewUrl(c.getProject(), c.getId());
      78          88 :     checkState(changeUrl.isPresent());
      79             : 
      80          88 :     StringBuilder m =
      81             :         new StringBuilder()
      82          88 :             .append("  ")
      83          88 :             .append(changeUrl.get())
      84          88 :             .append(" ")
      85          88 :             .append(cropSubject(input.subject()));
      86          88 :     if (input.isEdit()) {
      87           3 :       m.append(" [EDIT]");
      88             :     }
      89          88 :     if (input.isPrivate()) {
      90           5 :       m.append(" [PRIVATE]");
      91             :     }
      92          88 :     if (input.isWorkInProgress()) {
      93          14 :       m.append(" [WIP]");
      94             :     }
      95          88 :     return m.toString();
      96             :   }
      97             : }

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