From 3027943bdfba1156a7f45cb523f0bfa2f60cd908 Mon Sep 17 00:00:00 2001 From: Daniel Dourvaris Date: Thu, 7 Nov 2019 14:10:03 +0100 Subject: [PATCH] hovercards: enable hovercards on parsed commits references. --- rhodecode/lib/base.py | 14 ++- rhodecode/lib/helpers.py | 29 ++--- rhodecode/public/js/src/rhodecode.js | 14 ++- .../base/issue_tracker_settings.mako | 8 +- rhodecode/tests/lib/test_libs.py | 108 ++++++++++++------ 5 files changed, 118 insertions(+), 55 deletions(-) diff --git a/rhodecode/lib/base.py b/rhodecode/lib/base.py index 7a261df8..4300f6aa 100644 --- a/rhodecode/lib/base.py +++ b/rhodecode/lib/base.py @@ -539,12 +539,24 @@ def add_events_routes(config): pattern='/{repo_name}/pull-request/{pull_request_id}') config.add_route(name='pull_requests_global', pattern='/pull-request/{pull_request_id}') + config.add_route(name='repo_commit', pattern='/{repo_name}/changeset/{commit_id}') - config.add_route(name='repo_files', pattern='/{repo_name}/files/{commit_id}/{f_path}') + config.add_route(name='hovercard_user', + pattern='/_hovercard/user/{user_id}') + + config.add_route(name='hovercard_user_group', + pattern='/_hovercard/user_group/{user_group_id}') + + config.add_route(name='hovercard_pull_request', + pattern='/_hovercard/pull_request/{pull_request_id}') + + config.add_route(name='hovercard_repo_commit', + pattern='/_hovercard/commit/{repo_name}/{commit_id}') + def bootstrap_config(request): import pyramid.testing diff --git a/rhodecode/lib/helpers.py b/rhodecode/lib/helpers.py index 0493500c..a667c14b 100644 --- a/rhodecode/lib/helpers.py +++ b/rhodecode/lib/helpers.py @@ -1577,21 +1577,21 @@ def urlify_text(text_, safe=True): def url_func(match_obj): url_full = match_obj.groups()[0] return '%(url)s' % ({'url': url_full}) - _newtext = url_pat.sub(url_func, text_) + _new_text = url_pat.sub(url_func, text_) if safe: - return literal(_newtext) - return _newtext + return literal(_new_text) + return _new_text -def urlify_commits(text_, repository): +def urlify_commits(text_, repo_name): """ Extract commit ids from text and make link from them :param text_: - :param repository: repo name to build the URL with + :param repo_name: repo name to build the URL with """ - URL_PAT = re.compile(r'(^|\s)([0-9a-fA-F]{12,40})($|\s)') + url_pat = re.compile(r'(^|\s)([0-9a-fA-F]{12,40})($|\s)') def url_func(match_obj): commit_id = match_obj.groups()[1] @@ -1599,20 +1599,24 @@ def urlify_commits(text_, repository): suf = match_obj.groups()[2] tmpl = ( - '%(pref)s' + '%(pref)s' '%(commit_id)s%(suf)s' ) return tmpl % { 'pref': pref, 'cls': 'revision-link', - 'url': route_url('repo_commit', repo_name=repository, commit_id=commit_id), + 'url': route_url( + 'repo_commit', repo_name=repo_name, commit_id=commit_id), 'commit_id': commit_id, - 'suf': suf + 'suf': suf, + 'hovercard_alt': 'Commit: {}'.format(commit_id), + 'hovercard_url': route_url( + 'hovercard_repo_commit', repo_name=repo_name, commit_id=commit_id) } - newtext = URL_PAT.sub(url_func, text_) + new_text = url_pat.sub(url_func, text_) - return newtext + return new_text def _process_url_func(match_obj, repo_name, uid, entry, @@ -1767,9 +1771,6 @@ def urlify_commit_message(commit_text, repository=None, active_pattern_entries=N """ Parses given text message and makes proper links. issues are linked to given issue-server, and rest is a commit link - - :param commit_text: - :param repository: """ def escaper(_text): return _text.replace('<', '<').replace('>', '>') diff --git a/rhodecode/public/js/src/rhodecode.js b/rhodecode/public/js/src/rhodecode.js index b48ec165..b685c9d3 100644 --- a/rhodecode/public/js/src/rhodecode.js +++ b/rhodecode/public/js/src/rhodecode.js @@ -243,7 +243,7 @@ var tooltipActivate = function () { }); var hovercardCache = {}; - var loadHoverCard = function (url, callback) { + var loadHoverCard = function (url, altHovercard, callback) { var id = url; if (hovercardCache[id] !== undefined) { @@ -257,7 +257,12 @@ var tooltipActivate = function () { callback(hovercardCache[id]); return true; }).fail(function (data, textStatus, errorThrown) { - var msg = "

Error while fetching hovercard.\nError code {0} ({1}).

".format(data.status,data.statusText); + + if (parseInt(data.status) === 404) { + var msg = "

{0}

".format(altHovercard || "No Data exists for this hovercard"); + } else { + var msg = "

Error while fetching hovercard.\nError code {0} ({1}).

".format(data.status,data.statusText); + } callback(msg); return false }); @@ -291,16 +296,17 @@ var tooltipActivate = function () { // we set a variable so the data is only loaded once via Ajax, not every time the tooltip opens if ($origin.data('loaded') !== true) { var hovercardUrl = $origin.data('hovercardUrl'); + var altHovercard =$origin.data('hovercardAlt'); if (hovercardUrl !== undefined && hovercardUrl !== "") { - var loaded = loadHoverCard(hovercardUrl, function (data) { + var loaded = loadHoverCard(hovercardUrl, altHovercard, function (data) { instance.content(data); }) } else { if ($origin.data('hovercardAltHtml')) { var data = atob($origin.data('hovercardAltHtml')); } else { - var data = '
{0}
'.format($origin.data('hovercardAlt')) + var data = '
{0}
'.format(altHovercard) } var loaded = true; instance.content(data); diff --git a/rhodecode/templates/base/issue_tracker_settings.mako b/rhodecode/templates/base/issue_tracker_settings.mako index 2b0af701..1dd503ef 100644 --- a/rhodecode/templates/base/issue_tracker_settings.mako +++ b/rhodecode/templates/base/issue_tracker_settings.mako @@ -178,11 +178,13 @@
-
diff --git a/rhodecode/tests/lib/test_libs.py b/rhodecode/tests/lib/test_libs.py index 3aa47ce0..61918687 100644 --- a/rhodecode/tests/lib/test_libs.py +++ b/rhodecode/tests/lib/test_libs.py @@ -454,7 +454,10 @@ def test_clone_url_generator(tmpl, repo_name, overrides, prefix, expected): assert clone_url == expected -def _quick_url(text, tmpl="""%s""", url_=None): +idx = 0 + + +def _quick_url(text, tmpl="""%s""", url_=None, commits=''): """ Changes `some text url[foo]` => `some text foo @@ -462,47 +465,86 @@ def _quick_url(text, tmpl="""%s""", url_= """ import re # quickly change expected url[] into a link - URL_PAT = re.compile(r'(?:url\[)(.+?)(?:\])') + url_pat = re.compile(r'(?:url\[)(.+?)(?:\])') + commits = commits or [] + + global idx + idx = 0 def url_func(match_obj): + global idx _url = match_obj.groups()[0] - return tmpl % (url_ or '/some-url', _url) - return URL_PAT.sub(url_func, text) + if commits: + commit = commits[idx] + idx += 1 + return tmpl % (url_ or '/some-url', _url, commit) + else: + return tmpl % (url_ or '/some-url', _url) + + return url_pat.sub(url_func, text) -@pytest.mark.parametrize("sample, expected", [ - ("", - ""), - ("git-svn-id: https://svn.apache.org/repos/asf/libcloud/trunk@1441655 13f79535-47bb-0310-9956-ffa450edef68", - "git-svn-id: https://svn.apache.org/repos/asf/libcloud/trunk@1441655 13f79535-47bb-0310-9956-ffa450edef68"), - ("from rev 000000000000", - "from rev url[000000000000]"), - ("from rev 000000000000123123 also rev 000000000000", - "from rev url[000000000000123123] also rev url[000000000000]"), - ("this should-000 00", - "this should-000 00"), - ("longtextffffffffff rev 123123123123", - "longtextffffffffff rev url[123123123123]"), - ("rev ffffffffffffffffffffffffffffffffffffffffffffffffff", - "rev ffffffffffffffffffffffffffffffffffffffffffffffffff"), - ("ffffffffffff some text traalaa", - "url[ffffffffffff] some text traalaa"), - ("""Multi line - 123123123123 - some text 123123123123 - sometimes ! - """, - """Multi line - url[123123123123] - some text url[123123123123] - sometimes ! - """) +@pytest.mark.parametrize("sample, expected, commits", [ + ( + "", + "", + [""] + ), + ( + "git-svn-id: https://svn.apache.org/repos/asf/libcloud/trunk@1441655 13f79535-47bb-0310-9956-ffa450edef68", + "git-svn-id: https://svn.apache.org/repos/asf/libcloud/trunk@1441655 13f79535-47bb-0310-9956-ffa450edef68", + [""] + ), + ( + "from rev 000000000000", + "from rev url[000000000000]", + ["000000000000"] + ), + + ( + "from rev 000000000000123123 also rev 000000000000", + "from rev url[000000000000123123] also rev url[000000000000]", + ["000000000000123123", "000000000000"] + ), + ( + "this should-000 00", + "this should-000 00", + [""] + ), + ( + "longtextffffffffff rev 123123123123", + "longtextffffffffff rev url[123123123123]", + ["123123123123"] + ), + ( + "rev ffffffffffffffffffffffffffffffffffffffffffffffffff", + "rev ffffffffffffffffffffffffffffffffffffffffffffffffff", + ["ffffffffffffffffffffffffffffffffffffffffffffffffff"] + ), + ( + "ffffffffffff some text traalaa", + "url[ffffffffffff] some text traalaa", + ["ffffffffffff"] + ), + ( + """Multi line + 123123123123 + some text 000000000000 + sometimes ! + """, + """Multi line + url[123123123123] + some text url[000000000000] + sometimes ! + """, + ["123123123123", "000000000000"] + ) ], ids=no_newline_id_generator) -def test_urlify_commits(sample, expected): +def test_urlify_commits(sample, expected, commits): def fake_url(self, *args, **kwargs): return '/some-url' - expected = _quick_url(expected) + expected = _quick_url(expected, commits=commits) with mock.patch('rhodecode.lib.helpers.route_url', fake_url): from rhodecode.lib.helpers import urlify_commits