From: Jeremy Kerr Date: Thu, 16 Apr 2020 01:29:26 +0000 (+0800) Subject: parser: prevent IntegrityErrors X-Git-Tag: v2.2.2~5 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=91dce3e077ebdef833afd06a8407e1012929d929;p=thirdparty%2Fpatchwork.git parser: prevent IntegrityErrors Currently, the parser relies on causing (and catching) IntegrityErrors on patch insert to catch duplicate (msgid,project) mails. This change performs an atomic select -> insert instead. Signed-off-by: Jeremy Kerr Signed-off-by: Stephen Finucane [stephenfin: Remove 'expectedFailure' marker again] (cherry picked from commit 947c6aae94b7b554ca701c1d7e5baf000759ed2d) --- diff --git a/patchwork/parser.py b/patchwork/parser.py index cf3e3cf0..76b89d36 100644 --- a/patchwork/parser.py +++ b/patchwork/parser.py @@ -1092,7 +1092,10 @@ def parse_mail(mail, list_id=None): filenames = find_filenames(diff) delegate = find_delegate_by_filename(project, filenames) - try: + with transaction.atomic(): + if Patch.objects.filter(project=project, msgid=msgid): + raise DuplicateMailError(msgid=msgid) + patch = Patch.objects.create( msgid=msgid, project=project, @@ -1107,8 +1110,6 @@ def parse_mail(mail, list_id=None): delegate=delegate, state=find_state(mail)) logger.debug('Patch saved') - except IntegrityError: - raise DuplicateMailError(msgid=msgid) for attempt in range(1, 11): # arbitrary retry count try: diff --git a/patchwork/tests/test_parser.py b/patchwork/tests/test_parser.py index 11dcb21f..4041ef36 100644 --- a/patchwork/tests/test_parser.py +++ b/patchwork/tests/test_parser.py @@ -1161,7 +1161,6 @@ class DuplicateMailTest(TestCase): self.assertEqual(errors, []) - @unittest.expectedFailure def test_duplicate_patch(self): diff = read_patch('0001-add-line.patch') m = create_email(diff, listid=self.listid, msgid='1@example.com')