LCOV - code coverage report
Current view: top level - server/extensions/events - ChangeReverted.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 7 17 41.2 %
Date: 2022-11-19 15:00:39 Functions: 3 6 50.0 %

          Line data    Source code
       1             : // Copyright (C) 2016 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.exceptions.StorageException;
      19             : import com.google.gerrit.extensions.api.changes.NotifyHandling;
      20             : import com.google.gerrit.extensions.common.ChangeInfo;
      21             : import com.google.gerrit.extensions.events.ChangeRevertedListener;
      22             : import com.google.gerrit.server.plugincontext.PluginSetContext;
      23             : import com.google.gerrit.server.query.change.ChangeData;
      24             : import com.google.inject.Inject;
      25             : import com.google.inject.Singleton;
      26             : import java.time.Instant;
      27             : 
      28             : /** Helper class to fire an event when a change has been reverted. */
      29             : @Singleton
      30             : public class ChangeReverted {
      31         145 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      32             : 
      33             :   private final PluginSetContext<ChangeRevertedListener> listeners;
      34             :   private final EventUtil util;
      35             : 
      36             :   @Inject
      37         145 :   ChangeReverted(PluginSetContext<ChangeRevertedListener> listeners, EventUtil util) {
      38         145 :     this.listeners = listeners;
      39         145 :     this.util = util;
      40         145 :   }
      41             : 
      42             :   public void fire(ChangeData changeData, ChangeData revertChangeData, Instant when) {
      43          14 :     if (listeners.isEmpty()) {
      44          14 :       return;
      45             :     }
      46             :     try {
      47           0 :       Event event = new Event(util.changeInfo(changeData), util.changeInfo(revertChangeData), when);
      48           0 :       listeners.runEach(l -> l.onChangeReverted(event));
      49           0 :     } catch (StorageException e) {
      50           0 :       logger.atSevere().withCause(e).log("Couldn't fire event");
      51           0 :     }
      52           0 :   }
      53             : 
      54             :   /** Event to be fired when a change has been reverted. */
      55             :   private static class Event extends AbstractChangeEvent implements ChangeRevertedListener.Event {
      56             :     private final ChangeInfo revertChange;
      57             : 
      58             :     Event(ChangeInfo change, ChangeInfo revertChange, Instant when) {
      59           0 :       super(change, revertChange.owner, when, NotifyHandling.ALL);
      60           0 :       this.revertChange = revertChange;
      61           0 :     }
      62             : 
      63             :     @Override
      64             :     public ChangeInfo getRevertChange() {
      65           0 :       return revertChange;
      66             :     }
      67             :   }
      68             : }

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