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

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


As we've done for the Patch and Comment models, this change prevents
database errors from duplicate CoverLetters.

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

diff --git a/patchwork/parser.py b/patchwork/parser.py
index 406c916..b9eb88c 100644
--- a/patchwork/parser.py
+++ b/patchwork/parser.py
@@ -1220,7 +1220,10 @@ def parse_mail(mail, list_id=None):
                 SeriesReference.objects.create(
                     msgid=msgid, project=project, series=series)
 
-            try:
+            with transaction.atomic():
+                if CoverLetter.objects.filter(project=project, msgid=msgid):
+                    raise DuplicateMailError(msgid=msgid)
+
                 cover_letter = CoverLetter.objects.create(
                     msgid=msgid,
                     project=project,
@@ -1229,8 +1232,6 @@ def parse_mail(mail, list_id=None):
                     headers=headers,
                     submitter=author,
                     content=message)
-            except IntegrityError:
-                raise DuplicateMailError(msgid=msgid)
 
             logger.debug('Cover letter saved')
 
diff --git a/patchwork/tests/test_parser.py b/patchwork/tests/test_parser.py
index d1a9a21..cdf299c 100644
--- a/patchwork/tests/test_parser.py
+++ b/patchwork/tests/test_parser.py
@@ -21,6 +21,7 @@ from patchwork.models import Comment
 from patchwork.models import Patch
 from patchwork.models import Person
 from patchwork.models import State
+from patchwork.models import CoverLetter
 from patchwork.parser import clean_subject
 from patchwork.parser import get_or_create_author
 from patchwork.parser import find_patch_content as find_content
@@ -1134,3 +1135,12 @@ class DuplicateMailTest(TestCase):
 
         self.assertEqual(Patch.objects.count(), 1)
         self.assertEqual(Comment.objects.count(), 1)
+
+    def test_duplicate_coverletter(self):
+        m = create_email('test', listid=self.listid, msgid='1 at example.com')
+        del m['Subject']
+        m['Subject'] = '[PATCH 0/1] test cover letter'
+
+        self._test_duplicate_mail(m)
+
+        self.assertEqual(CoverLetter.objects.count(), 1)
-- 
2.17.1



More information about the Patchwork mailing list