]> git.ipfire.org Git - thirdparty/patchwork.git/commitdiff
parser: don't trigger database IntegrityErrors on duplicate comments
authorJeremy Kerr <jk@ozlabs.org>
Thu, 16 Apr 2020 01:29:27 +0000 (09:29 +0800)
committerStephen Finucane <stephen@that.guru>
Sat, 18 Apr 2020 11:57:17 +0000 (12:57 +0100)
As we've done for the Patch model, this change prevents database errors
from duplicate Comments.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
Reviewed-by: Stephen Finucane <stephen@that.guru>
(cherry picked from commit 55aa9cd749f3ff0de430c8f04c687d691c3a703a)

patchwork/parser.py
patchwork/tests/test_parser.py

index 76b89d3698830b63f393f85e7df9b3f787b20cd3..0f21e0f7d7fdac28272090bbd77956a90f474b51 100644 (file)
@@ -1277,7 +1277,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,
@@ -1285,8 +1287,6 @@ def parse_mail(mail, list_id=None):
             headers=headers,
             submitter=author,
             content=message)
-    except IntegrityError:
-        raise DuplicateMailError(msgid=msgid)
 
     logger.debug('Comment saved')
 
index 4041ef360fe63ad2cedc804f31909f3b0e51e927..f06a6f8ebd6bf4cb52918653418b78faf822c9df 100644 (file)
@@ -1168,3 +1168,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@example.com')
+        _parse_mail(m1)
+
+        m2 = create_email('test', listid=self.listid, msgid='2@example.com',
+                          in_reply_to='1@example.com')
+        self._test_duplicate_mail(m2)
+
+        self.assertEqual(Patch.objects.count(), 1)
+        self.assertEqual(Comment.objects.count(), 1)