LCOV - code coverage report
Current view: top level - server/extensions/events - VoteDeleted.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 26 31 83.9 %
Date: 2022-11-19 15:00:39 Functions: 9 9 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.common.Nullable;
      19             : import com.google.gerrit.entities.PatchSet;
      20             : import com.google.gerrit.exceptions.StorageException;
      21             : import com.google.gerrit.extensions.api.changes.NotifyHandling;
      22             : import com.google.gerrit.extensions.common.AccountInfo;
      23             : import com.google.gerrit.extensions.common.ApprovalInfo;
      24             : import com.google.gerrit.extensions.common.ChangeInfo;
      25             : import com.google.gerrit.extensions.common.RevisionInfo;
      26             : import com.google.gerrit.extensions.events.VoteDeletedListener;
      27             : import com.google.gerrit.server.GpgException;
      28             : import com.google.gerrit.server.account.AccountState;
      29             : import com.google.gerrit.server.patch.PatchListNotAvailableException;
      30             : import com.google.gerrit.server.patch.PatchListObjectTooLargeException;
      31             : import com.google.gerrit.server.permissions.PermissionBackendException;
      32             : import com.google.gerrit.server.plugincontext.PluginSetContext;
      33             : import com.google.gerrit.server.query.change.ChangeData;
      34             : import com.google.inject.Inject;
      35             : import com.google.inject.Singleton;
      36             : import java.io.IOException;
      37             : import java.time.Instant;
      38             : import java.util.Map;
      39             : 
      40             : /** Helper class to fire an event when a vote has been deleted from a change. */
      41             : @Singleton
      42             : public class VoteDeleted {
      43         138 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      44             : 
      45             :   private final PluginSetContext<VoteDeletedListener> listeners;
      46             :   private final EventUtil util;
      47             : 
      48             :   @Inject
      49         138 :   VoteDeleted(PluginSetContext<VoteDeletedListener> listeners, EventUtil util) {
      50         138 :     this.listeners = listeners;
      51         138 :     this.util = util;
      52         138 :   }
      53             : 
      54             :   public void fire(
      55             :       ChangeData changeData,
      56             :       PatchSet ps,
      57             :       AccountState reviewer,
      58             :       Map<String, Short> approvals,
      59             :       Map<String, Short> oldApprovals,
      60             :       NotifyHandling notify,
      61             :       String message,
      62             :       @Nullable AccountState remover,
      63             :       Instant when) {
      64           9 :     if (listeners.isEmpty()) {
      65           0 :       return;
      66             :     }
      67             :     try {
      68           9 :       Event event =
      69             :           new Event(
      70           9 :               util.changeInfo(changeData),
      71           9 :               util.revisionInfo(changeData.project(), ps),
      72           9 :               util.accountInfo(reviewer),
      73           9 :               util.approvals(reviewer, approvals, when),
      74           9 :               util.approvals(reviewer, oldApprovals, when),
      75             :               notify,
      76             :               message,
      77           9 :               util.accountInfo(remover),
      78             :               when);
      79           9 :       listeners.runEach(l -> l.onVoteDeleted(event));
      80           0 :     } catch (PatchListObjectTooLargeException e) {
      81           0 :       logger.atWarning().log("Couldn't fire event: %s", e.getMessage());
      82           0 :     } catch (PatchListNotAvailableException
      83             :         | GpgException
      84             :         | IOException
      85             :         | StorageException
      86             :         | PermissionBackendException e) {
      87           0 :       logger.atSevere().withCause(e).log("Couldn't fire event");
      88           9 :     }
      89           9 :   }
      90             : 
      91             :   /** Event to be fired when a vote has been deleted from a change. */
      92             :   private static class Event extends AbstractRevisionEvent implements VoteDeletedListener.Event {
      93             :     private final AccountInfo reviewer;
      94             :     private final Map<String, ApprovalInfo> approvals;
      95             :     private final Map<String, ApprovalInfo> oldApprovals;
      96             :     private final String message;
      97             : 
      98             :     Event(
      99             :         ChangeInfo change,
     100             :         RevisionInfo revision,
     101             :         AccountInfo reviewer,
     102             :         Map<String, ApprovalInfo> approvals,
     103             :         Map<String, ApprovalInfo> oldApprovals,
     104             :         NotifyHandling notify,
     105             :         String message,
     106             :         AccountInfo remover,
     107             :         Instant when) {
     108           9 :       super(change, revision, remover, when, notify);
     109           9 :       this.reviewer = reviewer;
     110           9 :       this.approvals = approvals;
     111           9 :       this.oldApprovals = oldApprovals;
     112           9 :       this.message = message;
     113           9 :     }
     114             : 
     115             :     @Override
     116             :     public Map<String, ApprovalInfo> getApprovals() {
     117           9 :       return approvals;
     118             :     }
     119             : 
     120             :     @Override
     121             :     public Map<String, ApprovalInfo> getOldApprovals() {
     122           9 :       return oldApprovals;
     123             :     }
     124             : 
     125             :     @Override
     126             :     public String getMessage() {
     127           9 :       return message;
     128             :     }
     129             : 
     130             :     @Override
     131             :     public AccountInfo getReviewer() {
     132           9 :       return reviewer;
     133             :     }
     134             :   }
     135             : }

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