git cl comments: shorten long URLs if possible.

R=ehmaldonado

Before:
  https://chromium-review.googlesource.com/c/2552792/3/cv/internal/gerrit/gerritfake/fake.go#321
After:
  https://crrev.com/c/2552792/3/cv/internal/gerrit/gerritfake/fake.go#321
Change-Id: Ie6044e2743c4359bc30c98d8915edd9119d4a386
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2556837
Auto-Submit: Andrii Shyshkalov <tandrii@google.com>
Reviewed-by: Edward Lesmes <ehmaldonado@chromium.org>
Commit-Queue: Andrii Shyshkalov <tandrii@google.com>
changes/37/2556837/2
Andrii Shyshkalov 5 years ago committed by LUCI CQ
parent 8820ab859b
commit a3762a907c

@ -1785,6 +1785,15 @@ class Changelist(object):
# {author+date: {path: {patchset: {line: url+message}}}} # {author+date: {path: {patchset: {line: url+message}}}}
comments = collections.defaultdict( comments = collections.defaultdict(
lambda: collections.defaultdict(lambda: collections.defaultdict(dict))) lambda: collections.defaultdict(lambda: collections.defaultdict(dict)))
server = self.GetCodereviewServer()
if server in _KNOWN_GERRIT_TO_SHORT_URLS:
# /c/ is automatically added by short URL server.
url_prefix = '%s/%s' % (_KNOWN_GERRIT_TO_SHORT_URLS[server],
self.GetIssue())
else:
url_prefix = '%s/c/%s' % (server, self.GetIssue())
for path, line_comments in file_comments.items(): for path, line_comments in file_comments.items():
for comment in line_comments: for comment in line_comments:
tag = comment.get('tag', '') tag = comment.get('tag', '')
@ -1796,8 +1805,8 @@ class Changelist(object):
else: else:
patchset = 'PS%d' % comment['patch_set'] patchset = 'PS%d' % comment['patch_set']
line = comment.get('line', 0) line = comment.get('line', 0)
url = ('https://%s/c/%s/%s/%s#%s%s' % url = ('%s/%s/%s#%s%s' %
(self.GetGerritHost(), self.GetIssue(), comment['patch_set'], path, (url_prefix, comment['patch_set'], path,
'b' if comment.get('side') == 'PARENT' else '', 'b' if comment.get('side') == 'PARENT' else '',
str(line) if line else '')) str(line) if line else ''))
comments[key][path][patchset][line] = (url, comment['message']) comments[key][path][patchset][line] = (url, comment['message'])

