Fix behavior of automatically adding 'Cq-Include-Trybots' in presubmit

NoTry: true
Bug: skia:7041
Change-Id: I4660db5e9526b29e2135bf4d5f0d134f1f9a5dce
Reviewed-on: https://skia-review.googlesource.com/45880
Commit-Queue: Ravi Mistry <rmistry@google.com>
Reviewed-by: Ben Wagner <benjaminwagner@google.com>
diff --git a/PRESUBMIT.py b/PRESUBMIT.py
index 3eb71a5..4bea709 100644
--- a/PRESUBMIT.py
+++ b/PRESUBMIT.py
@@ -530,9 +530,9 @@
             output_api.PresubmitNotifyResult(
                 'Branch changes do not run the presubmit checks.'))
 
-    # Automatically set CQ_INCLUDE_TRYBOTS if any of the changed files here
+    # Automatically set Cq-Include-Trybots if any of the changed files here
     # begin with the paths of interest.
-    cq_master_to_trybots = collections.defaultdict(set)
+    bots_to_include = []
     for affected_file in change.AffectedFiles():
       affected_file_path = affected_file.LocalPath()
       for path_prefix, extra_bots in PATH_PREFIX_TO_EXTRA_TRYBOTS.iteritems():
@@ -541,10 +541,10 @@
               output_api.PresubmitNotifyResult(
                   'Your CL modifies the path %s.\nAutomatically adding %s to '
                   'the CL description.' % (affected_file_path, extra_bots)))
-          _MergeCQExtraTrybotsMaps(
-              cq_master_to_trybots, _GetCQExtraTrybotsMap(extra_bots))
-    if cq_master_to_trybots:
-      _AddCQExtraTrybotsToDesc(cq_master_to_trybots, new_description_lines)
+          bots_to_include.append(extra_bots)
+    if bots_to_include:
+      output_api.EnsureCQIncludeTrybotsAreAdded(
+          cl, bots_to_include, new_description_lines)
 
     # If the description has changed update it.
     if new_description_lines != original_description_lines:
@@ -555,56 +555,6 @@
     return results
 
 
-def _AddCQExtraTrybotsToDesc(cq_master_to_trybots, description_lines):
-  """Adds the specified master and trybots to the CQ_INCLUDE_TRYBOTS keyword.
-
-  If the keyword already exists in the description then it appends to it only
-  if the specified values do not already exist.
-  If the keyword does not exist then it creates a new section in the
-  description.
-  """
-  found = None
-  foundIdx = -1
-  for idx, line in enumerate(description_lines):
-    if line.startswith('CQ_INCLUDE_TRYBOTS'):
-      found = line
-      foundIdx = idx
-
-  if found:
-    original_trybots_map = _GetCQExtraTrybotsMap(found)
-    _MergeCQExtraTrybotsMaps(cq_master_to_trybots, original_trybots_map)
-    new_line = _GetCQExtraTrybotsStr(cq_master_to_trybots)
-    if new_line != found:
-      description_lines[foundIdx] = new_line
-  else:
-    description_lines.append(_GetCQExtraTrybotsStr(cq_master_to_trybots))
-
-
-def _MergeCQExtraTrybotsMaps(dest_map, map_to_be_consumed):
-  """Merges two maps of masters to trybots into one."""
-  for master, trybots in map_to_be_consumed.iteritems():
-    dest_map[master].update(trybots)
-  return dest_map
-
-
-def _GetCQExtraTrybotsMap(cq_extra_trybots_str):
-  """Parses CQ_INCLUDE_TRYBOTS str and returns a map of masters to trybots."""
-  cq_master_to_trybots = collections.defaultdict(set)
-  for section in cq_extra_trybots_str.split(';'):
-    if section:
-      master, bots = section.split(':')
-      cq_master_to_trybots[master].update(bots.split(','))
-  return cq_master_to_trybots
-
-
-def _GetCQExtraTrybotsStr(cq_master_to_trybots):
-  """Constructs the CQ_INCLUDE_TRYBOTS str from a map of masters to trybots."""
-  sections = []
-  for master, trybots in cq_master_to_trybots.iteritems():
-    sections.append('%s:%s' % (master, ','.join(trybots)))
-  return 'CQ_INCLUDE_TRYBOTS=%s' % ';'.join(sections)
-
-
 def CheckChangeOnCommit(input_api, output_api):
   """Presubmit checks for the change on commit.