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,10 +1805,10 @@ 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'])
summaries = [] summaries = []

@ -2443,84 +2443,86 @@ class TestGitCl(unittest.TestCase):
] ]
} }
self.calls = [ self.calls = [
(('GetChangeComments', 'chromium-review.googlesource.com', (('GetChangeComments', 'chromium-review.googlesource.com',
'infra%2Finfra~1'), { 'infra%2Finfra~1'), {
'/COMMIT_MSG': [ '/COMMIT_MSG': [
{ {
'author': {'email': u'reviewer@example.com'}, 'author': {
'updated': u'2017-03-17 05:19:37.500000000', 'email': u'reviewer@example.com'
'patch_set': 2, },
'side': 'REVISION', 'updated': u'2017-03-17 05:19:37.500000000',
'message': 'Please include a bug link', 'patch_set': 2,
}, 'side': 'REVISION',
], 'message': 'Please include a bug link',
'codereview.settings': [ },
{ ],
'author': {'email': u'owner@example.com'}, 'codereview.settings': [
'updated': u'2017-03-16 20:00:41.000000000', {
'patch_set': 2, 'author': {
'side': 'PARENT', 'email': u'owner@example.com'
'line': 42, },
'message': 'I removed this because it is bad', 'updated': u'2017-03-16 20:00:41.000000000',
}, 'patch_set': 2,
] 'side': 'PARENT',
}), 'line': 42,
(('GetChangeRobotComments', 'chromium-review.googlesource.com', 'message': 'I removed this because it is bad',
'infra%2Finfra~1'), {}), },
] * 2 + [ ]
(('write_json', 'output.json', [ }),
{ (('GetChangeRobotComments', 'chromium-review.googlesource.com',
u'date': u'2017-03-16 20:00:41.000000', 'infra%2Finfra~1'), {}),
u'message': ( ] * 2 + [(('write_json', 'output.json', [{
u'PTAL\n' + u'date':
u'\n' + u'2017-03-16 20:00:41.000000',
u'codereview.settings\n' + u'message': (u'PTAL\n' + u'\n' + u'codereview.settings\n' +
u' Base, Line 42: https://chromium-review.googlesource.com/' + u' Base, Line 42: https://crrev.com/c/1/2/'
u'c/1/2/codereview.settings#b42\n' + u'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':
u'date': u'2017-03-17 05:19:37.500000', False,
u'message': ( u'sender':
u'Patch Set 2: Code-Review+1\n' + u'owner@example.com'
u'\n' + }, {
u'/COMMIT_MSG\n' + u'date':
u' PS2, File comment: https://chromium-review.googlesource' + u'2017-03-17 05:19:37.500000',
u'.com/c/1/2//COMMIT_MSG#\n' + u'message':
u' Please include a bug link\n'), (u'Patch Set 2: Code-Review+1\n' + u'\n' + u'/COMMIT_MSG\n' +
u'autogenerated': False, u' PS2, File comment: https://crrev.com/c/1/2//COMMIT_MSG#\n' +
u'approval': False, u' Please include a bug link\n'),
u'disapproval': False, u'autogenerated':
u'sender': u'reviewer@example.com' False,
} u'approval':
]), '') 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' I removed this because it is bad\n'),
u' Base, Line 42: https://chromium-review.googlesource.com/' + date=datetime.datetime(2017, 3, 16, 20, 0, 41, 0),
u'c/1/2/codereview.settings#b42\n' + autogenerated=False,
u' I removed this because it is bad\n'), disapproval=False,
date=datetime.datetime(2017, 3, 16, 20, 0, 41, 0), approval=False,
autogenerated=False, sender=u'owner@example.com'),
disapproval=False, approval=False, sender=u'owner@example.com'), git_cl._CommentSummary(message=(
git_cl._CommentSummary( u'Patch Set 2: Code-Review+1\n' + u'\n' + u'/COMMIT_MSG\n' +
message=( u' PS2, File comment: https://crrev.com/c/1/2//COMMIT_MSG#\n' +
u'Patch Set 2: Code-Review+1\n' +
u'\n' +
u'/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,
autogenerated=False, 500000),
disapproval=False, approval=False, sender=u'reviewer@example.com'), autogenerated=False,
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,34 +2599,38 @@ 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'updated': u'2017-03-17 05:30:37.000000000', u'email': u'tricium@serviceaccount.com'
u'robot_run_id': u'5565031076855808', },
u'robot_id': u'Linter/Category', u'updated': u'2017-03-17 05:30:37.000000000',
u'tag': u'autogenerated:tricium', u'robot_run_id': u'5565031076855808',
u'patch_set': 2, u'robot_id': u'Linter/Category',
u'side': u'REVISION', u'tag': u'autogenerated:tricium',
u'message': u'Linter warning message text', u'patch_set': 2,
u'line': 32, u'side': u'REVISION',
}, u'message': u'Linter warning message text',
], u'line': 32,
}), },
],
}),
] ]
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