From patchwork Sun Apr 15 22:53:57 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Finucane X-Patchwork-Id: 898326 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.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 40PRfY1Zh5z9s06 for ; Mon, 16 Apr 2018 08:54:45 +1000 (AEST) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=that.guru Authentication-Results: ozlabs.org; dkim=fail reason="key not found in DNS" (0-bit key; unprotected) header.d=that.guru header.i=@that.guru header.b="S8Dxg0mH"; 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 40PRfX74SRzF1sD for ; Mon, 16 Apr 2018 08:54:44 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=that.guru Authentication-Results: lists.ozlabs.org; dkim=fail reason="key not found in DNS" (0-bit key; unprotected) header.d=that.guru header.i=@that.guru header.b="S8Dxg0mH"; dkim-atps=neutral X-Original-To: patchwork@lists.ozlabs.org Delivered-To: patchwork@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=none (mailfrom) smtp.mailfrom=that.guru (client-ip=23.83.214.30; helo=caracal.maple.relay.mailchannels.net; envelope-from=stephen@that.guru; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=that.guru Authentication-Results: lists.ozlabs.org; dkim=fail reason="key not found in DNS" (0-bit key; unprotected) header.d=that.guru header.i=@that.guru header.b="S8Dxg0mH"; dkim-atps=neutral Received: from caracal.maple.relay.mailchannels.net (caracal.maple.relay.mailchannels.net [23.83.214.30]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 40PRf24Q5qzF1Rt for ; Mon, 16 Apr 2018 08:54:18 +1000 (AEST) X-Sender-Id: 5xi41l16bi|x-authuser|stephen@that.guru Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 3F381121F4F for ; Sun, 15 Apr 2018 22:54:15 +0000 (UTC) Received: from one.mxroute.com (unknown [100.96.15.83]) (Authenticated sender: 5xi41l16bi) by relay.mailchannels.net (Postfix) with ESMTPA id C6E331220BD for ; Sun, 15 Apr 2018 22:54:14 +0000 (UTC) X-Sender-Id: 5xi41l16bi|x-authuser|stephen@that.guru Received: from one.mxroute.com (one-outgoing.mxroute.com [100.96.15.1]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384) by 0.0.0.0:2500 (trex/5.14.1); Sun, 15 Apr 2018 22:54:15 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: 5xi41l16bi|x-authuser|stephen@that.guru X-MailChannels-Auth-Id: 5xi41l16bi X-Whimsical-Gusty: 0456a4a75bb4e413_1523832855072_343111189 X-MC-Loop-Signature: 1523832855072:2474096927 X-MC-Ingress-Time: 1523832855071 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=that.guru; s=default; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=RDPdiChYowQrnSQ338OV/bkn6nr29G98SFThUhljjrs=; b=S8Dxg0mHh1Tmy1cJOmFdJZfLkm IN+0iuMFXMvjaMyVET9kagO1GDKi3eCyka3z28xsbgFdNHByCQyQrkTf+VvWQp+xHOkKzJUFxoc7A 09lXHDMrkJpUadLtmsT9w8fATk07Qi6JEYXhjR398TBOT2A0Jo7xS9znK9w9s72hwjD044lJFW9xy jPVxg3bw2tmaIZFJ5GQv2Te6tbntnEcaio4ffk4+lWBVs1FKJ5G23MhZbz9HHh4dTtxM4KbhlHaii 5fNTbzRcjEueB+O/nlh/rSWW/MlncgVExsEpw3ZR3eX6V4Bo18nJ3XIPHhco7zWV4x0AIk0S5hKVk wPs0ABfA==; From: Stephen Finucane To: patchwork@lists.ozlabs.org Subject: [PATCH 03/11] parser: Extract and save labels Date: Sun, 15 Apr 2018 23:53:57 +0100 Message-Id: <20180415225405.1354-4-stephen@that.guru> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180415225405.1354-1-stephen@that.guru> References: <20180415225405.1354-1-stephen@that.guru> X-AuthUser: stephen@that.guru X-BeenThere: patchwork@lists.ozlabs.org X-Mailman-Version: 2.1.26 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" Signed-off-by: Stephen Finucane --- patchwork/parser.py | 17 +++++++++++++++++ patchwork/tests/test_parser.py | 16 ++++++++++++++++ patchwork/tests/utils.py | 15 +++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/patchwork/parser.py b/patchwork/parser.py index 805037c7..7ac527a2 100644 --- a/patchwork/parser.py +++ b/patchwork/parser.py @@ -29,12 +29,14 @@ import logging import re from django.contrib.auth.models import User +from django.db.models import Q from django.utils import six from patchwork.models import Comment from patchwork.models import CoverLetter from patchwork.models import DelegationRule from patchwork.models import get_default_initial_patch_state +from patchwork.models import Label from patchwork.models import Patch from patchwork.models import Person from patchwork.models import Project @@ -497,6 +499,16 @@ def parse_version(subject, subject_prefixes): return 1 +def parse_labels(subject_prefixes, project): + """Extract labels from subject. + + Args: + subject_prefixes: List of subject prefixes to extract tags from + """ + return Label.objects.filter(Q(project=project) | Q(project=None), + name__in=subject_prefixes) + + def _find_content(mail): """Extract the payload(s) from a mail. @@ -1001,6 +1013,8 @@ def parse_mail(mail, list_id=None): filenames = find_filenames(diff) delegate = find_delegate_by_filename(project, filenames) + labels = parse_labels(prefixes, project) + patch = Patch.objects.create( msgid=msgid, project=project, @@ -1016,6 +1030,9 @@ def parse_mail(mail, list_id=None): state=find_state(mail)) logger.debug('Patch saved') + if labels: + patch.labels.set(labels) + # if we don't have a series marker, we will never have an existing # series to match against. series = None diff --git a/patchwork/tests/test_parser.py b/patchwork/tests/test_parser.py index 5ba06c0f..50a07523 100644 --- a/patchwork/tests/test_parser.py +++ b/patchwork/tests/test_parser.py @@ -38,6 +38,7 @@ from patchwork.parser import get_or_create_author from patchwork.parser import find_patch_content as find_content from patchwork.parser import find_project from patchwork.parser import find_series +from patchwork.parser import parse_labels from patchwork.parser import parse_mail as _parse_mail from patchwork.parser import parse_pull_request from patchwork.parser import parse_series_marker @@ -46,6 +47,7 @@ from patchwork.parser import split_prefixes from patchwork.parser import subject_check from patchwork.tests import TEST_MAIL_DIR from patchwork.tests import TEST_FUZZ_DIR +from patchwork.tests.utils import create_label from patchwork.tests.utils import create_project from patchwork.tests.utils import create_series from patchwork.tests.utils import create_series_reference @@ -888,6 +890,20 @@ class SubjectTest(TestCase): self.assertEqual(parse_version('Hello, world (v2)', []), 2) self.assertEqual(parse_version('Hello, world (V6)', []), 6) + def test_labels(self): + label = create_label(name='RFC') + + self.assertEqual(list(parse_labels(['RFC'], label.project)), [label]) + self.assertEqual(list(parse_labels(['rfcx'], label.project)), []) + + project = create_project() + + self.assertEqual(list(parse_labels(['RFC'], project)), []) + + label = create_label(name='stuff', project=None) + + self.assertEqual(list(parse_labels(['stuff'], project)), [label]) + class SubjectMatchTest(TestCase): diff --git a/patchwork/tests/utils.py b/patchwork/tests/utils.py index 00eb6c2a..e51862fc 100644 --- a/patchwork/tests/utils.py +++ b/patchwork/tests/utils.py @@ -29,6 +29,7 @@ from patchwork.models import Bundle from patchwork.models import Check from patchwork.models import Comment from patchwork.models import CoverLetter +from patchwork.models import Label from patchwork.models import Patch from patchwork.models import Person from patchwork.models import Project @@ -266,6 +267,20 @@ def create_series_reference(**kwargs): return SeriesReference.objects.create(**values) +def create_label(**kwargs): + """Create a 'Label' object.""" + num = Label.objects.count() + + values = { + 'name': 'label%d' % num, + 'project': create_project() if 'project' not in kwargs else None, + 'color': '#fff', + } + values.update(**kwargs) + + return Label.objects.create(**values) + + def _create_submissions(create_func, count=1, **kwargs): """Create 'count' Submission-based objects.