LCOV - code coverage report
Current view: top level - server/extensions/events - AssigneeChanged.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 18 20 90.0 %
Date: 2022-11-19 15:00:39 Functions: 6 6 100.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.AccountInfo;
      21             : import com.google.gerrit.extensions.common.ChangeInfo;
      22             : import com.google.gerrit.extensions.events.AssigneeChangedListener;
      23             : import com.google.gerrit.server.account.AccountState;
      24             : import com.google.gerrit.server.plugincontext.PluginSetContext;
      25             : import com.google.gerrit.server.query.change.ChangeData;
      26             : import com.google.inject.Inject;
      27             : import com.google.inject.Singleton;
      28             : import java.time.Instant;
      29             : 
      30             : /** Helper class to fire an event when a user has been set as assignee on a change. */
      31             : @Singleton
      32             : public class AssigneeChanged {
      33         145 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      34             : 
      35             :   private final PluginSetContext<AssigneeChangedListener> listeners;
      36             :   private final EventUtil util;
      37             : 
      38             :   @Inject
      39         145 :   AssigneeChanged(PluginSetContext<AssigneeChangedListener> listeners, EventUtil util) {
      40         145 :     this.listeners = listeners;
      41         145 :     this.util = util;
      42         145 :   }
      43             : 
      44             :   public void fire(
      45             :       ChangeData changeData, AccountState accountState, AccountState oldAssignee, Instant when) {
      46           7 :     if (listeners.isEmpty()) {
      47           4 :       return;
      48             :     }
      49             :     try {
      50           3 :       Event event =
      51             :           new Event(
      52           3 :               util.changeInfo(changeData),
      53           3 :               util.accountInfo(accountState),
      54           3 :               util.accountInfo(oldAssignee),
      55             :               when);
      56           3 :       listeners.runEach(l -> l.onAssigneeChanged(event));
      57           0 :     } catch (StorageException e) {
      58           0 :       logger.atSevere().withCause(e).log("Couldn't fire event");
      59           3 :     }
      60           3 :   }
      61             : 
      62             :   /** Event to be fired when a user has been set as assignee on a change. */
      63             :   private static class Event extends AbstractChangeEvent implements AssigneeChangedListener.Event {
      64             :     private final AccountInfo oldAssignee;
      65             : 
      66             :     Event(ChangeInfo change, AccountInfo editor, AccountInfo oldAssignee, Instant when) {
      67           3 :       super(change, editor, when, NotifyHandling.ALL);
      68           3 :       this.oldAssignee = oldAssignee;
      69           3 :     }
      70             : 
      71             :     @Override
      72             :     public AccountInfo getOldAssignee() {
      73           3 :       return oldAssignee;
      74             :     }
      75             :   }
      76             : }

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