From a233a0ca72dda2f054363309fe4ecb585ad4950f Mon Sep 17 00:00:00 2001 From: Alex Iribarren Date: Apr 19 2021 13:09:36 +0000 Subject: Show VCS and DistURL tags as links when appropriate --- diff --git a/tests/test_www/test_util.py b/tests/test_www/test_util.py index 0ead026..0f7c918 100644 --- a/tests/test_www/test_util.py +++ b/tests/test_www/test_util.py @@ -1,6 +1,6 @@ import unittest -from kojiweb.util import formatMode +from kojiweb.util import formatMode, formatLink class TestFormatMode(unittest.TestCase): def test_format_mode(self): @@ -16,3 +16,21 @@ class TestFormatMode(unittest.TestCase): for s, mode in formats: self.assertEqual(formatMode(mode), s) + + def test_format_link(self): + formats = ( + ('test me', 'test me'), + (' test ', 'test'), + ('', '<script>hack</script>'), + ('not://valid', 'not://valid'), + ('https://foo.com', 'https://foo.com'), + ('http://bar.com/', 'http://bar.com/'), + ('HTtP://BaR.CoM/', 'HTtP://BaR.CoM/'), + ('https://baz.com/baz&t=1', 'https://baz.com/baz&t=1'), + ('ssh://git@pagure.io/foo', 'ssh://git@pagure.io/foo'), + ('git://git@pagure.io/foo', 'git://git@pagure.io/foo'), + ('obs://build.opensuse.org/foo', 'obs://build.opensuse.org/foo'), + ) + + for input, output in formats: + self.assertEqual(formatLink(input), output) diff --git a/www/kojiweb/buildinfo.chtml b/www/kojiweb/buildinfo.chtml index 1198e07..c164c07 100644 --- a/www/kojiweb/buildinfo.chtml +++ b/www/kojiweb/buildinfo.chtml @@ -52,12 +52,12 @@ #end if #if $vcs - $util.escapeHTML($vcs) + $util.formatLink($vcs) #end if #if $disturl - DistURL$util.escapeHTML($disturl) + DistURL$util.formatLink($disturl) #end if diff --git a/www/kojiweb/rpminfo.chtml b/www/kojiweb/rpminfo.chtml index fe2c714..f2313f3 100644 --- a/www/kojiweb/rpminfo.chtml +++ b/www/kojiweb/rpminfo.chtml @@ -70,12 +70,12 @@ #if $vcs - $util.escapeHTML($vcs) + $util.formatLink($vcs) #end if #if $disturl - DistURL$util.escapeHTML($disturl) + DistURL$util.formatLink($disturl) #end if #end if diff --git a/www/lib/kojiweb/util.py b/www/lib/kojiweb/util.py index 6471d62..bdb9827 100644 --- a/www/lib/kojiweb/util.py +++ b/www/lib/kojiweb/util.py @@ -523,6 +523,16 @@ def formatThousands(value): return '{:,}'.format(value) +def formatLink(url): + """Turn a string into an HTML link if it looks vaguely like a URL. + If it doesn't, just return it properly escaped.""" + url = escapeHTML(url.strip()) + m = re.match(r'(https?|ssh|git|obs)://.*', url, flags=re.IGNORECASE) + if m: + return '{}'.format(url, url) + + return url + def rowToggle(template): """If the value of template._rowNum is even, return 'row-even'; if it is odd, return 'row-odd'. Increment the value before checking it.