@ -2447,7 +2447,9 @@ class TestGitCl(unittest.TestCase):
'infra%2Finfra~1'), { 'infra%2Finfra~1'), {
'/COMMIT_MSG': [ '/COMMIT_MSG': [
{ {
'author': {'email': u'reviewer@example.com'}, 'author': {
'email': u'reviewer@example.com'
},
'updated': u'2017-03-17 05:19:37.500000000', 'updated': u'2017-03-17 05:19:37.500000000',
'patch_set': 2, 'patch_set': 2,
'side': 'REVISION', 'side': 'REVISION',
@ -2456,7 +2458,9 @@ class TestGitCl(unittest.TestCase):
], ],
'codereview.settings': [ 'codereview.settings': [
{ {
'author': {'email': u'owner@example.com'}, 'author': {
'email': u'owner@example.com'
},
'updated': u'2017-03-16 20:00:41.000000000', 'updated': u'2017-03-16 20:00:41.000000000',
'patch_set': 2, 'patch_set': 2,
'side': 'PARENT', 'side': 'PARENT',
@ -2467,60 +2471,58 @@ class TestGitCl(unittest.TestCase):
}), }),
(('GetChangeRobotComments', 'chromium-review.googlesource.com', (('GetChangeRobotComments', 'chromium-review.googlesource.com',
'infra%2Finfra~1'), {}), 'infra%2Finfra~1'), {}),
] * 2 + [ ] * 2 + [(('write_json', 'output.json', [{
(('write_json', 'output.json', [ u'date':
{ u'2017-03-16 20:00:41.000000',
u'date': u'2017-03-16 20:00:41.000000', u'message': (u'PTAL\n' + u'\n' + u'codereview.settings\n' +
u'message': ( u' Base, Line 42: https://crrev.com/c/1/2/'
u'PTAL\n' + u'codereview.settings#b42\n' +
u'\n' +
u'codereview.settings\n' +
u' Base, Line 42: https://chromium-review.googlesource.com/' +
u'c/1/2/codereview.settings#b42\n' +
u' I removed this because it is bad\n'), u' I removed this because it is bad\n'),
u'autogenerated': False, u'autogenerated':
u'approval': False, False,
u'disapproval': False, u'approval':
u'sender': u'owner@example.com' False,
u'disapproval':
False,
u'sender':
u'owner@example.com'
}, { }, {
u'date': u'2017-03-17 05:19:37.500000', u'date':
u'message': ( u'2017-03-17 05:19:37.500000',
u'Patch Set 2: Code-Review+1\n' + u'message':
u'\n' + (u'Patch Set 2: Code-Review+1\n' + u'\n' + u'/COMMIT_MSG\n' +
u'/COMMIT_MSG\n' + u' PS2, File comment: https://crrev.com/c/1/2//COMMIT_MSG#\n' +
u' PS2, File comment: https://chromium-review.googlesource' +
u'.com/c/1/2//COMMIT_MSG#\n' +
u' Please include a bug link\n'), u' Please include a bug link\n'),
u'autogenerated': False, u'autogenerated':
u'approval': False, False,
u'disapproval': False, u'approval':
u'sender': u'reviewer@example.com' False,
} u'disapproval':
]), '') False,
] u'sender':
u'reviewer@example.com'
}]), '')]
expected_comments_summary = [ expected_comments_summary = [
git_cl._CommentSummary( git_cl._CommentSummary(
message=( message=(u'PTAL\n' + u'\n' + u'codereview.settings\n' +
u'PTAL\n' + u' Base, Line 42: https://crrev.com/c/1/2/' +
u'\n' + u'codereview.settings#b42\n' +
u'codereview.settings\n' +
u' Base, Line 42: https://chromium-review.googlesource.com/' +
u'c/1/2/codereview.settings#b42\n' +
u' I removed this because it is bad\n'), u' I removed this because it is bad\n'),
date=datetime.datetime(2017, 3, 16, 20, 0, 41, 0), date=datetime.datetime(2017, 3, 16, 20, 0, 41, 0),
autogenerated=False, autogenerated=False,
disapproval=False, approval=False, sender=u'owner@example.com'), disapproval=False,
git_cl._CommentSummary( approval=False,
message=( sender=u'owner@example.com'),
u'Patch Set 2: Code-Review+1\n' + git_cl._CommentSummary(message=(
u'\n' + u'Patch Set 2: Code-Review+1\n' + u'\n' + u'/COMMIT_MSG\n' +
u'/COMMIT_MSG\n' + u' PS2, File comment: https://crrev.com/c/1/2//COMMIT_MSG#\n' +
u' PS2, File comment: https://chromium-review.googlesource.com/' +
u'c/1/2//COMMIT_MSG#\n' +
u' Please include a bug link\n'), u' Please include a bug link\n'),
date=datetime.datetime(2017, 3, 17, 5, 19, 37, 500000), date=datetime.datetime(2017, 3, 17, 5, 19, 37,
500000),
autogenerated=False, autogenerated=False,
disapproval=False, approval=False, sender=u'reviewer@example.com'), disapproval=False,
approval=False,
sender=u'reviewer@example.com'),
] ]
cl = git_cl.Changelist( cl = git_cl.Changelist(
issue=1, branchref='refs/heads/foo') issue=1, branchref='refs/heads/foo')
@ -2533,7 +2535,7 @@ class TestGitCl(unittest.TestCase):
# of autogenerated comment), and unlike other types of comments, only robot # of autogenerated comment), and unlike other types of comments, only robot
# comments from the latest patchset are shown. # comments from the latest patchset are shown.
self.mockGit.config['remote.origin.url'] = ( self.mockGit.config['remote.origin.url'] = (
'https://chromium.googlesource.com/infra/infra') 'https://x.googlesource.com/infra/infra')
gerrit_util.GetChangeDetail.return_value = { gerrit_util.GetChangeDetail.return_value = {
'owner': {'email': 'owner@example.com'}, 'owner': {'email': 'owner@example.com'},
'current_revision': 'ba5eba11', 'current_revision': 'ba5eba11',
@ -2597,13 +2599,15 @@ class TestGitCl(unittest.TestCase):
] ]
} }
self.calls = [ self.calls = [
(('GetChangeComments', 'chromium-review.googlesource.com', (('GetChangeComments', 'x-review.googlesource.com', 'infra%2Finfra~1'),
'infra%2Finfra~1'), {}), {}),
(('GetChangeRobotComments', 'chromium-review.googlesource.com', (('GetChangeRobotComments', 'x-review.googlesource.com',
'infra%2Finfra~1'), { 'infra%2Finfra~1'), {
'codereview.settings': [ 'codereview.settings': [
{ {
u'author': {u'email': u'tricium@serviceaccount.com'}, u'author': {
u'email': u'tricium@serviceaccount.com'
},
u'updated': u'2017-03-17 05:30:37.000000000', u'updated': u'2017-03-17 05:30:37.000000000',
u'robot_run_id': u'5565031076855808', u'robot_run_id': u'5565031076855808',
u'robot_id': u'Linter/Category', u'robot_id': u'Linter/Category',
@ -2617,14 +2621,16 @@ class TestGitCl(unittest.TestCase):
}), }),
] ]
expected_comments_summary = [ expected_comments_summary = [
git_cl._CommentSummary(date=datetime.datetime(2017, 3, 17, 5, 30, 37), git_cl._CommentSummary(
message=( date=datetime.datetime(2017, 3, 17, 5, 30, 37),
u'(1 comment)\n\ncodereview.settings\n' message=(u'(1 comment)\n\ncodereview.settings\n'
u' PS2, Line 32: https://chromium-review.googlesource.com/' u' PS2, Line 32: https://x-review.googlesource.com/c/1/2/'
u'c/1/2/codereview.settings#32\n' u'codereview.settings#32\n'
u' Linter warning message text\n'), u' Linter warning message text\n'),
sender=u'tricium@serviceaccount.com', sender=u'tricium@serviceaccount.com',
autogenerated=True, approval=False, disapproval=False) autogenerated=True,
approval=False,
disapproval=False)
] ]
cl = git_cl.Changelist( cl = git_cl.Changelist(
issue=1, branchref='refs/heads/foo') issue=1, branchref='refs/heads/foo')

Loading…
Cancel
Save