gerrit: provide option to suppress emails on some operations

When cherry picking large stacks of patches into branches, gerrit
generates emails on every operation along the way: ready, verify,
review, This results in tons of noise of which the recipients usually
do not care about.

This patch adds a command line option which allows to suppress sending
emails on ready, review, trybotready, and verify gerrit operations.

BUG=none
TEST=using the new command line option modified the 'verified' flag of
     an existing gerit patch. Observed that emails are not generated
     when --ne s passed in as a command line argument, and are
     generated in case --ne is not present.

Change-Id: I15a3ad15e4acdec294a0606a999ca7f7b3bcf5be
Signed-off-by: Vadim Bendebury <vbendeb@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1464365
Reviewed-by: Mike Frysinger <vapier@chromium.org>
(cherry picked from commit 2e3f82da9e4dc8c0feca4c0221dc536a882d5ed8)
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/1500216
diff --git a/lib/gerrit.py b/lib/gerrit.py
index 66e3f39..e82a7b8 100644
--- a/lib/gerrit.py
+++ b/lib/gerrit.py
@@ -329,7 +329,8 @@
 
     return change
 
-  def SetReview(self, change, msg=None, labels=None, dryrun=False):
+  def SetReview(self, change, msg=None, labels=None,
+                dryrun=False, notify='ALL'):
     """Update the review labels on a gerrit change.
 
     Args:
@@ -337,6 +338,7 @@
       msg: A text comment to post to the review.
       labels: A dict of label/value to set on the review.
       dryrun: If True, don't actually update the review.
+      notify: A string, parameter controlling gerrit's email generation.
     """
     if not msg and not labels:
       return
@@ -350,7 +352,7 @@
                        key, val, change)
       return
     gob_util.SetReview(self.host, self._to_changenum(change),
-                       msg=msg, labels=labels, notify='ALL')
+                       msg=msg, labels=labels, notify=notify)
 
   def SetTopic(self, change, topic, dryrun=False):
     """Update the topic on a gerrit change.
diff --git a/scripts/gerrit.py b/scripts/gerrit.py
index 6147ae4..c3d95fd 100644
--- a/scripts/gerrit.py
+++ b/scripts/gerrit.py
@@ -317,7 +317,8 @@
   num = args[-1]
   for arg in args[:-1]:
     helper, cl = GetGerrit(opts, arg)
-    helper.SetReview(cl, labels={'Code-Review': num}, dryrun=opts.dryrun)
+    helper.SetReview(cl, labels={'Code-Review': num},
+                     dryrun=opts.dryrun, notify=opts.notify)
 UserActReview.arg_min = 2
 
 
@@ -326,7 +327,8 @@
   num = args[-1]
   for arg in args[:-1]:
     helper, cl = GetGerrit(opts, arg)
-    helper.SetReview(cl, labels={'Verified': num}, dryrun=opts.dryrun)
+    helper.SetReview(cl, labels={'Verified': num},
+                     dryrun=opts.dryrun, notify=opts.notify)
 UserActVerify.arg_min = 2
 
 
@@ -335,7 +337,8 @@
   num = args[-1]
   for arg in args[:-1]:
     helper, cl = GetGerrit(opts, arg)
-    helper.SetReview(cl, labels={'Commit-Queue': num}, dryrun=opts.dryrun)
+    helper.SetReview(cl, labels={'Commit-Queue': num},
+                     dryrun=opts.dryrun, notify=opts.notify)
 UserActReady.arg_min = 2
 
 
@@ -344,7 +347,8 @@
   num = args[-1]
   for arg in args[:-1]:
     helper, cl = GetGerrit(opts, arg)
-    helper.SetReview(cl, labels={'Trybot-Ready': num}, dryrun=opts.dryrun)
+    helper.SetReview(cl, labels={'Trybot-Ready': num},
+                     dryrun=opts.dryrun, notify=opts.notify)
 UserActTrybotready.arg_min = 2
 
 
@@ -474,6 +478,10 @@
   parser.add_argument('-n', '--dry-run', default=False, action='store_true',
                       dest='dryrun',
                       help='Show what would be done, but do not make changes')
+  parser.add_argument('--ne', '--no-emails', default=True, action='store_false',
+                      dest='send_email',
+                      help='Do not send email for some operations '
+                           '(e.g. ready/review/trybotready/verify)')
   parser.add_argument('-v', '--verbose', default=False, action='store_true',
                       help='Be more verbose in output')
   parser.add_argument('-b', '--branch',
@@ -489,6 +497,9 @@
 
   # A cache of gerrit helpers we'll load on demand.
   opts.gerrit = {}
+
+  # Convert user friendly command line option into a gerrit parameter.
+  opts.notify = 'ALL' if opts.send_email else 'NONE'
   opts.Freeze()
 
   # pylint: disable=W0603