Add validator that checks for required footers

Project owner can now configure required footers. Commits that miss
any of the required footers in the commit message are rejected on
push.

Change-Id: Id84c9526a331492d41f0f007d477f828a0c9994d
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/FooterValidator.java b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/FooterValidator.java
new file mode 100644
index 0000000..fcffa47
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/FooterValidator.java
@@ -0,0 +1,85 @@
+// 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.uploadvalidator;
+
+import com.google.common.base.Function;
+import com.google.common.collect.FluentIterable;
+import com.google.gerrit.extensions.annotations.PluginName;
+import com.google.gerrit.server.config.PluginConfig;
+import com.google.gerrit.server.config.PluginConfigFactory;
+import com.google.gerrit.server.events.CommitReceivedEvent;
+import com.google.gerrit.server.git.validators.CommitValidationException;
+import com.google.gerrit.server.git.validators.CommitValidationListener;
+import com.google.gerrit.server.git.validators.CommitValidationMessage;
+import com.google.gerrit.server.project.NoSuchProjectException;
+import com.google.inject.Inject;
+
+import org.eclipse.jgit.revwalk.FooterLine;
+
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+
+public class FooterValidator implements CommitValidationListener {
+  public static String KEY_REQUIRED_FOOTER = "requiredFooter";
+
+  private final String pluginName;
+  private final PluginConfigFactory cfgFactory;
+
+  @Inject
+  FooterValidator(@PluginName String pluginName, PluginConfigFactory cfgFactory) {
+    this.pluginName = pluginName;
+    this.cfgFactory = cfgFactory;
+  }
+
+  @Override
+  public List<CommitValidationMessage> onCommitReceived(
+      CommitReceivedEvent receiveEvent) throws CommitValidationException {
+    try {
+      PluginConfig cfg =
+          cfgFactory.getFromProjectConfig(
+              receiveEvent.project.getNameKey(), pluginName);
+      String[] requiredFooters =
+          cfg.getStringList(KEY_REQUIRED_FOOTER);
+      if (requiredFooters.length > 0) {
+        List<CommitValidationMessage> messages = new LinkedList<>();
+        Set<String> footers = FluentIterable.from(receiveEvent.commit.getFooterLines())
+            .transform(new Function<FooterLine, String>() {
+                @Override
+                public String apply(FooterLine f) {
+                    return f.getKey().toLowerCase(Locale.US);
+                }
+            })
+            .toSet();
+        for (int i = 0; i < requiredFooters.length; i++) {
+          if (!footers.contains(requiredFooters[i].toLowerCase(Locale.US))) {
+            messages.add(new CommitValidationMessage(
+                "missing required footer: " + requiredFooters[i], true));
+          }
+        }
+        if (!messages.isEmpty()) {
+          throw new CommitValidationException(
+              "missing required footers in commit message", messages);
+        }
+      }
+    } catch (NoSuchProjectException e) {
+      throw new CommitValidationException("failed to check for required footers", e);
+    }
+
+    return Collections.emptyList();
+  }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/Module.java b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/Module.java
index d5328f8..44bf5c6 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/Module.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/Module.java
@@ -15,6 +15,7 @@
 package com.googlesource.gerrit.plugins.uploadvalidator;
 
 import static com.googlesource.gerrit.plugins.uploadvalidator.FileExtensionValidator.KEY_BLOCKED_FILE_EXTENSION;
+import static com.googlesource.gerrit.plugins.uploadvalidator.FooterValidator.KEY_REQUIRED_FOOTER;
 
 import com.google.gerrit.extensions.annotations.Exports;
 import com.google.gerrit.extensions.registration.DynamicSet;
@@ -27,7 +28,6 @@
   protected void configure() {
     DynamicSet.bind(binder(), CommitValidationListener.class)
         .to(FileExtensionValidator.class);
-
     bind(ProjectConfigEntry.class)
         .annotatedWith(Exports.named(KEY_BLOCKED_FILE_EXTENSION))
         .toInstance(
@@ -35,5 +35,15 @@
                 ProjectConfigEntry.Type.ARRAY, null, false,
                 "Forbidden file extensions. Pushes of commits that "
                     + "contain files with these extensions will be rejected."));
+
+    DynamicSet.bind(binder(), CommitValidationListener.class)
+        .to(FooterValidator.class);
+    bind(ProjectConfigEntry.class)
+        .annotatedWith(Exports.named(KEY_REQUIRED_FOOTER))
+        .toInstance(
+            new ProjectConfigEntry("Required Footers", null,
+                ProjectConfigEntry.Type.ARRAY, null, false,
+                "Required footers. Pushes of commits that miss any"
+                    + " of the footers will be rejected."));
   }
 }
diff --git a/src/main/resources/Documentation/about.md b/src/main/resources/Documentation/about.md
index 3cd7ee0..e9d1359 100644
--- a/src/main/resources/Documentation/about.md
+++ b/src/main/resources/Documentation/about.md
@@ -1,4 +1,6 @@
 This plugin allows to configure upload validations per project.
 
-Project owners can configure blocked file extensions. Pushes of commits
-that contain files with these extensions are rejected by Gerrit.
+Project owners can configure blocked file extensions and required
+footers. Pushes of commits that contain files with blocked extensions
+or that miss a required footer in the commit message are rejected by
+Gerrit.
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index 6515655..72386b3 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -13,9 +13,15 @@
     blockedFileExtension = zip
     blockedFileExtension = war
     blockedFileExtension = exe
+    requiredFooter = Bug
 ```
 
 plugin.@PLUGIN@.blockedFileExtension
 :	File extension to be blocked.
 
 	Blocked file extensions are *not* inherited by child projects.
+
+plugin.@PLUGIN@.requiredFooter
+:	Footer that is required.
+
+	Required footers are *not* inherited by child projects.