LCOV - code coverage report
Current view: top level - server/extensions/events - ReviewerAdded.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 19 23 82.6 %
Date: 2022-11-19 15:00:39 Functions: 6 6 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.collect.Lists;
      18             : import com.google.common.flogger.FluentLogger;
      19             : import com.google.gerrit.entities.PatchSet;
      20             : import com.google.gerrit.exceptions.StorageException;
      21             : import com.google.gerrit.extensions.api.changes.NotifyHandling;
      22             : import com.google.gerrit.extensions.common.AccountInfo;
      23             : import com.google.gerrit.extensions.common.ChangeInfo;
      24             : import com.google.gerrit.extensions.common.RevisionInfo;
      25             : import com.google.gerrit.extensions.events.ReviewerAddedListener;
      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.List;
      38             : 
      39             : /** Helper class to fire an event when reviewers have been added to a change. */
      40             : @Singleton
      41             : public class ReviewerAdded {
      42         146 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      43             : 
      44             :   private final PluginSetContext<ReviewerAddedListener> listeners;
      45             :   private final EventUtil util;
      46             : 
      47             :   @Inject
      48         146 :   ReviewerAdded(PluginSetContext<ReviewerAddedListener> 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 patchSet,
      56             :       List<AccountState> reviewers,
      57             :       AccountState adder,
      58             :       Instant when) {
      59          73 :     if (listeners.isEmpty() || reviewers.isEmpty()) {
      60          68 :       return;
      61             :     }
      62             : 
      63             :     try {
      64          26 :       Event event =
      65             :           new Event(
      66          26 :               util.changeInfo(changeData),
      67          26 :               util.revisionInfo(changeData.project(), patchSet),
      68          26 :               Lists.transform(reviewers, util::accountInfo),
      69          26 :               util.accountInfo(adder),
      70             :               when);
      71          26 :       listeners.runEach(l -> l.onReviewersAdded(event));
      72           0 :     } catch (PatchListObjectTooLargeException e) {
      73           0 :       logger.atWarning().log("Couldn't fire event: %s", e.getMessage());
      74           0 :     } catch (PatchListNotAvailableException
      75             :         | GpgException
      76             :         | IOException
      77             :         | StorageException
      78             :         | PermissionBackendException e) {
      79           0 :       logger.atSevere().withCause(e).log("Couldn't fire event");
      80          26 :     }
      81          26 :   }
      82             : 
      83             :   /** Event to be fired when reviewers have been added to a change. */
      84             :   private static class Event extends AbstractRevisionEvent implements ReviewerAddedListener.Event {
      85             :     private final List<AccountInfo> reviewers;
      86             : 
      87             :     Event(
      88             :         ChangeInfo change,
      89             :         RevisionInfo revision,
      90             :         List<AccountInfo> reviewers,
      91             :         AccountInfo adder,
      92             :         Instant when) {
      93          26 :       super(change, revision, adder, when, NotifyHandling.ALL);
      94          26 :       this.reviewers = reviewers;
      95          26 :     }
      96             : 
      97             :     @Override
      98             :     public List<AccountInfo> getReviewers() {
      99          26 :       return reviewers;
     100             :     }
     101             :   }
     102             : }

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