hovercards: enable hovercards on parsed commits references.
This commit is contained in:
parent
51f357d869
commit
3027943bdf
5 changed files with 118 additions and 55 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -1577,21 +1577,21 @@ def urlify_text(text_, safe=True):
|
|||
def url_func(match_obj):
|
||||
url_full = match_obj.groups()[0]
|
||||
return '<a href="%(url)s">%(url)s</a>' % ({'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<a class="%(cls)s" href="%(url)s">'
|
||||
'%(pref)s<a class="tooltip-hovercard %(cls)s" href="%(url)s" data-hovercard-alt="%(hovercard_alt)s" data-hovercard-url="%(hovercard_url)s">'
|
||||
'%(commit_id)s</a>%(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('>', '>')
|
||||
|
|
|
|||
|
|
@ -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 = "<p class='error-message'>Error while fetching hovercard.\nError code {0} ({1}).</p>".format(data.status,data.statusText);
|
||||
|
||||
if (parseInt(data.status) === 404) {
|
||||
var msg = "<p>{0}</p>".format(altHovercard || "No Data exists for this hovercard");
|
||||
} else {
|
||||
var msg = "<p class='error-message'>Error while fetching hovercard.\nError code {0} ({1}).</p>".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 = '<div style="white-space: pre-wrap">{0}</div>'.format($origin.data('hovercardAlt'))
|
||||
var data = '<div style="white-space: pre-wrap">{0}</div>'.format(altHovercard)
|
||||
}
|
||||
var loaded = true;
|
||||
instance.content(data);
|
||||
|
|
|
|||
|
|
@ -178,11 +178,13 @@
|
|||
<div class="fields">
|
||||
<div class="field">
|
||||
<div class='textarea-full'>
|
||||
<textarea id="test_pattern_data" >
|
||||
<textarea id="test_pattern_data" rows="10">
|
||||
This is an example text for testing issue tracker patterns.
|
||||
This commit fixes ticket #451.
|
||||
This is an example text for testing issue tracker patterns, add a pattern here and
|
||||
hit preview to see the link.
|
||||
Open a pull request !101 to contribute !
|
||||
Added tag v1.3.0 for commit 0f3b629be725
|
||||
|
||||
Add a test pattern here and hit preview to see the link.
|
||||
</textarea>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -454,7 +454,10 @@ def test_clone_url_generator(tmpl, repo_name, overrides, prefix, expected):
|
|||
assert clone_url == expected
|
||||
|
||||
|
||||
def _quick_url(text, tmpl="""<a class="revision-link" href="%s">%s</a>""", url_=None):
|
||||
idx = 0
|
||||
|
||||
|
||||
def _quick_url(text, tmpl="""<a class="tooltip-hovercard revision-link" href="%s" data-hovercard-alt="Commit: %s" data-hovercard-url="/some-url">%s</a>""", url_=None, commits=''):
|
||||
"""
|
||||
Changes `some text url[foo]` => `some text <a href="/">foo</a>
|
||||
|
||||
|
|
@ -462,47 +465,86 @@ def _quick_url(text, tmpl="""<a class="revision-link" href="%s">%s</a>""", 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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue