Add AccountInfo to the GitReferenceUpdatedListener.Event
Change-Id: I19d27885a5c75c7e22031e3f0e80fff33b20d7f6
This commit is contained in:

committed by
Yuxuan 'fishy' Wang

parent
f7d3a29ac5
commit
3b68afa0ca
@@ -38,6 +38,9 @@ java_library(
|
||||
java_library(
|
||||
name = 'api',
|
||||
srcs = glob([SRC + '**/*.java']),
|
||||
deps = [
|
||||
'//gerrit-common:annotations',
|
||||
],
|
||||
provided_deps = [
|
||||
'//lib/guice:guice',
|
||||
'//lib/guice:guice-assistedinject',
|
||||
|
@@ -14,7 +14,9 @@
|
||||
|
||||
package com.google.gerrit.extensions.events;
|
||||
|
||||
import com.google.gerrit.common.Nullable;
|
||||
import com.google.gerrit.extensions.annotations.ExtensionPoint;
|
||||
import com.google.gerrit.extensions.common.AccountInfo;
|
||||
|
||||
/** Notified when one or more references are modified. */
|
||||
@ExtensionPoint
|
||||
@@ -28,6 +30,10 @@ public interface GitReferenceUpdatedListener {
|
||||
boolean isCreate();
|
||||
boolean isDelete();
|
||||
boolean isNonFastForward();
|
||||
/**
|
||||
* The updater, could be null if it's the server.
|
||||
*/
|
||||
@Nullable AccountInfo getUpdater();
|
||||
}
|
||||
|
||||
void onGitReferenceUpdated(Event event);
|
||||
|
@@ -87,7 +87,7 @@ class ChangeProjectAccess extends ProjectAccessHandler<ProjectAccess> {
|
||||
RevCommit commit = config.commit(md);
|
||||
|
||||
gitRefUpdated.fire(config.getProject().getNameKey(), RefNames.REFS_CONFIG,
|
||||
base, commit.getId());
|
||||
base, commit.getId(), user.asIdentifiedUser().getAccount());
|
||||
hooks.doRefUpdatedHook(
|
||||
new Branch.NameKey(config.getProject().getNameKey(), RefNames.REFS_CONFIG),
|
||||
base, commit.getId(), user.asIdentifiedUser().getAccount());
|
||||
|
@@ -33,6 +33,7 @@ import com.google.gerrit.server.account.GroupIncludeCacheImpl;
|
||||
import com.google.gerrit.server.account.Realm;
|
||||
import com.google.gerrit.server.cache.CacheRemovalListener;
|
||||
import com.google.gerrit.server.cache.h2.DefaultCacheFactory;
|
||||
import com.google.gerrit.server.change.ChangeJson;
|
||||
import com.google.gerrit.server.change.ChangeKindCacheImpl;
|
||||
import com.google.gerrit.server.change.MergeabilityCacheImpl;
|
||||
import com.google.gerrit.server.change.PatchSetInserter;
|
||||
@@ -139,5 +140,8 @@ public class BatchProgramModule extends FactoryModule {
|
||||
factory(CapabilityControl.Factory.class);
|
||||
factory(ChangeData.Factory.class);
|
||||
factory(ProjectState.Factory.class);
|
||||
|
||||
bind(ChangeJson.Factory.class).toProvider(
|
||||
Providers.<ChangeJson.Factory>of(null));
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,45 @@
|
||||
// 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.gerrit.extensions.common.AccountInfo;
|
||||
import com.google.gerrit.reviewdb.client.Account;
|
||||
import com.google.gerrit.server.account.AccountCache;
|
||||
import com.google.inject.Inject;
|
||||
|
||||
public class EventUtil {
|
||||
|
||||
private final AccountCache accountCache;
|
||||
|
||||
@Inject
|
||||
EventUtil(AccountCache accountCache) {
|
||||
this.accountCache = accountCache;
|
||||
}
|
||||
|
||||
public AccountInfo accountInfo(Account a) {
|
||||
if (a == null || a.getId() == null) {
|
||||
return null;
|
||||
}
|
||||
AccountInfo ai = new AccountInfo(a.getId().get());
|
||||
ai.email = a.getPreferredEmail();
|
||||
ai.name = a.getFullName();
|
||||
ai.username = a.getUserName();
|
||||
return ai;
|
||||
}
|
||||
|
||||
public AccountInfo accountInfo(Account.Id accountId) {
|
||||
return accountInfo(accountCache.get(accountId).getAccount());
|
||||
}
|
||||
}
|
@@ -14,8 +14,10 @@
|
||||
|
||||
package com.google.gerrit.server.extensions.events;
|
||||
|
||||
import com.google.gerrit.extensions.common.AccountInfo;
|
||||
import com.google.gerrit.extensions.events.GitReferenceUpdatedListener;
|
||||
import com.google.gerrit.extensions.registration.DynamicSet;
|
||||
import com.google.gerrit.reviewdb.client.Account;
|
||||
import com.google.gerrit.reviewdb.client.Project;
|
||||
import com.google.inject.Inject;
|
||||
|
||||
@@ -26,42 +28,111 @@ import org.eclipse.jgit.transport.ReceiveCommand;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
public class GitReferenceUpdated {
|
||||
private static final Logger log = LoggerFactory
|
||||
.getLogger(GitReferenceUpdated.class);
|
||||
|
||||
public static final GitReferenceUpdated DISABLED = new GitReferenceUpdated(
|
||||
Collections.<GitReferenceUpdatedListener> emptyList());
|
||||
public static final GitReferenceUpdated DISABLED = new GitReferenceUpdated() {
|
||||
@Override
|
||||
public void fire(Project.NameKey project, RefUpdate refUpdate,
|
||||
ReceiveCommand.Type type, Account updater) {}
|
||||
|
||||
@Override
|
||||
public void fire(Project.NameKey project, RefUpdate refUpdate,
|
||||
ReceiveCommand.Type type, Account.Id updater) {}
|
||||
|
||||
@Override
|
||||
public void fire(Project.NameKey project, RefUpdate refUpdate,
|
||||
Account updater) {}
|
||||
|
||||
@Override
|
||||
public void fire(Project.NameKey project, RefUpdate refUpdate,
|
||||
AccountInfo updater) {}
|
||||
|
||||
@Override
|
||||
public void fire(Project.NameKey project, String ref, ObjectId oldObjectId,
|
||||
ObjectId newObjectId, Account updater) {}
|
||||
|
||||
@Override
|
||||
public void fire(Project.NameKey project, ReceiveCommand cmd,
|
||||
Account updater) {}
|
||||
|
||||
@Override
|
||||
public void fire(Project.NameKey project, BatchRefUpdate batchRefUpdate,
|
||||
Account.Id updater) {}
|
||||
};
|
||||
|
||||
private final Iterable<GitReferenceUpdatedListener> listeners;
|
||||
private final EventUtil util;
|
||||
|
||||
@Inject
|
||||
GitReferenceUpdated(DynamicSet<GitReferenceUpdatedListener> listeners) {
|
||||
GitReferenceUpdated(DynamicSet<GitReferenceUpdatedListener> listeners,
|
||||
EventUtil util) {
|
||||
this.listeners = listeners;
|
||||
this.util = util;
|
||||
}
|
||||
|
||||
GitReferenceUpdated(Iterable<GitReferenceUpdatedListener> listeners) {
|
||||
this.listeners = listeners;
|
||||
private GitReferenceUpdated() {
|
||||
this.listeners = null;
|
||||
this.util = null;
|
||||
}
|
||||
|
||||
public void fire(Project.NameKey project, RefUpdate refUpdate,
|
||||
ReceiveCommand.Type type) {
|
||||
ReceiveCommand.Type type, Account updater) {
|
||||
fire(project, refUpdate.getName(), refUpdate.getOldObjectId(),
|
||||
refUpdate.getNewObjectId(), type);
|
||||
refUpdate.getNewObjectId(), type, util.accountInfo(updater));
|
||||
}
|
||||
|
||||
public void fire(Project.NameKey project, RefUpdate refUpdate) {
|
||||
public void fire(Project.NameKey project, RefUpdate refUpdate,
|
||||
ReceiveCommand.Type type, Account.Id updater) {
|
||||
fire(project, refUpdate.getName(), refUpdate.getOldObjectId(),
|
||||
refUpdate.getNewObjectId(), ReceiveCommand.Type.UPDATE);
|
||||
refUpdate.getNewObjectId(), type, util.accountInfo(updater));
|
||||
}
|
||||
|
||||
public void fire(Project.NameKey project, RefUpdate refUpdate,
|
||||
Account updater) {
|
||||
fire(project, refUpdate.getName(), refUpdate.getOldObjectId(),
|
||||
refUpdate.getNewObjectId(), ReceiveCommand.Type.UPDATE,
|
||||
util.accountInfo(updater));
|
||||
}
|
||||
|
||||
public void fire(Project.NameKey project, RefUpdate refUpdate,
|
||||
AccountInfo updater) {
|
||||
fire(project, refUpdate.getName(), refUpdate.getOldObjectId(),
|
||||
refUpdate.getNewObjectId(), ReceiveCommand.Type.UPDATE, updater);
|
||||
}
|
||||
|
||||
public void fire(Project.NameKey project, String ref, ObjectId oldObjectId,
|
||||
ObjectId newObjectId, ReceiveCommand.Type type) {
|
||||
ObjectId newObjectId, Account updater) {
|
||||
fire(project, ref, oldObjectId, newObjectId, ReceiveCommand.Type.UPDATE,
|
||||
util.accountInfo(updater));
|
||||
}
|
||||
|
||||
public void fire(Project.NameKey project, ReceiveCommand cmd, Account updater) {
|
||||
fire(project, cmd.getRefName(), cmd.getOldId(), cmd.getNewId(), cmd.getType(),
|
||||
util.accountInfo(updater));
|
||||
}
|
||||
|
||||
public void fire(Project.NameKey project, BatchRefUpdate batchRefUpdate,
|
||||
Account.Id updater) {
|
||||
for (ReceiveCommand cmd : batchRefUpdate.getCommands()) {
|
||||
if (cmd.getResult() == ReceiveCommand.Result.OK) {
|
||||
fire(project, cmd, util.accountInfo(updater));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void fire(Project.NameKey project, ReceiveCommand cmd,
|
||||
AccountInfo updater) {
|
||||
fire(project, cmd.getRefName(), cmd.getOldId(), cmd.getNewId(), cmd.getType(),
|
||||
updater);
|
||||
}
|
||||
|
||||
private void fire(Project.NameKey project, String ref, ObjectId oldObjectId,
|
||||
ObjectId newObjectId, ReceiveCommand.Type type, AccountInfo updater) {
|
||||
ObjectId o = oldObjectId != null ? oldObjectId : ObjectId.zeroId();
|
||||
ObjectId n = newObjectId != null ? newObjectId : ObjectId.zeroId();
|
||||
Event event = new Event(project, ref, o.name(), n.name(), type);
|
||||
Event event = new Event(project, ref, o.name(), n.name(), type, updater);
|
||||
for (GitReferenceUpdatedListener l : listeners) {
|
||||
try {
|
||||
l.onGitReferenceUpdated(event);
|
||||
@@ -71,38 +142,24 @@ public class GitReferenceUpdated {
|
||||
}
|
||||
}
|
||||
|
||||
public void fire(Project.NameKey project, String ref, ObjectId oldObjectId,
|
||||
ObjectId newObjectId) {
|
||||
fire(project, ref, oldObjectId, newObjectId, ReceiveCommand.Type.UPDATE);
|
||||
}
|
||||
|
||||
public void fire(Project.NameKey project, ReceiveCommand cmd) {
|
||||
fire(project, cmd.getRefName(), cmd.getOldId(), cmd.getNewId(), cmd.getType());
|
||||
}
|
||||
|
||||
public void fire(Project.NameKey project, BatchRefUpdate batchRefUpdate) {
|
||||
for (ReceiveCommand cmd : batchRefUpdate.getCommands()) {
|
||||
if (cmd.getResult() == ReceiveCommand.Result.OK) {
|
||||
fire(project, cmd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class Event implements GitReferenceUpdatedListener.Event {
|
||||
private final String projectName;
|
||||
private final String ref;
|
||||
private final String oldObjectId;
|
||||
private final String newObjectId;
|
||||
private final ReceiveCommand.Type type;
|
||||
private final AccountInfo updater;
|
||||
|
||||
Event(Project.NameKey project, String ref,
|
||||
String oldObjectId, String newObjectId,
|
||||
ReceiveCommand.Type type) {
|
||||
ReceiveCommand.Type type,
|
||||
AccountInfo updater) {
|
||||
this.projectName = project.get();
|
||||
this.ref = ref;
|
||||
this.oldObjectId = oldObjectId;
|
||||
this.newObjectId = newObjectId;
|
||||
this.type = type;
|
||||
this.updater = updater;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -140,6 +197,11 @@ public class GitReferenceUpdated {
|
||||
return type == ReceiveCommand.Type.UPDATE_NONFASTFORWARD;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AccountInfo getUpdater() {
|
||||
return updater;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s[%s,%s: %s -> %s]", getClass().getSimpleName(),
|
||||
|
@@ -26,6 +26,7 @@ import com.google.common.util.concurrent.CheckedFuture;
|
||||
import com.google.gerrit.extensions.restapi.ResourceConflictException;
|
||||
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
|
||||
import com.google.gerrit.extensions.restapi.RestApiException;
|
||||
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;
|
||||
@@ -350,7 +351,10 @@ public class BatchUpdate implements AutoCloseable {
|
||||
// callers may assume a patch set ref being created means the change
|
||||
// was created, or a branch advancing meaning some changes were
|
||||
// closed.
|
||||
u.gitRefUpdated.fire(u.project, u.batchRefUpdate);
|
||||
u.gitRefUpdated.fire(
|
||||
u.project,
|
||||
u.batchRefUpdate,
|
||||
u.getUser().isIdentifiedUser() ? u.getUser().getAccountId() : null);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -125,8 +125,8 @@ public class MetaDataUpdate implements AutoCloseable {
|
||||
public MetaDataUpdate create(Project.NameKey name, Repository repository,
|
||||
IdentifiedUser user, BatchRefUpdate batch) {
|
||||
MetaDataUpdate md = factory.create(name, repository, batch);
|
||||
md.getCommitBuilder().setAuthor(createPersonIdent(user));
|
||||
md.getCommitBuilder().setCommitter(serverIdent);
|
||||
md.setAuthor(user);
|
||||
return md;
|
||||
}
|
||||
|
||||
@@ -176,6 +176,7 @@ public class MetaDataUpdate implements AutoCloseable {
|
||||
private final CommitBuilder commit;
|
||||
private boolean allowEmpty;
|
||||
private boolean insertChangeId;
|
||||
private IdentifiedUser author;
|
||||
|
||||
@AssistedInject
|
||||
public MetaDataUpdate(GitReferenceUpdated gitRefUpdated,
|
||||
@@ -198,8 +199,9 @@ public class MetaDataUpdate implements AutoCloseable {
|
||||
getCommitBuilder().setMessage(message);
|
||||
}
|
||||
|
||||
public void setAuthor(IdentifiedUser user) {
|
||||
getCommitBuilder().setAuthor(user.newCommitterIdent(
|
||||
public void setAuthor(IdentifiedUser author) {
|
||||
this.author = author;
|
||||
getCommitBuilder().setAuthor(author.newCommitterIdent(
|
||||
getCommitBuilder().getCommitter().getWhen(),
|
||||
getCommitBuilder().getCommitter().getTimeZone()));
|
||||
}
|
||||
@@ -244,6 +246,7 @@ public class MetaDataUpdate implements AutoCloseable {
|
||||
}
|
||||
|
||||
void fireGitRefUpdatedEvent(RefUpdate ru) {
|
||||
gitRefUpdated.fire(projectName, ru);
|
||||
gitRefUpdated.fire(
|
||||
projectName, ru, author == null ? null : author.getAccount());
|
||||
}
|
||||
}
|
||||
|
@@ -14,6 +14,7 @@
|
||||
|
||||
package com.google.gerrit.server.git;
|
||||
|
||||
import com.google.gerrit.extensions.common.AccountInfo;
|
||||
import com.google.gerrit.reviewdb.client.Project;
|
||||
import com.google.gerrit.server.GerritPersonIdent;
|
||||
import com.google.gerrit.server.extensions.events.GitReferenceUpdated;
|
||||
@@ -261,7 +262,7 @@ public class NotesBranchUtil {
|
||||
throw new IOException("Couldn't update " + notesBranch + ". "
|
||||
+ result.name());
|
||||
} else {
|
||||
gitRefUpdated.fire(project, refUpdate);
|
||||
gitRefUpdated.fire(project, refUpdate, (AccountInfo) null);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@@ -667,7 +667,7 @@ public class ReceiveCommits {
|
||||
// We only fire gitRefUpdated for direct refs updates.
|
||||
// Events for change refs are fired when they are created.
|
||||
//
|
||||
gitRefUpdated.fire(project.getNameKey(), c);
|
||||
gitRefUpdated.fire(project.getNameKey(), c, user.getAccount());
|
||||
hooks.doRefUpdatedHook(
|
||||
new Branch.NameKey(project.getNameKey(), c.getRefName()),
|
||||
c.getOldId(),
|
||||
@@ -2180,7 +2180,7 @@ public class ReceiveCommits {
|
||||
|
||||
PatchSet newPatchSet = replaceOp.getPatchSet();
|
||||
gitRefUpdated.fire(project.getNameKey(), newPatchSet.getRefName(),
|
||||
ObjectId.zeroId(), newCommit);
|
||||
ObjectId.zeroId(), newCommit, user.getAccount());
|
||||
|
||||
if (magicBranch != null && magicBranch.submit) {
|
||||
submit(changeCtl, newPatchSet);
|
||||
|
@@ -324,7 +324,7 @@ public class SubmoduleOp {
|
||||
switch (rfu.update()) {
|
||||
case NEW:
|
||||
case FAST_FORWARD:
|
||||
gitRefUpdated.fire(subscriber.getParentKey(), rfu);
|
||||
gitRefUpdated.fire(subscriber.getParentKey(), rfu, account);
|
||||
changeHooks.doRefUpdatedHook(subscriber, rfu, account);
|
||||
// TODO since this is performed "in the background" no mail will be
|
||||
// sent to inform users about the updated branch
|
||||
|
@@ -148,7 +148,9 @@ public class CreateBranch implements RestModifyView<ProjectResource, Input> {
|
||||
case FAST_FORWARD:
|
||||
case NEW:
|
||||
case NO_CHANGE:
|
||||
referenceUpdated.fire(name.getParentKey(), u, ReceiveCommand.Type.CREATE);
|
||||
referenceUpdated.fire(
|
||||
name.getParentKey(), u, ReceiveCommand.Type.CREATE,
|
||||
identifiedUser.get().getAccount());
|
||||
hooks.doRefUpdatedHook(name, u, identifiedUser.get().getAccount());
|
||||
break;
|
||||
case LOCK_FAILURE:
|
||||
|
@@ -370,7 +370,8 @@ public class CreateProject implements RestModifyView<TopLevelResource, ProjectIn
|
||||
Result result = ru.update();
|
||||
switch (result) {
|
||||
case NEW:
|
||||
referenceUpdated.fire(project, ru, ReceiveCommand.Type.CREATE);
|
||||
referenceUpdated.fire(project, ru, ReceiveCommand.Type.CREATE,
|
||||
currentUser.get().getAccountId());
|
||||
break;
|
||||
case FAST_FORWARD:
|
||||
case FORCED:
|
||||
|
@@ -108,7 +108,8 @@ public class DeleteBranch implements RestModifyView<BranchResource, Input> {
|
||||
case NO_CHANGE:
|
||||
case FAST_FORWARD:
|
||||
case FORCED:
|
||||
referenceUpdated.fire(rsrc.getNameKey(), u, ReceiveCommand.Type.DELETE);
|
||||
referenceUpdated.fire(rsrc.getNameKey(), u, ReceiveCommand.Type.DELETE,
|
||||
identifiedUser.get().getAccount());
|
||||
hooks.doRefUpdatedHook(rsrc.getBranchKey(), u, identifiedUser.get().getAccount());
|
||||
break;
|
||||
|
||||
|
@@ -162,7 +162,8 @@ class DeleteBranches implements RestModifyView<ProjectResource, Input> {
|
||||
}
|
||||
|
||||
private void postDeletion(ProjectResource project, ReceiveCommand cmd) {
|
||||
referenceUpdated.fire(project.getNameKey(), cmd);
|
||||
referenceUpdated.fire(project.getNameKey(), cmd,
|
||||
identifiedUser.get().getAccount());
|
||||
Branch.NameKey branchKey =
|
||||
new Branch.NameKey(project.getNameKey(), cmd.getRefName());
|
||||
hooks.doRefUpdatedHook(branchKey, cmd.getOldId(), cmd.getNewId(),
|
||||
|
@@ -196,7 +196,7 @@ public class PutConfig implements RestModifyView<ProjectResource, Input> {
|
||||
// Only fire hook if project was actually changed.
|
||||
if (!Objects.equals(baseRev, commitRev)) {
|
||||
gitRefUpdated.fire(projectName, RefNames.REFS_CONFIG,
|
||||
baseRev, commitRev);
|
||||
baseRev, commitRev, user.get().asIdentifiedUser().getAccount());
|
||||
hooks.doRefUpdatedHook(
|
||||
new Branch.NameKey(projectName, RefNames.REFS_CONFIG),
|
||||
baseRev, commitRev, user.get().asIdentifiedUser().getAccount());
|
||||
|
@@ -94,7 +94,7 @@ public class PutDescription implements RestModifyView<ProjectResource, PutDescri
|
||||
// Only fire hook if project was actually changed.
|
||||
if (!Objects.equals(baseRev, commitRev)) {
|
||||
gitRefUpdated.fire(resource.getNameKey(), RefNames.REFS_CONFIG,
|
||||
baseRev, commitRev);
|
||||
baseRev, commitRev, user.getAccount());
|
||||
hooks.doRefUpdatedHook(
|
||||
new Branch.NameKey(resource.getNameKey(), RefNames.REFS_CONFIG),
|
||||
baseRev, commitRev, user.getAccount());
|
||||
|
Reference in New Issue
Block a user