Make CheckChangedLUCIConfigs work on non-git workspace

The check relies on cl.GetRemoteBranch() and cl.GetRemoteUrl() that
both rely on git. Provide that info with Gerrit info from input_api
if available instead of always calling git.

Bug: 333744051
Change-Id: I915d6451d808c9bf871804d116ea884294ee7c84
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5479889
Commit-Queue: Gavin Mak <gavinmak@google.com>
Reviewed-by: Josip Sokcevic <sokcevic@chromium.org>
Reviewed-by: Riley Wong <rgw@google.com>
changes/89/5479889/4
Gavin Mak 1 year ago committed by LUCI CQ
parent 90d5723d2f
commit 14cccc4b9a

@ -2171,8 +2171,11 @@ def CheckChangedLUCIConfigs(input_api, output_api):
LUCI_CONFIG_HOST_NAME = 'config.luci.app'
cl = git_cl.Changelist()
if input_api.change.issue and input_api.gerrit:
if input_api.gerrit:
if input_api.change.issue:
remote_branch = input_api.gerrit.GetDestRef(input_api.change.issue)
else:
remote_branch = input_api.gerrit.branch
else:
remote, remote_branch = cl.GetRemoteBranch()
if remote_branch.startswith('refs/remotes/%s/' % remote):
@ -2182,6 +2185,13 @@ def CheckChangedLUCIConfigs(input_api, output_api):
remote_branch = remote_branch.replace('refs/remotes/branch-heads/',
'refs/branch-heads/', 1)
if input_api.gerrit:
host = input_api.gerrit.host
project = input_api.gerrit.project
gerrit_url = f'https://{host}/{project}'
remote_host_url = gerrit_url.replace('-review.googlesource',
'.googlesource')
else:
remote_host_url = cl.GetRemoteUrl()
if not remote_host_url:
return [

@ -1910,7 +1910,8 @@ class ChangeUnittest(PresubmitTestsBase):
class CannedChecksUnittest(PresubmitTestsBase):
"""Tests presubmit_canned_checks.py."""
def MockInputApi(self, change, committing):
def MockInputApi(self, change, committing, gerrit=None):
# pylint: disable=no-self-use
input_api = mock.MagicMock(presubmit.InputApi)
input_api.thread_pool = presubmit.ThreadPool()
@ -1921,7 +1922,7 @@ class CannedChecksUnittest(PresubmitTestsBase):
input_api.os_walk = mock.Mock()
input_api.os_path = os.path
input_api.re = presubmit.re
input_api.gerrit = mock.MagicMock(presubmit.GerritAccessor)
input_api.gerrit = gerrit
input_api.urllib_request = mock.MagicMock(presubmit.urllib_request)
input_api.urllib_error = mock.MagicMock(presubmit.urllib_error)
input_api.unittest = unittest
@ -2362,6 +2363,42 @@ class CannedChecksUnittest(PresubmitTestsBase):
stderr=subprocess.PIPE,
shell=input_api.is_windows)
@mock.patch('git_cl.Changelist')
@mock.patch('auth.Authenticator')
def testCannedCheckChangedLUCIConfigsGerritInfo(self, mockGetAuth, mockCl):
affected_file = mock.MagicMock(presubmit.ProvidedDiffAffectedFile)
affected_file.LocalPath.return_value = 'foo.cfg'
mockGetAuth().get_id_token().token = 123
host = 'host.googlesource.com'
project = 'project/deadbeef'
branch = 'branch'
http_resp = b")]}'\n" + json.dumps({
'configSets':
[{
'name': 'project/deadbeef',
'url': f'https://{host}/{project}/+/{branch}/generated'
# no affected file in generated folder
}]
}).encode("utf-8")
gerrit_mock = mock.MagicMock(presubmit.GerritAccessor)
gerrit_mock.host = host
gerrit_mock.project = project
gerrit_mock.branch = branch
change1 = presubmit.Change('foo', 'foo1', self.fake_root_dir, None, 0,
0, None)
input_api = self.MockInputApi(change1, False, gerrit_mock)
input_api.urllib_request.urlopen().read.return_value = http_resp
input_api.AffectedFiles = lambda **_: (affected_file, )
results = presubmit_canned_checks.CheckChangedLUCIConfigs(
input_api, presubmit.OutputApi)
self.assertEqual(len(results), 0)
def testCannedCheckChangeHasNoTabs(self):
self.ContentTest(presubmit_canned_checks.CheckChangeHasNoTabs,
'blah blah', None, 'blah\tblah', None,
@ -2966,7 +3003,9 @@ the current line as well!
def GetInputApiWithOWNERS(self, owners_content):
input_api = self.GetInputApiWithFiles({'OWNERS': ('M', owners_content)})
input_api.gerrit.IsCodeOwnersEnabledOnRepo = lambda: True
gerrit_mock = mock.MagicMock(presubmit.GerritAccessor)
gerrit_mock.IsCodeOwnersEnabledOnRepo = lambda: True
input_api.gerrit = gerrit_mock
return input_api

Loading…
Cancel
Save