LCOV - code coverage report
Current view: top level - server/extensions/events - PrivateStateChanged.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 16 18 88.9 %
Date: 2022-11-19 15:00:39 Functions: 5 5 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2017 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.PrivateStateChangedListener;
      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.permissions.PermissionBackendException;
      29             : import com.google.gerrit.server.plugincontext.PluginSetContext;
      30             : import com.google.gerrit.server.query.change.ChangeData;
      31             : import com.google.inject.Inject;
      32             : import com.google.inject.Singleton;
      33             : import java.io.IOException;
      34             : import java.time.Instant;
      35             : 
      36             : /** Helper class to fire an event when the private flag of a change has been toggled. */
      37             : @Singleton
      38             : public class PrivateStateChanged {
      39         142 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      40             : 
      41             :   private final PluginSetContext<PrivateStateChangedListener> listeners;
      42             :   private final EventUtil util;
      43             : 
      44             :   @Inject
      45         142 :   PrivateStateChanged(PluginSetContext<PrivateStateChangedListener> listeners, EventUtil util) {
      46         142 :     this.listeners = listeners;
      47         142 :     this.util = util;
      48         142 :   }
      49             : 
      50             :   public void fire(ChangeData changeData, PatchSet patchSet, AccountState account, Instant when) {
      51          16 :     if (listeners.isEmpty()) {
      52           4 :       return;
      53             :     }
      54             :     try {
      55          12 :       Event event =
      56             :           new Event(
      57          12 :               util.changeInfo(changeData),
      58          12 :               util.revisionInfo(changeData.project(), patchSet),
      59          12 :               util.accountInfo(account),
      60             :               when);
      61          12 :       listeners.runEach(l -> l.onPrivateStateChanged(event));
      62           0 :     } catch (StorageException
      63             :         | PatchListNotAvailableException
      64             :         | GpgException
      65             :         | IOException
      66             :         | PermissionBackendException e) {
      67           0 :       logger.atSevere().withCause(e).log("Couldn't fire event");
      68          12 :     }
      69          12 :   }
      70             : 
      71             :   /** Event to be fired when the private flag of a change has been toggled. */
      72             :   private static class Event extends AbstractRevisionEvent
      73             :       implements PrivateStateChangedListener.Event {
      74             : 
      75             :     protected Event(ChangeInfo change, RevisionInfo revision, AccountInfo who, Instant when) {
      76          12 :       super(change, revision, who, when, NotifyHandling.ALL);
      77          12 :     }
      78             :   }
      79             : }

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