LCOV - code coverage report
Current view: top level - server/extensions/events - ChangeAbandoned.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 18 22 81.8 %
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.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.ChangeInfo;
      23             : import com.google.gerrit.extensions.common.RevisionInfo;
      24             : import com.google.gerrit.extensions.events.ChangeAbandonedListener;
      25             : import com.google.gerrit.server.GpgException;
      26             : import com.google.gerrit.server.account.AccountState;
      27             : import com.google.gerrit.server.patch.PatchListNotAvailableException;
      28             : import com.google.gerrit.server.patch.PatchListObjectTooLargeException;
      29             : import com.google.gerrit.server.permissions.PermissionBackendException;
      30             : import com.google.gerrit.server.plugincontext.PluginSetContext;
      31             : import com.google.gerrit.server.query.change.ChangeData;
      32             : import com.google.inject.Inject;
      33             : import com.google.inject.Singleton;
      34             : import java.io.IOException;
      35             : import java.time.Instant;
      36             : 
      37             : /** Helper class to fire an event when a change has been abandoned. */
      38             : @Singleton
      39             : public class ChangeAbandoned {
      40         142 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      41             : 
      42             :   private final PluginSetContext<ChangeAbandonedListener> listeners;
      43             :   private final EventUtil util;
      44             : 
      45             :   @Inject
      46         142 :   ChangeAbandoned(PluginSetContext<ChangeAbandonedListener> listeners, EventUtil util) {
      47         142 :     this.listeners = listeners;
      48         142 :     this.util = util;
      49         142 :   }
      50             : 
      51             :   public void fire(
      52             :       ChangeData changeData,
      53             :       PatchSet ps,
      54             :       AccountState abandoner,
      55             :       String reason,
      56             :       Instant when,
      57             :       NotifyHandling notifyHandling) {
      58          20 :     if (listeners.isEmpty()) {
      59           4 :       return;
      60             :     }
      61             :     try {
      62          16 :       Event event =
      63             :           new Event(
      64          16 :               util.changeInfo(changeData),
      65          16 :               util.revisionInfo(changeData.project(), ps),
      66          16 :               util.accountInfo(abandoner),
      67             :               reason,
      68             :               when,
      69             :               notifyHandling);
      70          16 :       listeners.runEach(l -> l.onChangeAbandoned(event));
      71           0 :     } catch (PatchListObjectTooLargeException e) {
      72           0 :       logger.atWarning().log("Couldn't fire event: %s", e.getMessage());
      73           0 :     } catch (PatchListNotAvailableException
      74             :         | GpgException
      75             :         | IOException
      76             :         | StorageException
      77             :         | PermissionBackendException e) {
      78           0 :       logger.atSevere().withCause(e).log("Couldn't fire event");
      79          16 :     }
      80          16 :   }
      81             : 
      82             :   /** Event to be fired when a change has been abandoned. */
      83             :   private static class Event extends AbstractRevisionEvent
      84             :       implements ChangeAbandonedListener.Event {
      85             :     private final String reason;
      86             : 
      87             :     Event(
      88             :         ChangeInfo change,
      89             :         RevisionInfo revision,
      90             :         AccountInfo abandoner,
      91             :         String reason,
      92             :         Instant when,
      93             :         NotifyHandling notifyHandling) {
      94          16 :       super(change, revision, abandoner, when, notifyHandling);
      95          16 :       this.reason = reason;
      96          16 :     }
      97             : 
      98             :     @Override
      99             :     public String getReason() {
     100          16 :       return reason;
     101             :     }
     102             :   }
     103             : }

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