[PATCH 4/5] parser: don't trigger database IntegrityErrors on duplicate comments

Jeremy Kerr jk at ozlabs.org
Thu Apr 16 11:29:27 AEST 2020


As we've done for the Patch model, this change prevents database errors
from duplicate Comments.

Signed-off-by: Jeremy Kerr <jk at ozlabs.org>
---
 patchwork/parser.py            |  6 +++---
 patchwork/tests/test_parser.py | 12 ++++++++++++
 2 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/patchwork/parser.py b/patchwork/parser.py
index e03634a..406c916 100644
--- a/patchwork/parser.py
+++ b/patchwork/parser.py
@@ -1247,7 +1247,9 @@ def parse_mail(mail, list_id=None):
 
     author = get_or_create_author(mail, project)
 
-    try:
+    with transaction.atomic():
+        if Comment.objects.filter(submission=submission, msgid=msgid):
+            raise DuplicateMailError(msgid=msgid)
         comment = Comment.objects.create(
             submission=submission,
             msgid=msgid,
@@ -1255,8 +1257,6 @@ def parse_mail(mail, list_id=None):
             headers=headers,
             submitter=author,
             content=message)
-    except IntegrityError:
-        raise DuplicateMailError(msgid=msgid)
 
     logger.debug('Comment saved')
 
diff --git a/patchwork/tests/test_parser.py b/patchwork/tests/test_parser.py
index c7c918a..d1a9a21 100644
--- a/patchwork/tests/test_parser.py
+++ b/patchwork/tests/test_parser.py
@@ -1122,3 +1122,15 @@ class DuplicateMailTest(TestCase):
         self._test_duplicate_mail(m)
 
         self.assertEqual(Patch.objects.count(), 1)
+
+    def test_duplicate_comment(self):
+        diff = read_patch('0001-add-line.patch')
+        m1 = create_email(diff, listid=self.listid, msgid='1 at example.com')
+        _parse_mail(m1)
+
+        m2 = create_email('test', listid=self.listid, msgid='2 at example.com',
+                          in_reply_to='1 at example.com')
+        self._test_duplicate_mail(m2)
+
+        self.assertEqual(Patch.objects.count(), 1)
+        self.assertEqual(Comment.objects.count(), 1)
-- 
2.17.1



More information about the Patchwork mailing list