From patchwork Thu Apr 16 01:29:27 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeremy Kerr X-Patchwork-Id: 1271471 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [203.11.71.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 492hXl0gF3z9sR4 for ; Thu, 16 Apr 2020 11:32:07 +1000 (AEST) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=ozlabs.org Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; secure) header.d=ozlabs.org header.i=@ozlabs.org header.a=rsa-sha256 header.s=201707 header.b=YaiN4BlP; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 492hXk6pt4zDrCW for ; Thu, 16 Apr 2020 11:32:06 +1000 (AEST) X-Original-To: patchwork@lists.ozlabs.org Delivered-To: patchwork@lists.ozlabs.org Received: from ozlabs.org (bilbo.ozlabs.org [IPv6:2401:3900:2:1::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 492hVd6pF6zDr9S for ; Thu, 16 Apr 2020 11:30:17 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dmarc=pass (p=none dis=none) header.from=ozlabs.org Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=ozlabs.org header.i=@ozlabs.org header.a=rsa-sha256 header.s=201707 header.b=YaiN4BlP; dkim-atps=neutral Received: by ozlabs.org (Postfix, from userid 1023) id 492hVd2bV5z9sSk; Thu, 16 Apr 2020 11:30:16 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ozlabs.org; s=201707; t=1587000617; bh=+DPLSixTnZg7be4nqEkxEIOQur6vqey7EuhsQyE6bd8=; h=From:To:Subject:Date:In-Reply-To:References:From; b=YaiN4BlP77D/IV85+TKxUF/2B3jDtdfbjT94AoVsmSA98ODNRF45pRPtx4HZCi/ax iS5mt0d12ZI3pDAAfA1wgS21kAQHHmMxWdS6pkhm1qwigwnByMi0d4+ITeqVyqzGSb YsAq3PzqXsaduh3I9y1Gtm3IuWgOF+ogk4b4QNN+dG8ZFRCx7mPJ1T/TY+ABZinj4n 9WhQ1HSghfkVNvLQstJK/+fYzG3rUfPBE3eWEbiMkS1yqp5mmI8i1hYH9yrbSU41ZN N2j6cvMaR7nIDND5o1jxkATJDQAgCPFymDmllmbDbaZUAvfK7zxebbVc3x8tH/oFx5 qPT/znIyIPqfg== From: Jeremy Kerr To: patchwork@lists.ozlabs.org Subject: [PATCH 4/5] parser: don't trigger database IntegrityErrors on duplicate comments Date: Thu, 16 Apr 2020 09:29:27 +0800 Message-Id: <20200416012928.23893-5-jk@ozlabs.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200416012928.23893-1-jk@ozlabs.org> References: <20200416012928.23893-1-jk@ozlabs.org> X-BeenThere: patchwork@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Patchwork development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: patchwork-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Patchwork" As we've done for the Patch model, this change prevents database errors from duplicate Comments. Signed-off-by: Jeremy Kerr --- 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@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)