LCOV - code coverage report
Current view: top level - server/extensions/events - RevisionCreated.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 25 28 89.3 %
Date: 2022-11-19 15:00:39 Functions: 7 8 87.5 %

          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.RevisionCreatedListener;
      25             : import com.google.gerrit.server.GpgException;
      26             : import com.google.gerrit.server.account.AccountState;
      27             : import com.google.gerrit.server.change.NotifyResolver;
      28             : import com.google.gerrit.server.patch.PatchListNotAvailableException;
      29             : import com.google.gerrit.server.patch.PatchListObjectTooLargeException;
      30             : import com.google.gerrit.server.permissions.PermissionBackendException;
      31             : import com.google.gerrit.server.plugincontext.PluginSetContext;
      32             : import com.google.gerrit.server.query.change.ChangeData;
      33             : import com.google.inject.Inject;
      34             : import com.google.inject.Singleton;
      35             : import java.io.IOException;
      36             : import java.time.Instant;
      37             : 
      38             : /** Helper class to fire an event when a revision has been created for a change. */
      39             : @Singleton
      40             : public class RevisionCreated {
      41         146 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      42             : 
      43         146 :   public static final RevisionCreated DISABLED =
      44         146 :       new RevisionCreated() {
      45             :         @Override
      46             :         public void fire(
      47             :             ChangeData changeData,
      48             :             PatchSet patchSet,
      49             :             AccountState uploader,
      50             :             Instant when,
      51           0 :             NotifyResolver.Result notify) {}
      52             :       };
      53             : 
      54             :   private final PluginSetContext<RevisionCreatedListener> listeners;
      55             :   private final EventUtil util;
      56             : 
      57             :   @Inject
      58         146 :   RevisionCreated(PluginSetContext<RevisionCreatedListener> listeners, EventUtil util) {
      59         146 :     this.listeners = listeners;
      60         146 :     this.util = util;
      61         146 :   }
      62             : 
      63         146 :   private RevisionCreated() {
      64         146 :     this.listeners = null;
      65         146 :     this.util = null;
      66         146 :   }
      67             : 
      68             :   public void fire(
      69             :       ChangeData changeData,
      70             :       PatchSet patchSet,
      71             :       AccountState uploader,
      72             :       Instant when,
      73             :       NotifyResolver.Result notify) {
      74         103 :     if (listeners.isEmpty()) {
      75           8 :       return;
      76             :     }
      77             :     try {
      78          95 :       Event event =
      79             :           new Event(
      80          95 :               util.changeInfo(changeData),
      81          95 :               util.revisionInfo(changeData.project(), patchSet),
      82          95 :               util.accountInfo(uploader),
      83             :               when,
      84          95 :               notify.handling());
      85          95 :       listeners.runEach(l -> l.onRevisionCreated(event));
      86           0 :     } catch (PatchListObjectTooLargeException e) {
      87           0 :       logger.atWarning().log("Couldn't fire event: %s", e.getMessage());
      88           1 :     } catch (PatchListNotAvailableException
      89             :         | GpgException
      90             :         | IOException
      91             :         | StorageException
      92             :         | PermissionBackendException e) {
      93           1 :       logger.atSevere().withCause(e).log("Couldn't fire event");
      94          95 :     }
      95          95 :   }
      96             : 
      97             :   /** Event to be fired when a revision has been created for a change. */
      98             :   private static class Event extends AbstractRevisionEvent
      99             :       implements RevisionCreatedListener.Event {
     100             : 
     101             :     Event(
     102             :         ChangeInfo change,
     103             :         RevisionInfo revision,
     104             :         AccountInfo uploader,
     105             :         Instant when,
     106             :         NotifyHandling notify) {
     107          95 :       super(change, revision, uploader, when, notify);
     108          95 :     }
     109             :   }
     110             : }

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