AssigneeValidator example
Will not allow a user to be assigned to more than 5 changes.
Change-Id: I1887f7007e66bd5ccac207c7789be2712ffbd00a
diff --git a/src/main/java/com/googlesource/gerrit/plugins/cookbook/AssigneeValidator.java b/src/main/java/com/googlesource/gerrit/plugins/cookbook/AssigneeValidator.java
new file mode 100644
index 0000000..d47dee4
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/cookbook/AssigneeValidator.java
@@ -0,0 +1,62 @@
+// Copyright (C) 2014 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.googlesource.gerrit.plugins.cookbook;
+
+import com.google.gerrit.reviewdb.client.Account;
+import com.google.gerrit.reviewdb.client.Change;
+import com.google.gerrit.server.query.QueryParseException;
+import com.google.gerrit.server.query.change.ChangeQueryBuilder;
+import com.google.gerrit.server.query.change.ChangeQueryProcessor;
+import com.google.gerrit.server.validators.AssigneeValidationListener;
+import com.google.gerrit.server.validators.ValidationException;
+import com.google.gwtorm.server.OrmException;
+import com.google.inject.Inject;
+
+import org.eclipse.jgit.util.io.DisabledOutputStream;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.OutputStream;
+import java.io.PrintWriter;
+import java.util.Set;
+
+public class AssigneeValidator implements AssigneeValidationListener {
+ private static final Logger log =
+ LoggerFactory.getLogger(AssigneeValidationListener.class);
+
+ private static int MAX_ASSIGNED_CHANGES = 5;
+
+ @Inject
+ ChangeQueryBuilder queryBuilder;
+
+ @Inject
+ ChangeQueryProcessor queryProcessor;
+
+ @Override
+ public void validateAssignee(Change change, Account assignee)
+ throws ValidationException {
+ try {
+ if (queryProcessor
+ .query(queryBuilder.assignee(assignee.getPreferredEmail())).entities()
+ .size() > MAX_ASSIGNED_CHANGES) {
+ throw new ValidationException("Cannot assign user to more than "
+ + MAX_ASSIGNED_CHANGES + " changes");
+ }
+ } catch (OrmException | QueryParseException e) {
+ log.error("Failed to validate assignee for change " + change.getId(), e);
+ // Allow assignee.
+ }
+ }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/cookbook/Module.java b/src/main/java/com/googlesource/gerrit/plugins/cookbook/Module.java
index b17c871..7ac8eb3 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/cookbook/Module.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/cookbook/Module.java
@@ -42,6 +42,7 @@
import com.google.gerrit.server.git.validators.UploadValidationListener;
import com.google.gerrit.server.plugins.ServerPluginProvider;
import com.google.gerrit.server.query.change.ChangeQueryBuilder.ChangeOperatorFactory;
+import com.google.gerrit.server.validators.AssigneeValidationListener;
import com.google.gerrit.server.validators.HashtagValidationListener;
import com.google.inject.AbstractModule;
@@ -75,6 +76,8 @@
.to(MergeUserValidator.class);
DynamicSet.bind(binder(), HashtagValidationListener.class)
.to(HashtagValidator.class);
+ DynamicSet.bind(binder(), AssigneeValidationListener.class)
+ .to(AssigneeValidator.class);
DynamicSet.bind(binder(), CommitValidationListener.class)
.to(CommitValidator.class);
DynamicSet.bind(binder(), NewProjectCreatedListener.class)