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.ChangeMergedListener; 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 merged. */ 38 : @Singleton 39 : public class ChangeMerged { 40 142 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 41 : 42 : private final PluginSetContext<ChangeMergedListener> listeners; 43 : private final EventUtil util; 44 : 45 : @Inject 46 142 : ChangeMerged(PluginSetContext<ChangeMergedListener> listeners, EventUtil util) { 47 142 : this.listeners = listeners; 48 142 : this.util = util; 49 142 : } 50 : 51 : public void fire( 52 : ChangeData changeData, PatchSet ps, AccountState merger, String newRevisionId, Instant when) { 53 55 : if (listeners.isEmpty()) { 54 0 : return; 55 : } 56 : try { 57 55 : Event event = 58 : new Event( 59 55 : util.changeInfo(changeData), 60 55 : util.revisionInfo(changeData.project(), ps), 61 55 : util.accountInfo(merger), 62 : newRevisionId, 63 : when); 64 55 : listeners.runEach(l -> l.onChangeMerged(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 55 : } 74 55 : } 75 : 76 : /** Event to be fired when a change has been merged. */ 77 : private static class Event extends AbstractRevisionEvent implements ChangeMergedListener.Event { 78 : private final String newRevisionId; 79 : 80 : Event( 81 : ChangeInfo change, 82 : RevisionInfo revision, 83 : AccountInfo merger, 84 : String newRevisionId, 85 : Instant when) { 86 55 : super(change, revision, merger, when, NotifyHandling.ALL); 87 55 : this.newRevisionId = newRevisionId; 88 55 : } 89 : 90 : @Override 91 : public String getNewRevisionId() { 92 51 : return newRevisionId; 93 : } 94 : } 95 : }