fix: git links in comments for multiple OIDs

The chance of finding multiple object IDs when performing lookups with
a shortened SHA1 hash (7 digits) seems to be quite high.

In those cases pygit2 will throw an error.
Let's catch those exceptions and gracefully handle them.

Fixes: aurweb-errors#496 (and alike)
Signed-off-by: moson <moson@archlinux.org>
This commit is contained in:
moson 2023-07-16 13:27:02 +02:00
parent 862221f5ce
commit 5729d6787f
No known key found for this signature in database
GPG key ID: 4A4760AB4EE15296
2 changed files with 46 additions and 2 deletions

View file

@ -72,8 +72,13 @@ class GitCommitsInlineProcessor(markdown.inlinepatterns.InlineProcessor):
def handleMatch(self, m, data):
oid = m.group(1)
if oid not in self._repo:
# Unknown OID; preserve the orginal text.
# Lookup might raise ValueError in case multiple object ID's were found
try:
if oid not in self._repo:
# Unknown OID; preserve the orginal text.
return None, None, None
except ValueError:
# Multiple OID's found; preserve the orginal text.
return None, None, None
el = Element("a")

View file

@ -1,5 +1,7 @@
import os
from unittest import mock
import pygit2
import pytest
from aurweb import aur_logging, config, db, time
@ -166,6 +168,43 @@ http://example.com/{commit_hash}\
assert comment.RenderedComment == expected
def test_git_commit_link_multiple_oids(
git: GitRepository, user: User, package: Package
):
# Make sure we get reproducible hashes by hardcoding the dates
date = "Sun, 16 Jul 2023 06:06:06 +0200"
os.environ["GIT_COMMITTER_DATE"] = date
os.environ["GIT_AUTHOR_DATE"] = date
# Package names that cause two object IDs starting with "09a3468"
pkgnames = [
"bfa3e330-23c5-11ee-9b6c-9c2dcdf2810d",
"54c6a420-23c6-11ee-9b6c-9c2dcdf2810d",
]
# Create our commits
for pkgname in pkgnames:
with db.begin():
package = db.create(
Package, PackageBase=package.PackageBase, Name=pkgname, Version="1.0"
)
git.commit(package, pkgname)
repo_path = config.get("serve", "repo-path")
repo = pygit2.Repository(repo_path)
# Make sure we get an error when we search the git repo for "09a3468"
with pytest.raises(ValueError) as oid_error:
assert "09a3468" in repo
assert "ambiguous OID prefix" in oid_error
# Create a comment, referencing "09a3468"
comment = create_comment(user, package.PackageBase, "Commit 09a3468 is nasty!")
# Make sure our comment does not contain a link.
assert comment.RenderedComment == "<p>Commit 09a3468 is nasty!</p>"
def test_flyspray_issue_link(user: User, pkgbase: PackageBase):
text = """\
FS#1234567.