From 3ec425522e0ce37e885680cca941414bcb99ab6c Mon Sep 17 00:00:00 2001 From: Ryan Lerch Date: Dec 04 2018 08:20:30 +0000 Subject: [PATCH 1/2] Make links act like links in the commit message Previously, links in the commit messages on the commit detail page were just plain text. This makes them 's that are clickable. Fixes #3242 --- diff --git a/pagure/templates/commit.html b/pagure/templates/commit.html index fed0887..53c0e60 100644 --- a/pagure/templates/commit.html +++ b/pagure/templates/commit.html @@ -212,7 +212,7 @@ {% if splitted_message|length > 1 %}
             {% for message in splitted_message %}
-    {{ message }}
+    {{ message | linkify | safe}}
             {% endfor %}
         
{% endif %} diff --git a/pagure/ui/filters.py b/pagure/ui/filters.py index cd388a6..e860f55 100644 --- a/pagure/ui/filters.py +++ b/pagure/ui/filters.py @@ -20,6 +20,7 @@ import textwrap import arrow import flask import six +import bleach from six.moves.urllib.parse import urlparse, parse_qsl from jinja2 import escape @@ -74,6 +75,17 @@ def format_ts(string): return arr.strftime("%Y-%m-%d %H:%M:%S %Z") +@UI_NS.app_template_filter("linkify") +def linkify_text(text): + """ escape all html tags with bleach, then use bleach to linkify + """ + if text: + cleaned = bleach.clean(text, tags=[], attributes=[]) + return bleach.linkify(cleaned) + else: + return "" + + @UI_NS.app_template_filter("format_loc") def format_loc( loc, From 722f0402b1d7213f9fce3c2592117012ac1370d7 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Dec 04 2018 08:48:44 +0000 Subject: [PATCH 2/2] Add tests checking pagure's behavior when the commit message has a link Signed-off-by: Pierre-Yves Chibon --- diff --git a/tests/__init__.py b/tests/__init__.py index 007a756..101530e 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -926,7 +926,8 @@ def add_commit_git_repo(folder, ncommits=10, filename='sources', def add_content_to_git( - folder, branch='master', filename='sources', content='foo'): + folder, branch='master', filename='sources', content='foo', + message=None): """ Create some more commits for the specified git repo. """ repo, newfolder, branch_ref_obj = _clone_and_top_commits( folder, branch, branch_ref=True) @@ -956,11 +957,12 @@ def add_content_to_git( committer = pygit2.Signature( 'Cecil Committer', 'cecil@committers.tld') branch_ref = "refs/heads/%s" % branch + message = message or 'Add content to file %s' % (filename) repo.create_commit( branch_ref, # the name of the reference to update author, committer, - 'Add content to file %s' % (filename), + message, # binary string representing the tree object ID tree, # list of binary strings representing parents of the new commit diff --git a/tests/test_pagure_flask_ui_repo.py b/tests/test_pagure_flask_ui_repo.py index 5d5e3e8..7533572 100644 --- a/tests/test_pagure_flask_ui_repo.py +++ b/tests/test_pagure_flask_ui_repo.py @@ -2878,6 +2878,83 @@ class PagureFlaskRepotests(tests.Modeltests): 'text-muted fa-fw" data-glyph="spreadsheet"> Commits' '\n
', output_text) + def test_view_commit_with_full_link(self): + """ Test the view_commit endpoint when the commit message includes + an url. """ + + tests.create_projects(self.session) + tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True) + + folder = os.path.join(self.path, 'repos', 'test.git') + + # Add a README to the git repo - First commit + tests.add_readme_git_repo(folder) + tests.create_projects_git(folder, bare=True) + # Add a commit with an url in the commit message + tests.add_content_to_git( + folder, branch='master', filename='sources', content='foo', + message='Test commit message\n\n' + 'Fixes http://example.com/pagure/issue/2' + ) + + # Add a README to the git repo - First commit + repo = pygit2.Repository(os.path.join(self.path, 'repos', 'test.git')) + commit = repo.revparse_single('HEAD') + + # View first commit + output = self.app.get('/test/c/%s' % commit.oid.hex) + self.assertEqual(output.status_code, 200) + output_text = output.get_data(as_text=True) + self.assertIn( + '#commit-overview-collapse', + output_text) + self.assertIn( + '
\n    '
+            'Test commit message\n    \n    '
+            'Fixes http://example.com/pagure/issue/2\n        '
+            '
', output_text) + self.assertIn( + '
file added
', output_text) + + def test_view_commit_with_short_link(self): + """ Test the view_commit endpoint when the commit message includes + an url. """ + + tests.create_projects(self.session) + tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True) + + folder = os.path.join(self.path, 'repos', 'test.git') + + # Add a README to the git repo - First commit + tests.add_readme_git_repo(folder) + tests.create_projects_git(folder, bare=True) + # Add a commit with an url in the commit message + tests.add_content_to_git( + folder, branch='master', filename='sources', content='foo', + message='Test commit message\n\nFixes #2' + ) + + # Add a README to the git repo - First commit + repo = pygit2.Repository(os.path.join(self.path, 'repos', 'test.git')) + commit = repo.revparse_single('HEAD') + + # View first commit + output = self.app.get('/test/c/%s' % commit.oid.hex) + self.assertEqual(output.status_code, 200) + output_text = output.get_data(as_text=True) + self.assertIn( + '#commit-overview-collapse', + output_text) + self.assertIn( + '
\n    '
+            'Test commit message\n    \n    '
+            'Fixes #2\n        
', output_text) + self.assertIn( + '
file added
', output_text) + def test_view_commit_patch(self): """ Test the view_commit_patch endpoint. """