LCOV - code coverage report
Current view: top level - server/extensions/events - ChangeRestored.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 17 22 77.3 %
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.ChangeRestoredListener;
      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 restored. */
      38             : @Singleton
      39             : public class ChangeRestored {
      40         145 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      41             : 
      42             :   private final PluginSetContext<ChangeRestoredListener> listeners;
      43             :   private final EventUtil util;
      44             : 
      45             :   @Inject
      46         145 :   ChangeRestored(PluginSetContext<ChangeRestoredListener> listeners, EventUtil util) {
      47         145 :     this.listeners = listeners;
      48         145 :     this.util = util;
      49         145 :   }
      50             : 
      51             :   public void fire(
      52             :       ChangeData changeData, PatchSet ps, AccountState restorer, String reason, Instant when) {
      53           7 :     if (listeners.isEmpty()) {
      54           0 :       return;
      55             :     }
      56             :     try {
      57           7 :       Event event =
      58             :           new Event(
      59           7 :               util.changeInfo(changeData),
      60           7 :               util.revisionInfo(changeData.project(), ps),
      61           7 :               util.accountInfo(restorer),
      62             :               reason,
      63             :               when);
      64           7 :       listeners.runEach(l -> l.onChangeRestored(event));
      65           0 :     } catch (PatchListObjectTooLargeException e) {
      66           0 :       logger.atWarning().log("Couldn't fire event: %s", e.getMessage());
      67           0 :     } catch (PatchListNotAvailableException
      68             :         | GpgException
      69             :         | IOException
      70             :         | StorageException
      71             :         | PermissionBackendException e) {
      72           0 :       logger.atSevere().withCause(e).log("Couldn't fire event");
      73           7 :     }
      74           7 :   }
      75             : 
      76             :   /** Event to be fired when a change has been restored. */
      77             :   private static class Event extends AbstractRevisionEvent implements ChangeRestoredListener.Event {
      78             : 
      79             :     private String reason;
      80             : 
      81             :     Event(
      82             :         ChangeInfo change,
      83             :         RevisionInfo revision,
      84             :         AccountInfo restorer,
      85             :         String reason,
      86             :         Instant when) {
      87           7 :       super(change, revision, restorer, when, NotifyHandling.ALL);
      88           7 :       this.reason = reason;
      89           7 :     }
      90             : 
      91             :     @Override
      92             :     public String getReason() {
      93           7 :       return reason;
      94             :     }
      95             :   }
      96             : }

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