Switch to EventListener
EventListener provides more information than what is exposed by
the GitReferenceUpdatedListener, while still providing the same
core information. This switches to the generic EventListener
interface.
Change-Id: I891bcdcb923d854fd8f60ab8d3bcff1056bf9267
diff --git a/src/main/java/com/googlesource/gerrit/plugins/refprotection/BackupRef.java b/src/main/java/com/googlesource/gerrit/plugins/refprotection/BackupRef.java
index e4d7fc8..f32f597 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/refprotection/BackupRef.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/refprotection/BackupRef.java
@@ -24,11 +24,11 @@
package com.googlesource.gerrit.plugins.refprotection;
import com.google.gerrit.extensions.annotations.PluginName;
-import com.google.gerrit.extensions.events.GitReferenceUpdatedListener.Event;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.server.config.PluginConfigFactory;
+import com.google.gerrit.server.events.RefUpdatedEvent;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.project.CreateBranch;
import com.google.gerrit.server.project.ProjectResource;
@@ -62,7 +62,7 @@
this.createBranchFactory = createBranchFactory;
}
- public void createBackup(Event event, ProjectResource project) {
+ public void createBackup(RefUpdatedEvent event, ProjectResource project) {
String refName = event.getRefName();
String backupRef = get(project, refName);
@@ -73,7 +73,7 @@
CreateBranch.Input input = new CreateBranch.Input();
input.ref = backupRef;
- input.revision = event.getOldObjectId();
+ input.revision = event.refUpdate.oldRev;
try {
createBranchFactory.create(backupRef).apply(project, input);
diff --git a/src/main/java/com/googlesource/gerrit/plugins/refprotection/RefProtectionModule.java b/src/main/java/com/googlesource/gerrit/plugins/refprotection/RefProtectionModule.java
index 48de79a..b2700c7 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/refprotection/RefProtectionModule.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/refprotection/RefProtectionModule.java
@@ -23,14 +23,14 @@
*/
package com.googlesource.gerrit.plugins.refprotection;
-import com.google.gerrit.extensions.events.GitReferenceUpdatedListener;
+import com.google.gerrit.common.EventListener;
import com.google.gerrit.extensions.registration.DynamicSet;
import com.google.inject.AbstractModule;
public class RefProtectionModule extends AbstractModule {
@Override
protected void configure() {
- DynamicSet.bind(binder(), GitReferenceUpdatedListener.class).to(
+ DynamicSet.bind(binder(), EventListener.class).to(
RefUpdateListener.class);
requestStaticInjection(BackupRef.class);
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/refprotection/RefUpdateListener.java b/src/main/java/com/googlesource/gerrit/plugins/refprotection/RefUpdateListener.java
index cbaffad..0ca08f4 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/refprotection/RefUpdateListener.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/refprotection/RefUpdateListener.java
@@ -27,10 +27,12 @@
import static org.eclipse.jgit.lib.Constants.R_TAGS;
import com.google.gerrit.extensions.annotations.PluginName;
-import com.google.gerrit.extensions.events.GitReferenceUpdatedListener;
+import com.google.gerrit.common.EventListener;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.config.PluginConfigFactory;
+import com.google.gerrit.server.events.Event;
+import com.google.gerrit.server.events.RefUpdatedEvent;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.project.NoSuchProjectException;
import com.google.gerrit.server.project.ProjectControl;
@@ -47,7 +49,7 @@
import java.io.IOException;
-class RefUpdateListener implements GitReferenceUpdatedListener {
+class RefUpdateListener implements EventListener {
private static final Logger log =
LoggerFactory.getLogger(RefUpdateListener.class);
@@ -76,18 +78,22 @@
}
@Override
- public void onGitReferenceUpdated(final Event event) {
- if ((protectDeleted || protectFastForward) && isRelevantRef(event)) {
- Project.NameKey nameKey = new Project.NameKey(event.getProjectName());
- try {
- ProjectResource project =
- new ProjectResource(projectControl.controlFor(nameKey, user));
- if ((protectDeleted && isRefDeleted(event))
- || (protectFastForward && isNonFastForwardUpdate(event, project))) {
- backupRef.createBackup(event, project);
+ public void onEvent(Event event) {
+ if (event instanceof RefUpdatedEvent) {
+ RefUpdatedEvent refUpdate = (RefUpdatedEvent)event;
+ if ((protectDeleted || protectFastForward) && isRelevantRef(refUpdate)) {
+ Project.NameKey nameKey = refUpdate.getProjectNameKey();
+ try {
+ ProjectResource project =
+ new ProjectResource(projectControl.controlFor(nameKey, user));
+ if ((protectDeleted && isRefDeleted(refUpdate))
+ || (protectFastForward && isNonFastForwardUpdate(refUpdate,
+ project))) {
+ backupRef.createBackup(refUpdate, project);
+ }
+ } catch (NoSuchProjectException | IOException e) {
+ log.error(e.getMessage(), e);
}
- } catch (NoSuchProjectException | IOException e) {
- log.error(e.getMessage(), e);
}
}
}
@@ -98,7 +104,7 @@
* @param event the Event
* @return True if relevant, otherwise False.
*/
- private boolean isRelevantRef(Event event) {
+ private boolean isRelevantRef(RefUpdatedEvent event) {
return (!isNewRef(event)) &&
(event.getRefName().startsWith(R_HEADS)
|| event.getRefName().startsWith(R_TAGS));
@@ -110,8 +116,8 @@
* @param event the Event
* @return True if a new ref, otherwise False.
*/
- private boolean isNewRef(Event event) {
- return event.getOldObjectId().equals(ObjectId.zeroId().getName());
+ private boolean isNewRef(RefUpdatedEvent event) {
+ return event.refUpdate.oldRev.equals(ObjectId.zeroId().getName());
}
/**
@@ -120,11 +126,11 @@
* @param event the Event
* @return True if a ref deletion, otherwise False.
*/
- private boolean isRefDeleted(Event event) {
- if (event.getNewObjectId().equals(ObjectId.zeroId().getName())) {
+ private boolean isRefDeleted(RefUpdatedEvent event) {
+ if (event.refUpdate.newRev.equals(ObjectId.zeroId().getName())) {
log.info(String.format(
"Ref Deleted: project [%s] refname [%s] old object id [%s]",
- event.getProjectName(), event.getRefName(), event.getOldObjectId()));
+ event.getProjectNameKey().toString(), event.getRefName(), event.refUpdate.oldRev));
return true;
}
return false;
@@ -136,7 +142,7 @@
* @param event the Event
* @return True if a non-fast-forward update, otherwise False.
*/
- private boolean isNonFastForwardUpdate(Event event, ProjectResource project)
+ private boolean isNonFastForwardUpdate(RefUpdatedEvent event, ProjectResource project)
throws RepositoryNotFoundException, IOException {
if (isRefDeleted(event)) {
// Can't be non-fast-forward if the ref was deleted, and
@@ -146,9 +152,9 @@
try (Repository repo = repoManager.openRepository(project.getNameKey())) {
try (RevWalk walk = new RevWalk(repo)) {
RevCommit oldCommit =
- walk.parseCommit(repo.resolve(event.getOldObjectId()));
+ walk.parseCommit(repo.resolve(event.refUpdate.oldRev));
RevCommit newCommit =
- walk.parseCommit(repo.resolve(event.getNewObjectId()));
+ walk.parseCommit(repo.resolve(event.refUpdate.newRev));
return !walk.isMergedInto(oldCommit, newCommit);
}
}