From 17a1bf668d2a24c6b8192cd2a758f526e57a28e9 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Oct 05 2017 12:49:10 +0000 Subject: Consolidate the code in our custom markdown processor With this commit we are consolidating all the pieces of code needing to extract the namespace, project name and user name corresponding to the page displayed in a single place simplifying the maintenance of said piece of code and avoiding to fix bugs in multiple places. This simple consolidate fixes automatically linking to a commit by providing its hash in markdown. Fixes https://pagure.io/pagure/issue/2612 Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/pfmarkdown.py b/pagure/pfmarkdown.py index 07a054b..e4cd654 100644 --- a/pagure/pfmarkdown.py +++ b/pagure/pfmarkdown.py @@ -164,26 +164,10 @@ class ImplicitIssuePattern(markdown.inlinepatterns.Pattern): return text try: - root = flask.request.url_root - url = flask.request.url + namespace, repo, user = _get_ns_repo_user() except RuntimeError: return text - user = flask.request.args.get('user') - namespace = flask.request.args.get('namespace') - repo = flask.request.args.get('repo') - - if not user and not repo: - if 'fork/' in url: - user, ext = url.split('fork/')[1].split('/', 1) - else: - ext = url.split(root)[1] - - if ext.count('/') >= 3: - namespace, repo = ext.split('/', 2)[:2] - else: - repo = ext.split('/', 1)[0] - issue = _issue_exists(user, namespace, repo, idx) if issue: return _obj_anchor_tag(user, namespace, repo, issue, text) @@ -208,21 +192,10 @@ class ImplicitPRPattern(markdown.inlinepatterns.Pattern): return text try: - root = flask.request.url_root - url = flask.request.url + namespace, repo, user = _get_ns_repo_user() except RuntimeError: return text - user = flask.request.args.get('user') - namespace = flask.request.args.get('namespace') - repo = flask.request.args.get('repo') - - if not user and not repo: - if 'fork/' in url: - user, repo = url.split('fork/')[1].split('/', 2)[:2] - else: - repo = url.split(root)[1].split('/', 1)[0] - issue = _issue_exists(user, namespace, repo, idx) if issue: return _obj_anchor_tag(user, namespace, repo, issue, text) @@ -242,22 +215,12 @@ class ImplicitCommitPattern(markdown.inlinepatterns.Pattern): githash = markdown.util.AtomicString(m.group(2)) text = '%s' % githash + try: - root = flask.request.url_root - url = flask.request.url + namespace, repo, user = _get_ns_repo_user() except RuntimeError: return text - user = flask.request.args.get('user') - namespace = flask.request.args.get('namespace') - repo = flask.request.args.get('repo') - - if not user and not repo: - if 'fork/' in url: - user, repo = url.split('fork/')[1].split('/', 2)[:2] - else: - repo = url.split(root)[1].split('/', 1)[0] - if pagure.lib.search_projects( pagure.SESSION, username=user, @@ -399,3 +362,31 @@ def _obj_anchor_tag(user, namespace, repo, obj, text): element.set('title', title) element.text = text return element + + +def _get_ns_repo_user(): + """ Return the namespace, repo, user corresponding to the given request + + :return: A tuple of three string corresponding to namespace, repo, user + :rtype: tuple(str, str, str) + """ + + root = flask.request.url_root + url = flask.request.url + + user = flask.request.args.get('user') + namespace = flask.request.args.get('namespace') + repo = flask.request.args.get('repo') + + if not user and not repo: + if 'fork/' in url: + user, ext = url.split('fork/')[1].split('/', 1) + else: + ext = url.split(root)[1] + + if ext.count('/') >= 3: + namespace, repo = ext.split('/', 2)[:2] + else: + repo = ext.split('/', 1)[0] + + return (namespace, repo, user)