# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Top-level presubmit script for catapult.

See https://www.chromium.org/developers/how-tos/depottools/presubmit-scripts
for more details about the presubmit API built into depot_tools.
"""
import re
import sys

_EXCLUDED_PATHS = (
    r'(.*[\\/])?\.git[\\/].*',
    r'.+\.png$',
    r'.+\.svg$',
    r'.+\.skp$',
    r'.+\.gypi$',
    r'.+\.gyp$',
    r'.+\.gn$',
    r'.*\.gitignore$',
    r'.*codereview.settings$',
    r'.*AUTHOR$',
    r'^CONTRIBUTORS\.md$',
    r'.*LICENSE$',
    r'.*OWNERS$',
    r'.*README\.md$',
    r'^dashboard[\\/]dashboard[\\/]templates[\\/].*',
    r'^experimental[\\/]heatmap[\\/].*',
    r'^perf_insights[\\/]test_data[\\/].*',
    r'^perf_insights[\\/]third_party[\\/].*',
    r'^third_party[\\/].*',
    r'^tracing[\\/]\.allow-devtools-save$',
    r'^tracing[\\/]bower\.json$',
    r'^tracing[\\/]\.bowerrc$',
    r'^tracing[\\/]tracing_examples[\\/]string_convert\.js$',
    r'^tracing[\\/]test_data[\\/].*',
    r'^tracing[\\/]third_party[\\/].*',
    r'^telemetry[\\/]support[\\/]html_output[\\/]results-template.html',
)


def GetPreferredTryMasters(project, change):
  return {
      'tryserver.client.catapult': {
          'Catapult Linux Tryserver': {'defaulttests'},
          'Catapult Mac Tryserver': {'defaulttests'},
          'Catapult Windows Tryserver': {'defaulttests'},
      }
  }


def CheckChangeLogBug(input_api, output_api):
  if input_api.change.BUG is None or re.match(
      r'catapult\:\#\d+(,\s*catapult\:\#\d+)*$', input_api.change.BUG):
    return []
  return [output_api.PresubmitError(
      ('Invalid bug "%s". BUG= should either not be present or start with '
       '"catapult:#"" for a github issue.' % input_api.change.BUG))]


def CheckChange(input_api, output_api):
  results = []
  try:
    sys.path += [input_api.PresubmitLocalPath()]
    from catapult_build import js_checks
    from catapult_build import html_checks
    results += input_api.canned_checks.PanProjectChecks(
        input_api, output_api, excluded_paths=_EXCLUDED_PATHS)
    results += CheckChangeLogBug(input_api, output_api)
    results += js_checks.RunChecks(
        input_api, output_api, excluded_paths=_EXCLUDED_PATHS)
    results += html_checks.RunChecks(
        input_api, output_api, excluded_paths=_EXCLUDED_PATHS)
  finally:
    sys.path.remove(input_api.PresubmitLocalPath())
  return results


def CheckChangeOnUpload(input_api, output_api):
  return CheckChange(input_api, output_api)


def CheckChangeOnCommit(input_api, output_api):
  return CheckChange(input_api, output_api)

@@ -1596,16 +1596,29 @@
       r'
+((chromium%5C:%7C
 catapult
@@ -1625,16 +1625,30 @@
 %5C:%5C#
+)
 %5Cd+
+)
 (,%5Cs*
+(chromium%5C:%7C
 cata
@@ -1659,16 +1659,17 @@
 %5C:%5C#
+)
 %5Cd+)*$',
  inp
@@ -1664,16 +1664,22 @@
 %5Cd+)*$',
+%0A     
  input_a
@@ -1775,55 +1775,59 @@
 s%22. 
-BUG= should either not be present or start with
+Chromium issues should be prefixed with %22chromium:%22
  '%0A 
@@ -1837,44 +1837,75 @@
    '
-%22c
+and C
 atapult
-:#%22%22 for a github issue.' %25
+ issues should be prefixed with %22catapult:#%22.' %25%0A      
  inp

Refactor BUG= presubmit check