LCOV - code coverage report
Current view: top level - server/git - PluggableCommitMessageGenerator.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 22 22 100.0 %
Date: 2022-11-19 15:00:39 Functions: 3 3 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;
      16             : 
      17             : import static com.google.common.base.Preconditions.checkState;
      18             : import static java.util.Objects.requireNonNull;
      19             : 
      20             : import com.google.common.flogger.FluentLogger;
      21             : import com.google.gerrit.common.Nullable;
      22             : import com.google.gerrit.entities.BranchNameKey;
      23             : import com.google.gerrit.extensions.registration.DynamicSet;
      24             : import com.google.gerrit.extensions.registration.Extension;
      25             : import com.google.inject.Inject;
      26             : import org.eclipse.jgit.revwalk.RevCommit;
      27             : 
      28             : /** Helper to call plugins that want to change the commit message before a change is merged. */
      29             : public class PluggableCommitMessageGenerator {
      30         103 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      31             : 
      32             :   private final DynamicSet<ChangeMessageModifier> changeMessageModifiers;
      33             : 
      34             :   @Inject
      35         103 :   PluggableCommitMessageGenerator(DynamicSet<ChangeMessageModifier> changeMessageModifiers) {
      36         103 :     this.changeMessageModifiers = changeMessageModifiers;
      37         103 :   }
      38             : 
      39             :   /**
      40             :    * Returns the commit message as modified by plugins. The returned message can be equal to {@code
      41             :    * originalMessage} in case no plugins are registered or the registered plugins decided not to
      42             :    * modify the message.
      43             :    */
      44             :   public String generate(
      45             :       RevCommit original,
      46             :       @Nullable RevCommit mergeTip,
      47             :       BranchNameKey dest,
      48             :       String originalMessage) {
      49         103 :     requireNonNull(original.getRawBuffer());
      50         103 :     if (mergeTip != null) {
      51          98 :       requireNonNull(mergeTip.getRawBuffer());
      52             :     }
      53             : 
      54         103 :     int count = 0;
      55         103 :     String current = originalMessage;
      56         103 :     for (Extension<ChangeMessageModifier> ext : changeMessageModifiers.entries()) {
      57           3 :       ChangeMessageModifier changeMessageModifier = ext.get();
      58           3 :       String className = changeMessageModifier.getClass().getName();
      59           3 :       current = changeMessageModifier.onSubmit(current, original, mergeTip, dest);
      60           3 :       checkState(
      61             :           current != null,
      62             :           "%s.onSubmit from plugin %s returned null instead of new commit message",
      63             :           className,
      64           3 :           ext.getPluginName());
      65           3 :       count++;
      66           3 :       logger.atFine().log(
      67             :           "Invoked %s from plugin %s, message length now %d",
      68           3 :           className, ext.getPluginName(), current.length());
      69           3 :     }
      70         103 :     logger.atFine().log(
      71             :         "Invoked %d ChangeMessageModifiers on message with original length %d",
      72         103 :         count, originalMessage.length());
      73         103 :     return current;
      74             :   }
      75             : }

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