
* stable-2.15: Update git submodules Use Logger's built-in string formatting where possible Doc: Fix code example in JS API Allow some sections of the change list to overflow Fix more comparisons of current user Revert "WorkQueue: Add metrics" Fix permissions checks on Gerrit API on current user GroupCacheImpl: Fix log message when UUID is not found Update git submodules Update git submodules WorkQueue.Executor#buildMetrics: Remove MetricMaker parameter ChangeQueryBuilder: Allow ownerin predicate to be evaluated by the index Update git submodules Update git submodules Update git submodules ldap.Helper: Use local logger and make logger in LdapRealm private Remove ValidationError#createLoggerSink to avoid passing around loggers LdapLoginServlet: Improve exception handling OperatingSystemMXBeanProvider: Log exception for ReflectiveOperationException HttpPluginServlet: Don't trim leading whitespace from about.md content ProjectConfig: Don't use JGit's StringUtils to convert to lower case Do not abort indexing if < 50% projects failed Revert "AllChangesIndexer: Don't abort when failing to open repository" WorkQueue: Don't fail when queue metric already exists WorkQueue: Sanitize metric name when queue is created DropWizardMetricMaker: Introduce method to sanitize metric name VersionedAccountDestinations: Remove unused createSink(String) method ProjectBasicAuthFilter: Add comment why cause is not logged BazelBuild: Fix exception message when command was interrupted GitwebServlet: Write only one log entry for CGI errors GitwebServlet: Log unexpected errors on error level PostGpgKeys: Remove unneeded use of Joiner Remove some logs for errors that are rethrown DropWizardMetricMaker: Improve error messages for invalid arguments DropWizardMetricMaker: Improve error message when metric name is invalid AllChangesIndexer: Don't abort when failing to open repository Change-Id: I33e8a1c9498c6adecd4ffc7d8ef0cc78aeb28eea
147 lines
5.6 KiB
Java
147 lines
5.6 KiB
Java
// Copyright (C) 2015 The Android Open Source Project
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package com.google.gerrit.server.extensions.events;
|
|
|
|
import com.google.common.collect.ImmutableSet;
|
|
import com.google.common.collect.Sets;
|
|
import com.google.gerrit.extensions.client.ListChangesOption;
|
|
import com.google.gerrit.extensions.common.AccountInfo;
|
|
import com.google.gerrit.extensions.common.ApprovalInfo;
|
|
import com.google.gerrit.extensions.common.ChangeInfo;
|
|
import com.google.gerrit.extensions.common.RevisionInfo;
|
|
import com.google.gerrit.reviewdb.client.Account;
|
|
import com.google.gerrit.reviewdb.client.Change;
|
|
import com.google.gerrit.reviewdb.client.PatchSet;
|
|
import com.google.gerrit.reviewdb.client.Project;
|
|
import com.google.gerrit.reviewdb.server.ReviewDb;
|
|
import com.google.gerrit.server.GpgException;
|
|
import com.google.gerrit.server.account.AccountState;
|
|
import com.google.gerrit.server.change.ChangeJson;
|
|
import com.google.gerrit.server.patch.PatchListNotAvailableException;
|
|
import com.google.gerrit.server.permissions.PermissionBackendException;
|
|
import com.google.gerrit.server.query.change.ChangeData;
|
|
import com.google.gwtorm.server.OrmException;
|
|
import com.google.inject.Inject;
|
|
import com.google.inject.Provider;
|
|
import com.google.inject.Singleton;
|
|
import java.io.IOException;
|
|
import java.sql.Timestamp;
|
|
import java.util.EnumSet;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
@Singleton
|
|
public class EventUtil {
|
|
private static final Logger log = LoggerFactory.getLogger(EventUtil.class);
|
|
|
|
private static final ImmutableSet<ListChangesOption> CHANGE_OPTIONS;
|
|
|
|
static {
|
|
EnumSet<ListChangesOption> opts = EnumSet.allOf(ListChangesOption.class);
|
|
|
|
// Some options, like actions, are expensive to compute because they potentially have to walk
|
|
// lots of history and inspect lots of other changes.
|
|
opts.remove(ListChangesOption.CHANGE_ACTIONS);
|
|
opts.remove(ListChangesOption.CURRENT_ACTIONS);
|
|
|
|
// CHECK suppresses some exceptions on corrupt changes, which is not appropriate for passing
|
|
// through the event system as we would rather let them propagate.
|
|
opts.remove(ListChangesOption.CHECK);
|
|
|
|
CHANGE_OPTIONS = Sets.immutableEnumSet(opts);
|
|
}
|
|
|
|
private final ChangeData.Factory changeDataFactory;
|
|
private final Provider<ReviewDb> db;
|
|
private final ChangeJson.Factory changeJsonFactory;
|
|
|
|
@Inject
|
|
EventUtil(
|
|
ChangeJson.Factory changeJsonFactory,
|
|
ChangeData.Factory changeDataFactory,
|
|
Provider<ReviewDb> db) {
|
|
this.changeDataFactory = changeDataFactory;
|
|
this.db = db;
|
|
this.changeJsonFactory = changeJsonFactory;
|
|
}
|
|
|
|
public ChangeInfo changeInfo(Change change) throws OrmException {
|
|
return changeJsonFactory.create(CHANGE_OPTIONS).format(change);
|
|
}
|
|
|
|
public RevisionInfo revisionInfo(Project project, PatchSet ps)
|
|
throws OrmException, PatchListNotAvailableException, GpgException, IOException,
|
|
PermissionBackendException {
|
|
return revisionInfo(project.getNameKey(), ps);
|
|
}
|
|
|
|
public RevisionInfo revisionInfo(Project.NameKey project, PatchSet ps)
|
|
throws OrmException, PatchListNotAvailableException, GpgException, IOException,
|
|
PermissionBackendException {
|
|
ChangeData cd = changeDataFactory.create(db.get(), project, ps.getId().getParentKey());
|
|
return changeJsonFactory.create(CHANGE_OPTIONS).getRevisionInfo(cd, ps);
|
|
}
|
|
|
|
public AccountInfo accountInfo(AccountState accountState) {
|
|
if (accountState == null || accountState.getAccount().getId() == null) {
|
|
return null;
|
|
}
|
|
Account account = accountState.getAccount();
|
|
AccountInfo accountInfo = new AccountInfo(account.getId().get());
|
|
accountInfo.email = account.getPreferredEmail();
|
|
accountInfo.name = account.getFullName();
|
|
accountInfo.username = accountState.getUserName().orElse(null);
|
|
return accountInfo;
|
|
}
|
|
|
|
public Map<String, ApprovalInfo> approvals(
|
|
AccountState accountState, Map<String, Short> approvals, Timestamp ts) {
|
|
Map<String, ApprovalInfo> result = new HashMap<>();
|
|
for (Map.Entry<String, Short> e : approvals.entrySet()) {
|
|
Integer value = e.getValue() != null ? Integer.valueOf(e.getValue()) : null;
|
|
result.put(
|
|
e.getKey(),
|
|
ChangeJson.getApprovalInfo(accountState.getAccount().getId(), value, null, null, ts));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public void logEventListenerError(Object event, Object listener, Exception error) {
|
|
if (log.isDebugEnabled()) {
|
|
log.debug(
|
|
"Error in event listener {} for event {}",
|
|
listener.getClass().getName(),
|
|
event.getClass().getName(),
|
|
error);
|
|
} else {
|
|
log.warn(
|
|
"Error in event listener {} for event {}: {}",
|
|
listener.getClass().getName(),
|
|
event.getClass().getName(),
|
|
error.getMessage());
|
|
}
|
|
}
|
|
|
|
public static void logEventListenerError(Object listener, Exception error) {
|
|
if (log.isDebugEnabled()) {
|
|
log.debug("Error in event listener {}", listener.getClass().getName(), error);
|
|
} else {
|
|
log.warn("Error in event listener {}: {}", listener.getClass().getName(), error.getMessage());
|
|
}
|
|
}
|
|
}
|