Add post upload hook to substitute hashtags for their mapped text
authorrmistry <rmistry@google.com>
Wed, 25 Mar 2015 19:53:35 +0000 (12:53 -0700)
committerCommit bot <commit-bot@chromium.org>
Wed, 25 Mar 2015 19:53:36 +0000 (12:53 -0700)
BUG=skia:3586
NOTRY=true

Review URL: https://codereview.chromium.org/1004733009

HASHTAGS [new file with mode: 0644]
PRESUBMIT.py

diff --git a/HASHTAGS b/HASHTAGS
new file mode 100644 (file)
index 0000000..9eb2003
--- /dev/null
+++ b/HASHTAGS
@@ -0,0 +1,16 @@
+# This file is used by the post upload hook in the PRESUBMIT file to
+# automatically change a CL's description based on the specified hashtags.
+# Please see skia:3586 for more details.
+#
+# The format of this file is:
+# hashtag_name,mapped_text
+#
+# Here are some examples:
+# * "projectxyz, BUG=skia:123" would convert "#projectxyz" into "BUG=skia:123".
+# * "notry, NOTRY=true" would convert "#notry" into "NOTRY=true".
+
+dummyproject,BUG=skia:2139,BUG=skia:2812
+notry,NOTRY=true
+nocommit,COMMIT=false
+try_android_test_n7_d,CQ_INCLUDE_TRYBOTS=client.skia.android:Test-Android-Nexus7-Tegra3-Arm7-Debug
+try_android_test_n7_r,CQ_INCLUDE_TRYBOTS=client.skia.android:Test-Android-Nexus7-Tegra3-Arm7-Release
index 00835f9..8b60774 100644 (file)
@@ -9,6 +9,7 @@ See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
 for more details about the presubmit API built into gcl.
 """
 
+import csv
 import fnmatch
 import os
 import re
@@ -21,6 +22,9 @@ REVERT_CL_SUBJECT_PREFIX = 'Revert '
 
 SKIA_TREE_STATUS_URL = 'http://skia-tree-status.appspot.com'
 
+CQ_KEYWORDS_THAT_NEED_APPENDING = ('CQ_INCLUDE_TRYBOTS', 'CQ_EXTRA_TRYBOTS',
+                                   'CQ_EXCLUDE_TRYBOTS', 'CQ_TRYBOTS')
+
 # Please add the complete email address here (and not just 'xyz@' or 'xyz').
 PUBLIC_API_OWNERS = (
     'reed@chromium.org',
@@ -387,6 +391,33 @@ def PostUploadHook(cl, change, output_api):
                 'Trybots do not yet work for non-master branches. '
                 'Automatically added \'NOTRY=true\' to the CL\'s description'))
 
+    # Read and process the HASHTAGS file.
+    with open('HASHTAGS', 'rb') as hashtags_csv:
+      hashtags_reader = csv.reader(hashtags_csv, delimiter=',')
+      for row in hashtags_reader:
+        if not row or row[0].startswith('#'):
+          # Ignore empty lines and comments
+          continue
+        hashtag = row[0]
+        # Search for the hashtag in the description.
+        if re.search('#%s' % hashtag, new_description, re.M | re.I):
+          for mapped_text in row[1:]:
+            # Special case handling for CQ_KEYWORDS_THAT_NEED_APPENDING.
+            appended_description = _HandleAppendingCQKeywords(
+                hashtag, mapped_text, new_description, results, output_api)
+            if appended_description:
+              new_description = appended_description
+              continue
+
+            # Add the mapped text if it does not already exist in the
+            # CL's description.
+            if not re.search(
+                r'^%s$' % mapped_text, new_description, re.M | re.I):
+              new_description += '\n%s' % mapped_text
+              results.append(
+                  output_api.PresubmitNotifyResult(
+                      'Found \'#%s\', automatically added \'%s\' to the CL\'s '
+                      'description' % (hashtag, mapped_text)))
 
     # If the description has changed update it.
     if new_description != original_description:
@@ -395,6 +426,31 @@ def PostUploadHook(cl, change, output_api):
     return results
 
 
+def _HandleAppendingCQKeywords(hashtag, keyword_and_value, description,
+                               results, output_api):
+  """Handles the CQ keywords that need appending if specified in hashtags."""
+  keyword = keyword_and_value.split('=')[0]
+  if keyword in CQ_KEYWORDS_THAT_NEED_APPENDING:
+    # If the keyword is already in the description then append to it.
+    match = re.search(
+        r'^%s=(.*)$' % keyword, description, re.M | re.I)
+    if match:
+      old_values = match.group(1).split(';')
+      new_value = keyword_and_value.split('=')[1]
+      if new_value in old_values:
+        # Do not need to do anything here.
+        return description
+      # Update the description with the new values.
+      new_description = description.replace(
+          match.group(0), "%s;%s" % (match.group(0), new_value))
+      results.append(
+          output_api.PresubmitNotifyResult(
+          'Found \'#%s\', automatically appended \'%s\' to %s in '
+          'the CL\'s description' % (hashtag, new_value, keyword)))
+      return new_description
+  return None
+
+
 def CheckChangeOnCommit(input_api, output_api):
   """Presubmit checks for the change on commit.