Commit 2d0e03cb authored by Andrii Shyshkalov's avatar Andrii Shyshkalov Committed by Commit Bot

git cl: safe fallback for Gerrit RPCs even if gerrit project is unknown.

R=ehmaldonado

Bug: 876910
Change-Id: I258efa0a95f20b80819bc89cccfb5e0a29e09d5c
Reviewed-on: https://chromium-review.googlesource.com/1188989
Commit-Queue: Andrii Shyshkalov <tandrii@chromium.org>
Reviewed-by: 's avatarEdward Lesmes <ehmaldonado@chromium.org>
parent 7b7eb880
......@@ -2405,10 +2405,12 @@ class _GerritChangelistImpl(_ChangelistCodereviewBase):
self._gerrit_server = 'https://%s' % self._gerrit_host
return self._gerrit_server
def _GetGerritProject(self, remote_url=None):
def _GetGerritProject(self):
"""Returns Gerrit project name based on remote git URL."""
remote_url = self.GetRemoteUrl()
if remote_url is None:
remote_url = self.GetRemoteUrl()
logging.warn('can\'t detect Gerrit project.')
return None
project = urlparse.urlparse(remote_url).path.strip('/')
if project.endswith('.git'):
project = project[:-len('.git')]
......@@ -2426,9 +2428,13 @@ class _GerritChangelistImpl(_ChangelistCodereviewBase):
"""Handy method for gerrit_util.ChangeIdentifier for a given CL.
Not to be confused by value of "Change-Id:" footer.
If Gerrit project can be determined, this will speed up Gerrit HTTP API RPC.
"""
return gerrit_util.ChangeIdentifier(
self._GetGerritProject(), self.GetIssue())
project = self._GetGerritProject()
if project:
return gerrit_util.ChangeIdentifier(project, self.GetIssue())
# Fall back on still unique, but less efficient change number.
return str(self.GetIssue())
@classmethod
def IssueConfigKey(cls):
......
......@@ -3450,7 +3450,7 @@ class TestGitCl(TestCase):
self.assertEqual(cl.GetRemoteUrl(), url)
self.assertEqual(cl.GetRemoteUrl(), url) # Must be cached.
def test_gerrit_project_detection(self):
def test_gerrit_change_identifier_with_project(self):
self.calls = [
((['git', 'symbolic-ref', 'HEAD'],), 'master'),
((['git', 'config', 'branch.master.merge'],), 'master'),
......@@ -3458,8 +3458,18 @@ class TestGitCl(TestCase):
((['git', 'config', 'remote.origin.url'],),
'https://chromium.googlesource.com/a/my/repo.git/'),
]
cl = git_cl.Changelist(codereview='gerrit', issue=1)
self.assertEqual(cl._GetGerritProject(), 'my/repo')
cl = git_cl.Changelist(codereview='gerrit', issue=123456)
self.assertEqual(cl._GerritChangeIdentifier(), 'my%2Frepo~123456')
def test_gerrit_change_identifier_without_project(self):
self.calls = [
((['git', 'symbolic-ref', 'HEAD'],), 'master'),
((['git', 'config', 'branch.master.merge'],), 'master'),
((['git', 'config', 'branch.master.remote'],), 'origin'),
((['git', 'config', 'remote.origin.url'],), CERR1),
]
cl = git_cl.Changelist(codereview='gerrit', issue=123456)
self.assertEqual(cl._GerritChangeIdentifier(), '123456')
if __name__ == '__main__':
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment