]> git.ipfire.org Git - thirdparty/patchwork.git/commitdiff
parser: don't trigger database IntegrityErrors on duplicate coverletters
authorJeremy Kerr <jk@ozlabs.org>
Thu, 16 Apr 2020 01:29:28 +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 and Comment models, this change prevents
database errors from duplicate CoverLetters.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
Signed-off-by: Stephen Finucane <stephen@that.guru>
[stephenfin: Add release note]
(cherry picked from commit 55fb26bf5bb3ca81ae35426efa9b2410e206c8b2)

patchwork/parser.py
patchwork/tests/test_parser.py
releasenotes/notes/issue-358-7d664318a19385fa.yaml [new file with mode: 0644]

index 0f21e0f7d7fdac28272090bbd77956a90f474b51..a1c4a8b40401b491e7cb6daf20c71f84ec24ee71 100644 (file)
@@ -1250,7 +1250,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,
@@ -1259,8 +1262,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')
 
index f06a6f8ebd6bf4cb52918653418b78faf822c9df..adefac491a2ad25b90696e8f151f47494ac48e27 100644 (file)
@@ -23,6 +23,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
@@ -1180,3 +1181,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@example.com')
+        del m['Subject']
+        m['Subject'] = '[PATCH 0/1] test cover letter'
+
+        self._test_duplicate_mail(m)
+
+        self.assertEqual(CoverLetter.objects.count(), 1)
diff --git a/releasenotes/notes/issue-358-7d664318a19385fa.yaml b/releasenotes/notes/issue-358-7d664318a19385fa.yaml
new file mode 100644 (file)
index 0000000..a0eeaa0
--- /dev/null
@@ -0,0 +1,7 @@
+---
+fixes:
+  - |
+    The parser module now uses an atomic select-insert when creating new patch,
+    cover letter and comment entries. This prevents the integrity errors from
+    being logged in the DB logs.
+    (`#358 <https://github.com/getpatchwork/patchwork/issues/358>`__)