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.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.ApprovalInfo;
23 : import com.google.gerrit.extensions.common.ChangeInfo;
24 : import com.google.gerrit.extensions.common.RevisionInfo;
25 : import com.google.gerrit.extensions.events.ReviewerDeletedListener;
26 : import com.google.gerrit.server.GpgException;
27 : import com.google.gerrit.server.account.AccountState;
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 : import java.util.Map;
38 :
39 : /** Helper class to fire an event when a reviewer has been deleted from a change. */
40 : @Singleton
41 : public class ReviewerDeleted {
42 142 : private static final FluentLogger logger = FluentLogger.forEnclosingClass();
43 :
44 : private final PluginSetContext<ReviewerDeletedListener> listeners;
45 : private final EventUtil util;
46 :
47 : @Inject
48 142 : ReviewerDeleted(PluginSetContext<ReviewerDeletedListener> listeners, EventUtil util) {
49 142 : this.listeners = listeners;
50 142 : this.util = util;
51 142 : }
52 :
53 : public void fire(
54 : ChangeData changeData,
55 : PatchSet patchSet,
56 : AccountState reviewer,
57 : AccountState remover,
58 : String message,
59 : Map<String, Short> newApprovals,
60 : Map<String, Short> oldApprovals,
61 : NotifyHandling notify,
62 : Instant when) {
63 16 : if (listeners.isEmpty()) {
64 4 : return;
65 : }
66 : try {
67 12 : Event event =
68 : new Event(
69 12 : util.changeInfo(changeData),
70 12 : util.revisionInfo(changeData.project(), patchSet),
71 12 : util.accountInfo(reviewer),
72 12 : util.accountInfo(remover),
73 : message,
74 12 : util.approvals(reviewer, newApprovals, when),
75 12 : util.approvals(reviewer, oldApprovals, when),
76 : notify,
77 : when);
78 12 : listeners.runEach(l -> l.onReviewerDeleted(event));
79 0 : } catch (PatchListObjectTooLargeException e) {
80 0 : logger.atWarning().log("Couldn't fire event: %s", e.getMessage());
81 0 : } catch (PatchListNotAvailableException
82 : | GpgException
83 : | IOException
84 : | StorageException
85 : | PermissionBackendException e) {
86 0 : logger.atSevere().withCause(e).log("Couldn't fire event");
87 12 : }
88 12 : }
89 :
90 : /** Event to be fired when a reviewer has been deleted from a change. */
91 : private static class Event extends AbstractRevisionEvent
92 : implements ReviewerDeletedListener.Event {
93 : private final AccountInfo reviewer;
94 : private final String comment;
95 : private final Map<String, ApprovalInfo> newApprovals;
96 : private final Map<String, ApprovalInfo> oldApprovals;
97 :
98 : Event(
99 : ChangeInfo change,
100 : RevisionInfo revision,
101 : AccountInfo reviewer,
102 : AccountInfo remover,
103 : String comment,
104 : Map<String, ApprovalInfo> newApprovals,
105 : Map<String, ApprovalInfo> oldApprovals,
106 : NotifyHandling notify,
107 : Instant when) {
108 12 : super(change, revision, remover, when, notify);
109 12 : this.reviewer = reviewer;
110 12 : this.comment = comment;
111 12 : this.newApprovals = newApprovals;
112 12 : this.oldApprovals = oldApprovals;
113 12 : }
114 :
115 : @Override
116 : public AccountInfo getReviewer() {
117 12 : return reviewer;
118 : }
119 :
120 : @Override
121 : public String getComment() {
122 12 : return comment;
123 : }
124 :
125 : @Override
126 : public Map<String, ApprovalInfo> getNewApprovals() {
127 12 : return newApprovals;
128 : }
129 :
130 : @Override
131 : public Map<String, ApprovalInfo> getOldApprovals() {
132 12 : return oldApprovals;
133 : }
134 : }
135 : }
|