From: Michael Tremer Date: Sun, 22 Oct 2017 12:58:19 +0000 (+0100) Subject: Refactor Bugzilla URL generation X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1aae140adf43b5cf5e6e015b8632b67e52e3faf4;p=people%2Fjschlag%2Fpbs.git Refactor Bugzilla URL generation Signed-off-by: Michael Tremer --- diff --git a/src/buildservice/bugtracker.py b/src/buildservice/bugtracker.py index 7dd4f67..f9bc43b 100644 --- a/src/buildservice/bugtracker.py +++ b/src/buildservice/bugtracker.py @@ -106,13 +106,10 @@ class Bugzilla(base.Object): return method(kwargs) - def bug_url(self, bugid): + def bug_url(self, bug_id): url = self.settings.get("bugzilla_url", None) - try: - return url % { "bugid" : bugid } - except: - return "#" + return url % { "bug_id" : bug_id } def enter_url(self, component): args = { diff --git a/src/buildservice/constants.py.in b/src/buildservice/constants.py.in index 5177859..d62a9c9 100644 --- a/src/buildservice/constants.py.in +++ b/src/buildservice/constants.py.in @@ -24,9 +24,6 @@ UPLOADS_DIR = "/var/tmp/pakfire/uploads" BUFFER_SIZE = 1024 * 100 # 100kb -BUGZILLA_PATTERN = r"(bug\s?|#)(\d+)" -CVE_PATTERN = r"(CVE)(\s|\-)(\d{4}\-\d{4})" - FILE_EXTENSIONS_VIEWABLE = ( ".c", ".cc", diff --git a/src/web/ui_modules.py b/src/web/ui_modules.py index 0a7b8ec..d27c4a8 100644 --- a/src/web/ui_modules.py +++ b/src/web/ui_modules.py @@ -16,48 +16,23 @@ from ..constants import * class UIModule(tornado.web.UIModule): @property - def pakfire(self): + def backend(self): return self.handler.application.backend + @property + def pakfire(self): + return self.backend + @property def settings(self): return self.pakfire.settings class TextModule(UIModule): - __cache = {} - - LINK = """%s""" - - @property - def bugzilla_url(self): - return self.settings.get("bugzilla_url", "") - - @property - def bugzilla_pattern(self): - if not self.__cache.has_key("bugzilla_pattern"): - self.__cache["bugzilla_pattern"] = re.compile(BUGZILLA_PATTERN) + BUGZILLA_PATTERN = re.compile(r"(?:bug\s?|#)(\d+)") + CVE_PATTERN = re.compile(r"(?:CVE)[\s\-](\d{4}\-\d{4})") - return self.__cache["bugzilla_pattern"] - - @property - def bugzilla_repl(self): - return self.LINK % (self.bugzilla_url % { "bugid" : r"\2" }, r"\1\2") - - @property - def cve_url(self): - return self.settings.get("cve_url", "") - - @property - def cve_pattern(self): - if not self.__cache.has_key("cve_pattern"): - self.__cache["cve_pattern"] = re.compile(CVE_PATTERN) - - return self.__cache["cve_pattern"] - - @property - def cve_repl(self): - return self.LINK % (self.cve_url % r"\3", r"\1\2\3") + LINK = """%s""" def split_paragraphs(self, s): for group_seperator, line_iteration in itertools.groupby(s.splitlines(True), key=str.isspace): @@ -68,29 +43,36 @@ class TextModule(UIModule): yield paragraph.replace("\n", " ") def render(self, text, pre=False, remove_linebreaks=True): - link = """%s""" - if remove_linebreaks: text = text.replace("\n", " ") # Escape the text and create make urls clickable. text = tornado.escape.xhtml_escape(text) text = tornado.escape.linkify(text, shorten=True, - extra_params='target="_blank"') + extra_params="target=\"_blank\" rel=\"noopener\"") - # Search for bug ids that need to be linked to bugzilla. - if self.bugzilla_url: - text = re.sub(self.bugzilla_pattern, self.bugzilla_repl, text, re.I|re.U) + # Search for bug ids that need to be linked to bugzilla + text = re.sub(self.BUGZILLA_PATTERN, self._bugzilla_repl, text, re.I|re.U) # Search for CVE numbers and create hyperlinks. - if self.cve_url: - text = re.sub(self.cve_pattern, self.cve_repl, text, re.I|re.U) + text = re.sub(self.CVE_PATTERN, self._cve_repl, text, re.I|re.U) if pre: return "
%s
" % text return text + def _bugzilla_repl(self, m): + bug_id = m.group(1) + + # Get the URL + bug_url = self.backend.bugzilla.bug_url(bug_id) + + return self.LINK % (bug_url, m.group(0)) + + def _cve_repl(self, m): + return self.LINK % ("http://cve.mitre.org/cgi-bin/cvename.cgi?name=%s" % m.group(1), m.group(0)) + class CommitMessageModule(TextModule): def render(self, commit):