Block keyword in commit messages as well as file

Change-Id: I7b57b8c626f830946dd9f6082e8e445abdc5d1c2
diff --git a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidator.java b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidator.java
index 5c5dc12..bd5420e 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidator.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidator.java
@@ -25,6 +25,7 @@
 import com.google.gerrit.extensions.annotations.PluginName;
 import com.google.gerrit.extensions.api.projects.ProjectConfigEntryType;
 import com.google.gerrit.extensions.registration.DynamicSet;
+import com.google.gerrit.reviewdb.client.Patch;
 import com.google.gerrit.server.config.PluginConfig;
 import com.google.gerrit.server.config.PluginConfigFactory;
 import com.google.gerrit.server.config.ProjectConfigEntry;
@@ -73,8 +74,8 @@
             .annotatedWith(Exports.named(KEY_CHECK_BLOCKED_KEYWORD_PATTERN))
             .toInstance(new ProjectConfigEntry("Blocked Keyword Pattern", null,
                 ProjectConfigEntryType.ARRAY, null, false,
-                "Pushes of commits that contain files with blocked keywords "
-                    + "will be rejected."));
+                "Pushes of commits that contain files or commit messages with "
+                + "blocked keywords will be rejected."));
       }
     };
   }
@@ -142,6 +143,8 @@
       ImmutableCollection<Pattern> blockedKeywordPartterns, PluginConfig cfg)
           throws IOException, ExecutionException {
     List<CommitValidationMessage> messages = new LinkedList<>();
+    checkCommitMessageForBlockedKeywords(blockedKeywordPartterns, messages,
+        c.getFullMessage());
     Map<String, ObjectId> content = CommitUtils.getChangedContent(repo, c);
     for (String path : content.keySet()) {
       ObjectLoader ol = repo.open(content.get(path));
@@ -153,6 +156,17 @@
     return messages;
   }
 
+  private static void checkCommitMessageForBlockedKeywords(
+      ImmutableCollection<Pattern> blockedKeywordPatterns,
+      List<CommitValidationMessage> messages, String commitMessage) {
+    int line = 0;
+    for (String l : commitMessage.split("[\r\n]+")) {
+      line++;
+      checkLineForBlockedKeywords(blockedKeywordPatterns, messages,
+          Patch.COMMIT_MSG, line, l);
+    }
+  }
+
   private static void checkFileForBlockedKeywords(
       ImmutableCollection<Pattern> blockedKeywordPartterns,
       List<CommitValidationMessage> messages, String path, ObjectLoader ol)
@@ -181,7 +195,7 @@
     }
     if (!found.isEmpty()) {
       messages.add(new CommitValidationMessage(MessageFormat.format(
-          "blocked keyword(s) found in file: {0} (Line: {1}) (found: {2})",
+          "blocked keyword(s) found in: {0} (Line: {1}) (found: {2})",
           path, lineNumber, Joiner.on(", ").join(found)), true));
     }
   }
diff --git a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidatorTest.java b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidatorTest.java
index f201592..c2a92e5 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidatorTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidatorTest.java
@@ -20,6 +20,7 @@
 
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
+import com.google.gerrit.reviewdb.client.Patch;
 import com.google.gerrit.server.git.validators.CommitValidationMessage;
 
 import org.eclipse.jgit.api.errors.GitAPIException;
@@ -67,7 +68,7 @@
         + "Testline4";
     files.put(new File(repo.getDirectory().getParent(), "foobar.txt"),
         content.getBytes(StandardCharsets.UTF_8));
-    return TestUtils.makeCommit(repo, "Commit with test files.", files);
+    return TestUtils.makeCommit(repo, "Commit foobar with test files.", files);
   }
 
   @Test
@@ -78,10 +79,12 @@
     List<CommitValidationMessage> m = validator.performValidation(
         repo, c, getPatterns().values(), EMPTY_PLUGIN_CONFIG);
     Set<String> expected = ImmutableSet.of(
-        "ERROR: blocked keyword(s) found in file: foo.txt (Line: 1)"
+        "ERROR: blocked keyword(s) found in: foo.txt (Line: 1)"
             + " (found: myp4ssw0rd, foobar)",
-        "ERROR: blocked keyword(s) found in file: bar.txt (Line: 5)"
-            + " (found: $Id: foo bar$)");
+        "ERROR: blocked keyword(s) found in: bar.txt (Line: 5)"
+            + " (found: $Id: foo bar$)",
+        "ERROR: blocked keyword(s) found in: " + Patch.COMMIT_MSG
+            + " (Line: 1) (found: foobar)");
     assertThat(TestUtils.transformMessages(m))
         .containsExactlyElementsIn(expected);
   }