Allow callers of PatchSetInserter to suppress emails and hooks

Add methods to allow callers of the PatchSetInserter class to
prevent it from sending email notifications and executing hooks
after creating a new patch set.

Change-Id: I1a3e62ff74011d917dc8b029c941c0e78b62f25e
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/change/PatchSetInserter.java b/gerrit-server/src/main/java/com/google/gerrit/server/change/PatchSetInserter.java
index 0e0b103..f16047a 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/change/PatchSetInserter.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/change/PatchSetInserter.java
@@ -93,6 +93,8 @@
   private boolean copyLabels;
   private SshInfo sshInfo;
   private boolean draft;
+  private boolean runHooks;
+  private boolean sendMail;
 
   @Inject
   public PatchSetInserter(ChangeHooks hooks,
@@ -124,6 +126,8 @@
     this.refControl = refControl;
     this.change = change;
     this.commit = commit;
+    this.runHooks = true;
+    this.sendMail = true;
   }
 
   public PatchSetInserter setPatchSet(PatchSet patchSet) {
@@ -169,6 +173,16 @@
     return this;
   }
 
+  public PatchSetInserter setRunHooks(boolean runHooks) {
+    this.runHooks = runHooks;
+    return this;
+  }
+
+  public PatchSetInserter setSendMail(boolean sendMail) {
+    this.sendMail = sendMail;
+    return this;
+  }
+
   public Change insert() throws InvalidChangeOperationException, OrmException,
       IOException {
     init();
@@ -248,23 +262,27 @@
         db.changeMessages().insert(Collections.singleton(changeMessage));
       }
 
-      try {
-        PatchSetInfo info = patchSetInfoFactory.get(commit, patchSet.getId());
-        ReplacePatchSetSender cm =
-            replacePatchSetFactory.create(updatedChange);
-        cm.setFrom(user.getAccountId());
-        cm.setPatchSet(patchSet, info);
-        cm.setChangeMessage(changeMessage);
-        cm.addReviewers(oldReviewers);
-        cm.addExtraCC(oldCC);
-        cm.send();
-      } catch (Exception err) {
-        log.error("Cannot send email for new patch set on change " + updatedChange.getId(),
-            err);
+      if (sendMail) {
+        try {
+          PatchSetInfo info = patchSetInfoFactory.get(commit, patchSet.getId());
+          ReplacePatchSetSender cm =
+              replacePatchSetFactory.create(updatedChange);
+          cm.setFrom(user.getAccountId());
+          cm.setPatchSet(patchSet, info);
+          cm.setChangeMessage(changeMessage);
+          cm.addReviewers(oldReviewers);
+          cm.addExtraCC(oldCC);
+          cm.send();
+        } catch (Exception err) {
+          log.error("Cannot send email for new patch set on change " + updatedChange.getId(),
+              err);
+        }
       }
 
       indexer.index(updatedChange);
-      hooks.doPatchsetCreatedHook(updatedChange, patchSet, db);
+      if (runHooks) {
+        hooks.doPatchsetCreatedHook(updatedChange, patchSet, db);
+      }
     } finally {
       db.rollback();
     }