Cherrypick post upload hook changes in PRESUBMIT.py of m39 branch

CLs uploaded from this branch will now automatically contain 'NOTRY=true' and 'NOTREECHECKS=true'.

These CLs are cherrypicked:
https://codereview.chromium.org/960203002/ ('Automatically add a docs preview link and NOTRY=true when there are only docs changes')
https://codereview.chromium.org/963553002/ ('Automatically add NOTREECHECKS when uploading CLs from non master branches')

BUG=skia:
NOTREECHECKS=true
NOTRY=true

Review URL: https://codereview.chromium.org/1006473003
diff --git a/PRESUBMIT.py b/PRESUBMIT.py
index 61fa2d6..a4d90c8 100644
--- a/PRESUBMIT.py
+++ b/PRESUBMIT.py
@@ -12,6 +12,7 @@
 import fnmatch
 import os
 import re
+import subprocess
 import sys
 import traceback
 
@@ -32,6 +33,8 @@
 
 AUTHORS_FILE_NAME = 'AUTHORS'
 
+DOCS_PREVIEW_URL = 'https://skia.org/?cl='
+
 
 def _CheckChangeHasEol(input_api, output_api, source_file_filter=None):
   """Checks that files end with atleast one \n (LF)."""
@@ -239,6 +242,87 @@
   return results
 
 
+def PostUploadHook(cl, change, output_api):
+  """git cl upload will call this hook after the issue is created/modified.
+
+  This hook does the following:
+  * Adds a link to preview docs changes if there are any docs changes in the CL.
+  * Adds 'NOTRY=true' if the CL contains only docs changes.
+  * Adds 'NOTREECHECKS=true' for non master branch changes since they do not
+    need to be gated on the master branch's tree.
+  * Adds 'NOTRY=true' for non master branch changes since trybots do not yet
+    work on them.
+  """
+
+  results = []
+  atleast_one_docs_change = False
+  all_docs_changes = True
+  for affected_file in change.AffectedFiles():
+    affected_file_path = affected_file.LocalPath()
+    file_path, _ = os.path.splitext(affected_file_path)
+    if 'site' == file_path.split(os.path.sep)[0]:
+      atleast_one_docs_change = True
+    else:
+      all_docs_changes = False
+    if atleast_one_docs_change and not all_docs_changes:
+      break
+
+  issue = cl.issue
+  rietveld_obj = cl.RpcServer()
+  if issue and rietveld_obj:
+    original_description = rietveld_obj.get_description(issue)
+    new_description = original_description
+
+    # If the change includes only doc changes then add NOTRY=true in the
+    # CL's description if it does not exist yet.
+    if all_docs_changes and not re.search(
+        r'^NOTRY=true$', new_description, re.M | re.I):
+      new_description += '\nNOTRY=true'
+      results.append(
+          output_api.PresubmitNotifyResult(
+              'This change has only doc changes. Automatically added '
+              '\'NOTRY=true\' to the CL\'s description'))
+
+    # If there is atleast one docs change then add preview link in the CL's
+    # description if it does not already exist there.
+    if atleast_one_docs_change and not re.search(
+        r'^DOCS_PREVIEW=.*', new_description, re.M | re.I):
+      # Automatically add a link to where the docs can be previewed.
+      new_description += '\nDOCS_PREVIEW= %s%s' % (DOCS_PREVIEW_URL, issue)
+      results.append(
+          output_api.PresubmitNotifyResult(
+              'Automatically added a link to preview the docs changes to the '
+              'CL\'s description'))
+
+    # If the target ref is not master then add NOTREECHECKS=true and NOTRY=true
+    # to the CL's description if it does not already exist there.
+    target_ref = rietveld_obj.get_issue_properties(issue, False).get(
+        'target_ref', '')
+    if target_ref != 'refs/heads/master':
+      if not re.search(
+          r'^NOTREECHECKS=true$', new_description, re.M | re.I):
+        new_description += "\nNOTREECHECKS=true"
+        results.append(
+            output_api.PresubmitNotifyResult(
+                'Branch changes do not need to rely on the master branch\'s '
+                'tree status. Automatically added \'NOTREECHECKS=true\' to the '
+                'CL\'s description'))
+      if not re.search(
+          r'^NOTRY=true$', new_description, re.M | re.I):
+        new_description += "\nNOTRY=true"
+        results.append(
+            output_api.PresubmitNotifyResult(
+                'Trybots do not yet work for non-master branches. '
+                'Automatically added \'NOTRY=true\' to the CL\'s description'))
+
+
+    # If the description has changed update it.
+    if new_description != original_description:
+      rietveld_obj.update_description(issue, new_description)
+
+    return results
+
+
 def CheckChangeOnCommit(input_api, output_api):
   """Presubmit checks for the change on commit.
 
diff --git a/codereview.settings b/codereview.settings
index 0dba340..f8e8c9d 100644
--- a/codereview.settings
+++ b/codereview.settings
@@ -5,3 +5,4 @@
 BUG_PREFIX: skia:
 TRYSERVER_SVN_URL: https://skia-try.googlecode.com/svn
 PROJECT: skia
+RUN_POST_UPLOAD_HOOK: True