LCOV - code coverage report
Current view: top level - server/extensions/events - CommentAdded.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 26 28 92.9 %
Date: 2022-11-19 15:00:39 Functions: 8 8 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2015 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.extensions.events;
      16             : 
      17             : import com.google.common.flogger.FluentLogger;
      18             : import com.google.gerrit.entities.PatchSet;
      19             : import com.google.gerrit.exceptions.StorageException;
      20             : import com.google.gerrit.extensions.api.changes.NotifyHandling;
      21             : import com.google.gerrit.extensions.common.AccountInfo;
      22             : import com.google.gerrit.extensions.common.ApprovalInfo;
      23             : import com.google.gerrit.extensions.common.ChangeInfo;
      24             : import com.google.gerrit.extensions.common.RevisionInfo;
      25             : import com.google.gerrit.extensions.events.CommentAddedListener;
      26             : import com.google.gerrit.server.GpgException;
      27             : import com.google.gerrit.server.account.AccountState;
      28             : import com.google.gerrit.server.patch.PatchListNotAvailableException;
      29             : import com.google.gerrit.server.patch.PatchListObjectTooLargeException;
      30             : import com.google.gerrit.server.permissions.PermissionBackendException;
      31             : import com.google.gerrit.server.plugincontext.PluginSetContext;
      32             : import com.google.gerrit.server.query.change.ChangeData;
      33             : import com.google.inject.Inject;
      34             : import com.google.inject.Singleton;
      35             : import java.io.IOException;
      36             : import java.time.Instant;
      37             : import java.util.Map;
      38             : 
      39             : /** Helper class to fire an event when a comment or vote has been added to a change. */
      40             : @Singleton
      41             : public class CommentAdded {
      42         146 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      43             : 
      44             :   private final PluginSetContext<CommentAddedListener> listeners;
      45             :   private final EventUtil util;
      46             : 
      47             :   @Inject
      48         146 :   CommentAdded(PluginSetContext<CommentAddedListener> listeners, EventUtil util) {
      49         146 :     this.listeners = listeners;
      50         146 :     this.util = util;
      51         146 :   }
      52             : 
      53             :   public void fire(
      54             :       ChangeData changeData,
      55             :       PatchSet ps,
      56             :       AccountState author,
      57             :       String comment,
      58             :       Map<String, Short> approvals,
      59             :       Map<String, Short> oldApprovals,
      60             :       Instant when) {
      61          65 :     if (listeners.isEmpty()) {
      62           4 :       return;
      63             :     }
      64             :     try {
      65          61 :       Event event =
      66             :           new Event(
      67          61 :               util.changeInfo(changeData),
      68          61 :               util.revisionInfo(changeData.project(), ps),
      69          61 :               util.accountInfo(author),
      70             :               comment,
      71          61 :               util.approvals(author, approvals, when),
      72          61 :               util.approvals(author, oldApprovals, when),
      73             :               when);
      74          61 :       listeners.runEach(l -> l.onCommentAdded(event));
      75           0 :     } catch (PatchListObjectTooLargeException e) {
      76           0 :       logger.atWarning().log("Couldn't fire event: %s", e.getMessage());
      77           1 :     } catch (PatchListNotAvailableException
      78             :         | GpgException
      79             :         | IOException
      80             :         | StorageException
      81             :         | PermissionBackendException e) {
      82           1 :       logger.atSevere().withCause(e).log("Couldn't fire event");
      83          61 :     }
      84          61 :   }
      85             : 
      86             :   /** Event to be fired when a comment or vote has been added to a change. */
      87             :   private static class Event extends AbstractRevisionEvent implements CommentAddedListener.Event {
      88             : 
      89             :     private final String comment;
      90             :     private final Map<String, ApprovalInfo> approvals;
      91             :     private final Map<String, ApprovalInfo> oldApprovals;
      92             : 
      93             :     Event(
      94             :         ChangeInfo change,
      95             :         RevisionInfo revision,
      96             :         AccountInfo author,
      97             :         String comment,
      98             :         Map<String, ApprovalInfo> approvals,
      99             :         Map<String, ApprovalInfo> oldApprovals,
     100             :         Instant when) {
     101          61 :       super(change, revision, author, when, NotifyHandling.ALL);
     102          61 :       this.comment = comment;
     103          61 :       this.approvals = approvals;
     104          61 :       this.oldApprovals = oldApprovals;
     105          61 :     }
     106             : 
     107             :     @Override
     108             :     public String getComment() {
     109          61 :       return comment;
     110             :     }
     111             : 
     112             :     @Override
     113             :     public Map<String, ApprovalInfo> getApprovals() {
     114          61 :       return approvals;
     115             :     }
     116             : 
     117             :     @Override
     118             :     public Map<String, ApprovalInfo> getOldApprovals() {
     119          61 :       return oldApprovals;
     120             :     }
     121             :   }
     122             : }

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