diff --git a/presubmit_canned_checks.py b/presubmit_canned_checks.py index 5c4307c59..4dbbcc4bd 100644 --- a/presubmit_canned_checks.py +++ b/presubmit_canned_checks.py @@ -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: - remote_branch = input_api.gerrit.GetDestRef(input_api.change.issue) + 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,7 +2185,14 @@ def CheckChangedLUCIConfigs(input_api, output_api): remote_branch = remote_branch.replace('refs/remotes/branch-heads/', 'refs/branch-heads/', 1) - remote_host_url = cl.GetRemoteUrl() + 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 [ output_api.PresubmitError( diff --git a/tests/presubmit_unittest.py b/tests/presubmit_unittest.py index c461e24ed..8b10c7812 100755 --- a/tests/presubmit_unittest.py +++ b/tests/presubmit_unittest.py @@ -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