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.WorkInProgressStateChangedListener; 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 work-in-progress state of a change has been toggled. */ 37 : @Singleton 38 : public class WorkInProgressStateChanged { 39 143 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 40 : 41 143 : public static final WorkInProgressStateChanged DISABLED = 42 143 : new WorkInProgressStateChanged() { 43 : @Override 44 : public void fire( 45 0 : ChangeData changeData, PatchSet patchSet, AccountState account, Instant when) {} 46 : }; 47 : 48 : private final PluginSetContext<WorkInProgressStateChangedListener> listeners; 49 : private final EventUtil util; 50 : 51 : @Inject 52 : WorkInProgressStateChanged( 53 143 : PluginSetContext<WorkInProgressStateChangedListener> listeners, EventUtil util) { 54 143 : this.listeners = listeners; 55 143 : this.util = util; 56 143 : } 57 : 58 143 : private WorkInProgressStateChanged() { 59 143 : this.listeners = null; 60 143 : this.util = null; 61 143 : } 62 : 63 : public void fire(ChangeData changeData, PatchSet patchSet, AccountState account, Instant when) { 64 12 : if (listeners.isEmpty()) { 65 4 : return; 66 : } 67 : try { 68 8 : Event event = 69 : new Event( 70 8 : util.changeInfo(changeData), 71 8 : util.revisionInfo(changeData.project(), patchSet), 72 8 : util.accountInfo(account), 73 : when); 74 8 : listeners.runEach(l -> l.onWorkInProgressStateChanged(event)); 75 0 : } catch (StorageException 76 : | PatchListNotAvailableException 77 : | GpgException 78 : | IOException 79 : | PermissionBackendException e) { 80 0 : logger.atSevere().withCause(e).log("Couldn't fire event"); 81 8 : } 82 8 : } 83 : 84 : /** Event to be fired when the work-in-progress state of a change has been toggled. */ 85 : private static class Event extends AbstractRevisionEvent 86 : implements WorkInProgressStateChangedListener.Event { 87 : 88 : protected Event(ChangeInfo change, RevisionInfo revision, AccountInfo who, Instant when) { 89 8 : super(change, revision, who, when, NotifyHandling.ALL); 90 8 : } 91 : } 92 : }