+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.
+ """
+
+ 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$', original_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=.*', original_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 description has changed update it.
+ if new_description != original_description:
+ rietveld_obj.update_description(issue, new_description)
+
+ return results
+
+