Commit bb623fc5 authored by Lukas Fleischer's avatar Lukas Fleischer
Browse files

Make references to Git commits clickable



Automatically detect Git commit identifiers, shorten them, and make them
link to the cgit interface.

Implements FS#43290.

Signed-off-by: Lukas Fleischer's avatarLukas Fleischer <lfleischer@archlinux.org>
parent a9ac385c
#!/usr/bin/python3 #!/usr/bin/python3
import re import re
import pygit2
import sys import sys
import bleach import bleach
import markdown import markdown
import aurweb.config
import aurweb.db import aurweb.db
repo_path = aurweb.config.get('serve', 'repo-path')
commit_uri = aurweb.config.get('options', 'commit_uri')
class LinkifyPreprocessor(markdown.preprocessors.Preprocessor): class LinkifyPreprocessor(markdown.preprocessors.Preprocessor):
_urlre = re.compile(r'(\b(?:https?|ftp):\/\/[\w\/\#~:.?+=&%@!\-;,]+?' _urlre = re.compile(r'(\b(?:https?|ftp):\/\/[\w\/\#~:.?+=&%@!\-;,]+?'
...@@ -21,10 +26,53 @@ class LinkifyExtension(markdown.extensions.Extension): ...@@ -21,10 +26,53 @@ class LinkifyExtension(markdown.extensions.Extension):
md.preprocessors.add('linkify', LinkifyPreprocessor(md), '_end') md.preprocessors.add('linkify', LinkifyPreprocessor(md), '_end')
class GitCommitsPreprocessor(markdown.preprocessors.Preprocessor):
_oidre = re.compile(r'(\b)([0-9a-f]{7,40})(\b)')
_repo = pygit2.Repository(repo_path)
_head = None
def __init__(self, md, head):
self._head = head
super(markdown.preprocessors.Preprocessor, self).__init__(md)
def handleMatch(self, m):
oid = m.group(2)
if oid not in self._repo:
return oid
prefixlen = 12
while prefixlen < 40:
if oid[:prefixlen] in self._repo:
break
prefixlen += 1
html = '[`' + oid[:prefixlen] + '`]'
html += '(' + commit_uri % (self._head, oid[:prefixlen]) + ')'
return html
def run(self, lines):
return [self._oidre.sub(self.handleMatch, line) for line in lines]
class GitCommitsExtension(markdown.extensions.Extension):
_head = None
def __init__(self, head):
self._head = head
super(markdown.extensions.Extension, self).__init__()
def extendMarkdown(self, md, md_globals):
preprocessor = GitCommitsPreprocessor(md, self._head)
md.preprocessors.add('git-commits', preprocessor, '_end')
def get_comment(conn, commentid): def get_comment(conn, commentid):
cur = conn.execute('SELECT Comments FROM PackageComments WHERE ID = ?', cur = conn.execute('SELECT PackageComments.Comments, PackageBases.Name '
[commentid]) 'FROM PackageComments INNER JOIN PackageBases '
return cur.fetchone()[0] 'ON PackageBases.ID = PackageComments.PackageBaseID '
'WHERE PackageComments.ID = ?', [commentid])
return cur.fetchone()
def save_rendered_comment(conn, commentid, html): def save_rendered_comment(conn, commentid, html):
...@@ -37,8 +85,9 @@ def main(): ...@@ -37,8 +85,9 @@ def main():
conn = aurweb.db.Connection() conn = aurweb.db.Connection()
text = get_comment(conn, commentid) text, pkgbase = get_comment(conn, commentid)
html = markdown.markdown(text, extensions=['nl2br', LinkifyExtension()]) html = markdown.markdown(text, extensions=['nl2br', LinkifyExtension(),
GitCommitsExtension(pkgbase)])
allowed_tags = bleach.sanitizer.ALLOWED_TAGS + ['p', 'br'] allowed_tags = bleach.sanitizer.ALLOWED_TAGS + ['p', 'br']
html = bleach.clean(html, tags=allowed_tags) html = bleach.clean(html, tags=allowed_tags)
save_rendered_comment(conn, commentid, html) save_rendered_comment(conn, commentid, html)
......
...@@ -30,6 +30,7 @@ auto_orphan_age = 15552000 ...@@ -30,6 +30,7 @@ auto_orphan_age = 15552000
auto_delete_age = 86400 auto_delete_age = 86400
source_file_uri = https://aur.archlinux.org/cgit/aur.git/tree/%s?h=%s source_file_uri = https://aur.archlinux.org/cgit/aur.git/tree/%s?h=%s
log_uri = https://aur.archlinux.org/cgit/aur.git/log/?h=%s log_uri = https://aur.archlinux.org/cgit/aur.git/log/?h=%s
commit_uri = https://aur.archlinux.org/cgit/aur.git/commit/?h=%s&id=%s
snapshot_uri = /cgit/aur.git/snapshot/%s.tar.gz snapshot_uri = /cgit/aur.git/snapshot/%s.tar.gz
enable-maintenance = 1 enable-maintenance = 1
maintenance-exceptions = 127.0.0.1 maintenance-exceptions = 127.0.0.1
......
...@@ -29,6 +29,7 @@ aur_location = https://aur.archlinux.org ...@@ -29,6 +29,7 @@ aur_location = https://aur.archlinux.org
aur_request_ml = aur-requests@archlinux.org aur_request_ml = aur-requests@archlinux.org
enable-maintenance = 0 enable-maintenance = 0
maintenance-exceptions = 127.0.0.1 maintenance-exceptions = 127.0.0.1
commit_uri = https://aur.archlinux.org/cgit/aur.git/log/?h=%s&id=%s
[notifications] [notifications]
notify-cmd = $NOTIFY notify-cmd = $NOTIFY
......
...@@ -6,6 +6,7 @@ test_description='rendercomment tests' ...@@ -6,6 +6,7 @@ test_description='rendercomment tests'
test_expect_success 'Test comment rendering.' ' test_expect_success 'Test comment rendering.' '
cat <<-EOD | sqlite3 aur.db && cat <<-EOD | sqlite3 aur.db &&
INSERT INTO PackageBases (ID, Name, PackagerUID, SubmittedTS, ModifiedTS, FlaggerComment) VALUES (1, "foobar", 1, 0, 0, "");
INSERT INTO PackageComments (ID, PackageBaseID, Comments, RenderedComment) VALUES (1, 1, "Hello world! INSERT INTO PackageComments (ID, PackageBaseID, Comments, RenderedComment) VALUES (1, 1, "Hello world!
This is a comment.", ""); This is a comment.", "");
EOD EOD
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment